2014-07-22 48 views
0

是否可以在另一个按钮的顶部绘制按钮(除了只有第二个按钮并将其相对于第一个按钮的左侧和顶部位置移动)?我试图让一个小的可选问号(按钮)出现在主按钮的角落,如果点击它将采取各种行动。VB.NET通过另一个按钮绘制按钮

否则,我只是在按钮顶部绘制问号并获得鼠标OnClick的相对位置,然后在HasLink属性为true并且鼠标位于特定区域时执行操作。

这些也将被动态创建。

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 
+2

是的,种的; SplitButton包含一个侧面部分,包含一个下拉菜单(不是另一个控件)的箭头。你可以做类似的事情,但**这很奇怪;按钮旁边的链接标签或类似标签稍微传统一点,也许不那么令人困惑 – Plutonix

+0

是的,SplitButton并不是我正在寻找的东西。我将继续使用鼠标定位并将图像绘制到按钮上来处理我最初的想法。谢谢(你的)信息。 – Capellan

+0

这是一个空间中2个功能性事物的例子*有相当数量的代码,因此代码知道用户点击了哪部分,像VisualStyles这样的东西按预期工作。 – Plutonix

回答

1

我认为绘制顶部是一个好主意,很容易实现。整个想法本身可能是有争议的,但我认为这不一定是坏事。
下面是一个如何设置事件流程的简单示例。

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 

表测试代码:

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 
+0

这是我所寻找的更多。它与我所拥有的非常相似,但是在MouseHover上的颜色更改显得更好。感谢代码。我做了一些更改,因为我将动态添加按钮。我添加了:受保护的覆盖Sub OnClick(e作为EventArgs)MyBase.OnClick(e)MessageBox.Show(clickInSpecialArea(Me.PointToClient(MousePosition)))End Sub并在窗体上加载Dim btn As SpecialButton = New SpecialButton btn.Text =“文本“btn.Size =新大小(200,100)Me.FlowLayoutPanel1.Controls.Add(btn) – Capellan

0

你可以只放置一个小按钮带有问号就可以在你的主键的一角,可以说小问号按钮被称为butq,现在使用下面的代码:

butq.BringToFront 
+0

这些按钮是动态绘制并放置在FlowPanel的控件集合中,所以我避免了必须完全创建第二个按钮并放置它在第一。 – Capellan

相关问题