Transparent ContextMenuStrip with non-transparent text

H

Hans Merkl

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
 
B

Bryan Phillips

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?
 
H

Hans Merkl

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);
}
 
B

Bryan Phillips

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.
 
H

Hans Merkl

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
 
Joined
Feb 3, 2008
Messages
2
Reaction score
0
Problem with shortcuts

I'm working on the same problem and with:
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.TextRe ctangle);

base.OnRenderItemText(e);
}

... the text is overdrawn on the different position. If I exclude the line
base.OnRenderItemText(e); than everything is OK except the shortcuts are drawn over the text. Anyone know how to avoid it?
 
Joined
Feb 3, 2008
Messages
2
Reaction score
0
virtual_from_croatia said:
I'm working on the same problem and with:
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.TextRe ctangle);

base.OnRenderItemText(e);
}

... the text is overdrawn on the different position. If I exclude the line
base.OnRenderItemText(e); than everything is OK except the shortcuts are drawn over the text. Anyone know how to avoid it?


OK, found the answer on my question. Instead to use e.Graphics... use:

TextRenderer.DrawText(e.Graphics, e.Text, e.TextFont, e.TextRectangle, b, e.TextFormat);

and delete the
line base.OnRenderItemText(e);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top