Option Strict On
Imports System.Windows.Forms
Public Class ContextMenu
Inherits ContextMenuStrip
Public Sub New()
Me.RenderMode = ToolStripRenderMode.ManagerRenderMode
Me.Renderer = New Renderer
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
Me.BackColor = Color.Transparent
Me.AllowMerge = True
End Sub
End Class
Public Class Renderer
Inherits ToolStripProfessionalRenderer
Protected Overrides Sub OnRenderToolStripBorder(ByVal e As System.Windows.Forms.ToolStripRenderEventArgs)
e.Graphics.DrawRectangle(Pens.Gainsboro, e.AffectedBounds)
End Sub
Protected Overrides Sub OnRenderMenuItemBackground(ByVal e As ToolStripItemRenderEventArgs)
If e.Item.Selected Then
Using sbr As New SolidBrush(Color.FromArgb(50, 0, 120, 255))
e.Graphics.FillRectangle(sbr, e.Item.ContentRectangle)
End Using
End If
End Sub
End Class
Your code does not explain the problem directly, and the example image is too small on my screen to see exactly what the problem is. However, it looks like there are scruffy edges along the curves and sloping edges of your images. I have no experience with ToolStripProfessionalRenderer, but a quick look in msdn suggest that ImageMargin does not refer to the actual image edge but to a rectangular frame around the image bounds.
Scruffy edges are typically a product of anti-aliasing. It could happen if you use an image which has been anti-aliased against a given background colour. The mixed pixels around the image edge look transparent, but they remain opaque when you make the background color transparent. Even if those pixels really are partially transparent, transparent backgrounds in Windows Forms do not do alpha blending (partial transparency) and the effect will be similar. (I guess that was different in vb6, because I've heard people complain about transparency being "broken" in dotnet.)
However, the Graphics class is able to implement alpha blending in commands such as DrawImage. Assuming your edge pixels really are partially transparent, maybe you could deal with it when you draw the images. This is just guesswork on my part, but maybe you want to look into overriding the ToolStripProfessionalRenderer.OnRenderItemImage method.
Hi BB, sorry the image was an example. When i drag a contexmenu on my form the default XP color sets the image margin blue. I want a context menu i can still add images but leave the image margin transparent.
Surely this can simply be over ridden by using Me.BackColor = Color.Transparent :/ you would of thought any way.
I've never done anything like that so don't really have a clue, I also don't have WinXP, but using Win7 I added this to your class,...
Code:
Protected Overrides Sub OnRenderImageMargin(e As System.Windows.Forms.ToolStripRenderEventArgs)
' by disabling the line below the image margin is now transparent. (Win7 test only)
'MyBase.OnRenderImageMargin(e)
End Sub
For testing I did this and attached an image of what my menu looks like.
Code:
Dim mymenu As New ContxtMenu ' < class name changed due to conflict.
mymenu.BackColor = Color.Coral
mymenu.Items.Add("Testing 123", My.Resources.eventlog.ToBitmap)
mymenu.Items.Add("Testing 456", My.Resources.eventlogInfo.ToBitmap)
mymenu.Items.Add("Testing 789", My.Resources.eventlogWarn.ToBitmap)
Me.ContextMenuStrip = mymenu