Results 1 to 10 of 10

Thread: Transparent contextmenustrip image margin

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    82

    Transparent contextmenustrip image margin

    Currently the image margin is XP default blue.

    any suggestions what is going wrong...

    Code:
    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
    example


  2. #2

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    82

    Re: Transparent contextmenustrip image margin

    Any one?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    82

    Re: Transparent contextmenustrip image margin

    A gentle nudge

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    82

    Re: Transparent contextmenustrip image margin

    Wow why is this so hard? lol VB6 was so easy. Still not resolved.

  5. #5
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Transparent contextmenustrip image margin

    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.

    BB

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    82

    Re: Transparent contextmenustrip image margin

    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.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    82

    Re: Transparent contextmenustrip image margin

    Surely this can simply be over ridden by using Me.BackColor = Color.Transparent :/ you would of thought any way.

  8. #8
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Transparent contextmenustrip image margin

    Quote Originally Posted by freeblog View Post
    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
    Attached Images Attached Images  

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    82

    Re: Transparent contextmenustrip image margin

    The custom control now freezes VS. Also acts very odd. Clicking properties with the menu selected goes to a different controls properties. Any idea?

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    82

    Re: Transparent contextmenustrip image margin

    Even lead to VS removing a reference to my main user control. Would not appear in the intellisense. Coincidence? Hmm not sure.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width