解决方案非常简单。您必须使用自定义ToolStripRenderer
并覆盖该OnRenderImageMargin
方法。您还需要准备一个尺寸合适的徽标图像。
这是代码:
public class Form1 : Form {
public Form1(){
InitializeComponent();
// This contextMenuStrip is used for your Notify Icon
// Just show it as you do
contextMenuStrip1.Renderer = new CustomRenderer();
}
}
public class CustomRenderer : ToolStripProfessionalRenderer
{
protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
{
e.Graphics.DrawImage(yourImage, e.AffectedBounds);
}
}
注意:您的图像应该已经旋转了 90 度。否则,您将不得不在绘制之前使用代码旋转它。
这是上面代码的屏幕截图,使用了 Stack Overflow 标志:
编辑后,您似乎想要一些不同的东西。您可能希望显示拉伸项目整个区域的徽标。我想这是最后一项。您必须使用 . 添加一个项目Text = string.Empty
。这是代码:
public class Form1 : Form {
public Form1(){
InitializeComponent();
// This contextMenuStrip is used for your Notify Icon
// Just show it as you do
contextMenuStrip1.Renderer = new CustomRenderer(){RootToolStrip = contextMenuStrip1};
//Add your last item first
int lastItemIndex = contextMenuStrip1.Items.Count - 1;
contextMenuStrip1.Items[lastItemIndex].AutoSize = false;
contextMenuStrip1.Items[lastItemIndex].Text = "";
contextMenuStrip1.Items[lastItemIndex].Height = 40;
}
}
public class CustomRenderer : ToolStripProfessionalRenderer
{
public ToolStrip RootToolStrip;
protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
int i = e.ToolStrip.Items.Count - 1;
if (e.ToolStrip.Items.IndexOf(e.Item) == i&&e.ToolStrip == RootToolStrip)
{
e.Graphics.DrawImage(yourImage, new Rectangle(0,0,e.Item.Width, e.Item.Height));
} else base.OnRenderMenuItemBackground(e);
}
}
截屏: