Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image Margin Background 1

Status
Not open for further replies.

snyperx3

Programmer
May 31, 2005
467
US
I am using VB.Net 2008. Is there a way to set the background of an image margin as an image? I have a system tray icon that has a context menu associated with it. I want the image margin of that context menu to have a single image displayed in it. The best example of this is with Daemon Tools (see attached image).

Thanks!

-Pete
 
You can extend the ContextMenuStrip class. Create a new class called CustomContextMenu. Paste this code into the class. Drag one of these CustomContextMenu controls from your ToolBox onto your form. Add a couple of items. Set the MarginImage property of the control to some image. Run your project and test it out. Modify as needed.
Code:
Public Class CustomContextMenu
    Inherits ContextMenuStrip

    Private mImage As Image

    Private Sub CustomContextMenu_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
        Me.Refresh()
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)

        If Me.Items.Count >= 1 And Me.mImage IsNot Nothing Then
            If Me.mImage.Width > 24 Then Call Me.ResizeImage()
            e.Graphics.DrawImage(Me.mImage, New PointF(0, 0))
        End If
    End Sub

    Public Property MarginImage() As Image
        Get
            Return Me.mImage
        End Get
        Set(ByVal value As Image)
            Me.mImage = value
            Me.Refresh()
        End Set
    End Property

    Private Sub ResizeImage()
        If Me.mImage IsNot Nothing Then
            Dim bmp As New Bitmap(24, Me.mImage.Height)
            Dim g As Graphics = Graphics.FromImage(bmp)
            g.DrawImage(Me.mImage, New PointF(0, 0))
            g.Dispose()
            g = Nothing
            Me.mImage.Dispose()
            Me.mImage = Nothing
            Me.mImage = bmp
        End If
    End Sub
End Class
 
Wow, that works great! Thanks.

One more thing that I havent been able to figure out: Is there a way to force the image I have to stay on the bottom of the margin? The image I have is 190 pixels tall and 24 wide, but the menu options make the menu larger than 190 pixels. When the menu is displayed, it puts the image at the top of the menu, but I would like to have it aligned on the bottom.

Thanks!

-Pete
 
Yes you can change this line of code:

e.Graphics.DrawImage(Me.mImage, New PointF(0, 0))

instead of drawing at coordinates 0,0, you can have it draw lower down by adjusting the y coordinate.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top