Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2007

Tip: Looking for answers? Try searching our database.

Transparent ContextMenuStrip with non-transparent text

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hans Merkl - 16 Jan 2007 21:19 GMT
Hi,

I am trying to implement a context menu that is 50% transparent. Setting
the opacity to 0.5 works but unfortunately the texts are also displayed
with transparency which makes them very hard to read.

Now I would like to draw the menu background with transparency but the text
without.

I have tried various approaches without success:

- Implement a ToolstripRenderer: Draws a gray background despite overriding
all overridable methods
- Handle Paint event of each menu item: Text gets drawn with transparency

Does anybody have an idea how to approach this?

Thanks in advance,

Hans
Bryan Phillips - 17 Jan 2007 01:29 GMT
Take a look at this example:  
http://www.danielmoth.com/Blog/2006/06/vista-glass-in-c_17.html

--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog:  http://bphillips76.spaces.live.com

"hans_merkl@rhmmedia.com" <hans_merkl@rhmmedia.com> wrote in message
news:5v9zmgwhz9fx.1nbl8kl1cx43e.dlg@40tude.net:

> Hi,
>
[quoted text clipped - 16 lines]
>
> Hans
Hans Merkl - 17 Jan 2007 03:17 GMT
> Take a look at this example:  
> http://www.danielmoth.com/Blog/2006/06/vista-glass-in-c_17.html

Bryan,

It seems that code works only with Vista. Do youn think I can use parts of
it with Win 2000 or XP?

Hans
Bryan Phillips - 17 Jan 2007 03:45 GMT
Sorry about that.  I don't think the example would be useful.  Have you
tried to override the onpaint method and draw the text using
Graphics.DrawString?

--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog:  http://bphillips76.spaces.live.com

"rhmware@newsgroups.nospam" <rhmware@newsgroups.nospam> wrote in message
news:154m75i6wo4dh.l6g3yd4i64hz.dlg@40tude.net:

> > Take a look at this example:
> > http://www.danielmoth.com/Blog/2006/06/vista-glass-in-c_17.html
[quoted text clipped - 5 lines]
>
> Hans
Hans Merkl - 17 Jan 2007 16:50 GMT
> Sorry about that.  I don't think the example would be useful.  Have you
> tried to override the onpaint method and draw the text using
> Graphics.DrawString?

Yes. I have tried that but the string gets drawn with the opacity setting
of the menu.

Here is some sample code I have written with a ToolStripRenderer.
It works fine but it seems the menu is also drawing a gray background
behind the transparent background colors that I am drawing. I wonder if
it's possible to prevent the drawing of the gray background.

private void cmdTestScenario_Click(object sender, EventArgs e)
       {
           System.Windows.Forms.ContextMenuStrip cm = new
ContextMenuStrip();
           cm.Renderer = new MyRenderer();
           cm.ShowCheckMargin = false;
           cm.ShowImageMargin = false;

           cm.Items.Add(new ToolStripLabel("Label"));
           ToolStripItem it = null;
           it = cm.Items.Add("Item1");
           it.BackColor = Color.Transparent;

           cm.Items.Add("Item2");

           cm.Show();
       }

       private class MyRenderer : ToolStripRenderer
       {
           protected override void
OnRenderToolStripBackground(ToolStripRenderEventArgs e)
           {
               //draw background with transparent white
               Color bgColor = Color.FromArgb(100,Color.Yellow);
               Brush b = new SolidBrush(bgColor);

               e.Graphics.FillRectangle(b, e.AffectedBounds);

               base.OnRenderToolStripBackground(e);
           }

           protected override void
OnRenderItemBackground(ToolStripItemRenderEventArgs e)
           {
                 base.OnRenderItemBackground(e);
           }

           protected override void
OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
           {
               //draw background with transparent white
               Color bgColor = Color.FromArgb(100, Color.LightBlue);
               Brush b = new SolidBrush(bgColor);

               e.Graphics.FillRectangle(b, e.Item.ContentRectangle);

               base.OnRenderMenuItemBackground(e);
           }

           protected override void
OnRenderItemText(ToolStripItemTextRenderEventArgs e)
           {
               //draw with solid red
               Color bgColor = Color.Red;
               Brush b = new SolidBrush(bgColor);

               e.Graphics.DrawString(e.Text,e.TextFont,b,e.TextRectangle);
               
               base.OnRenderItemText(e);
           }
Bryan Phillips - 18 Jan 2007 03:09 GMT
Remove the override for OnRenderMenuItemBackground. That is where the
gray looking background is coming from.

Also, I was able to disable the opacity change for the drawstring by NOT
calling base.OnRenderItemText(e); in the override for OnRenderItemText.  
The text looks almost crisp.  Let me know if this works for you.

--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog:  http://bphillips76.spaces.live.com

"rhmware@newsgroups.nospam" <rhmware@newsgroups.nospam> wrote in message
news:1xs1vvqiqxzue$.3hiiljcqzy44$.dlg@40tude.net:

> > Sorry about that.  I don't think the example would be useful.  Have you
> > tried to override the onpaint method and draw the text using
[quoted text clipped - 69 lines]
>                 base.OnRenderItemText(e);
>             }
Hans Merkl - 19 Jan 2007 22:31 GMT
Bryan,

I have commented out the calls to the base class but I still don't get
transparency. BTW but I am using Windows 2000.

Can you post your code so I can see what the difference is?

Hans

> Remove the override for OnRenderMenuItemBackground. That is where the
> gray looking background is coming from.
>
> Also, I was able to disable the opacity change for the drawstring by NOT
> calling base.OnRenderItemText(e); in the override for OnRenderItemText.  
> The text looks almost crisp.  Let me know if this works for you.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.