Capellan
Capellan

Reputation: 700

VB.NET Draw Button Over Another Button

Is it possible to draw a button on top of another button (besides just having a second button and moving it relative to the first button's Left and Top position)? I'm trying to make a little small optional question mark (button) appear in the corner of the main button that will take various actions if clicked.

Otherwise I was toying with the idea of just drawing the question mark on top of the button and getting the relative position of the mouse OnClick, then taking the action if the HasLink property is true and the mouse is in a specific area.

These will be dynamically created as well.

Public Class clsButton

Inherits Button
Private Property HasLink As Boolean = False

Public Sub New(ByVal Image As Image, ByVal ShowLink As Boolean)

    Me.BackgroundImage = Image
    Me.BackgroundImageLayout = ImageLayout.Center
    Me.Height = 100
    Me.Width = 200
    If ShowLink Then DrawLink()

End Sub

Public Sub DrawLink()

    Dim bmpBitmap As New Bitmap(Me.Width, Me.Height)
    Dim graGraphic As Graphics = Graphics.FromImage(bmpBitmap)

    Dim i As New Bitmap("c:\temp\question_mark.png")
    graGraphic.DrawImage(i, (Me.Width - i.Width) - 5, 5)

    Me.Image = bmpBitmap

End Sub

Protected Overrides Sub OnClick(e As EventArgs)
    MyBase.OnClick(e)
    Debug.WriteLine("X: " & MousePosition.X & " Y: " & MousePosition.Y)
    Debug.WriteLine(Me.PointToClient(MousePosition))
End Sub

End Class

Upvotes: 0

Views: 1082

Answers (2)

Afnan Makhdoom
Afnan Makhdoom

Reputation: 654

You can just position a small button with a question mark on it in the corner of your main button, lets say the small question mark button is called butq , now use the code below:

butq.BringToFront

Upvotes: 0

KekuSemau
KekuSemau

Reputation: 6856

I think drawing that on top is a good idea and easily accomplished. The entire idea as such may be debatable, but I don't think it's necessarily bad.
Here is a quick example how the event flow can be set up.

Public Class SpecialButton
    Inherits Button

    Private flagIsHovering As Boolean = False

    Private ReadOnly Property r As Rectangle
        Get
            Return New Rectangle(Me.Width - 20, 5, 15, 15)
        End Get
    End Property

    Public ReadOnly Property clickInSpecialArea(p As Point) As Boolean
        Get
            Return (Me.r.Contains(p))
        End Get
    End Property

    Private iconBG_normal As Color = Color.White
    Private iconBG_hover As Color = Color.DarkOrange

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

        With pevent.Graphics
            .FillRectangle(New SolidBrush(If(Me.flagIsHovering, iconBG_hover, iconBG_normal)), r)
            .DrawRectangle(Pens.Blue, r)
            .DrawString("?", New Font("Verdana", 8), Brushes.Blue, r.Location)
        End With
    End Sub

    Protected Overrides Sub OnMouseMove(mevent As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseMove(mevent)

        Dim oldState As Boolean = Me.flagIsHovering
        Me.flagIsHovering = (r.Contains(mevent.Location))
        If oldState <> Me.flagIsHovering Then Me.Invalidate()
    End Sub

    Protected Overrides Sub OnMouseLeave(e As System.EventArgs)
        MyBase.OnMouseLeave(e)
        If Me.flagIsHovering Then
            Me.flagIsHovering = False
            Me.Invalidate()
        End If
    End Sub

End Class

Form test code:

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim ct As New SpecialButton
        ct.Text = "Text"
        ct.Location = New Point(200, 20)
        ct.Size = New Size(100, 30)
        Me.Controls.Add(ct)

        AddHandler ct.MouseClick, Sub(sender_ As Object, e_ As MouseEventArgs)
                                      MessageBox.Show(
                                          "Special click: " &
                                          DirectCast(sender_, SpecialButton).clickInSpecialArea(e_.Location).ToString)
                                  End Sub
    End Sub
End Class

Upvotes: 1

Related Questions