我目前正在開發 vb.net 中的 WinForms 應用程式,您可以在組合框中選擇不同的資料。這些組合框中的專案可以編輯或洗掉,所以這是我的問題:有沒有辦法為每個專案添加圖示,例如鉛筆和垃圾桶,以向用戶顯示“單擊此處編輯”或“單擊此處洗掉“?
在我的腦海中,它看起來像下面的圖片:

非常感謝 :)
uj5u.com熱心網友回復:
我通過從ComboBox.
Imports System.ComponentModel
Public Class ComboBoxEx
Inherits ComboBox
...
End Class
這個想法是使用DrawMode DrawMode.OwnerDrawFixedand 來完成代碼中的所有繪圖。這允許我們繪制代表可點擊按鈕的影像。我在專案中添加了兩個影像作為資源(My.Resources.pencil并且My.Resources.Trash_16x16,您的影像可能有不同的名稱)。
Const IconSize = 20
Dim stringFormat As StringFormat = New StringFormat() With {.LineAlignment = StringAlignment.Center}
Public Sub New()
DrawMode = DrawMode.OwnerDrawFixed
DropDownStyle = ComboBoxStyle.DropDownList
ItemHeight = 21
End Sub
Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs)
e.DrawBackground()
If e.Index >= 0 Then
Dim g As Graphics = e.Graphics
Dim brushColor = If(((e.State And DrawItemState.Selected) <> 0),
SystemColors.Highlight,
e.BackColor)
Using brush As Brush = New SolidBrush(brushColor)
g.FillRectangle(brush, e.Bounds)
End Using
Using textBrush As Brush = New SolidBrush(e.ForeColor)
g.DrawString(Items(e.Index).ToString(), e.Font, textBrush, e.Bounds, stringFormat)
End Using
' Skip the default item at index = 0 and the text box area (DrawItemState.ComboBoxEdit)
If e.Index > 0 And (e.State And DrawItemState.ComboBoxEdit) = 0 Then
Dim image = My.Resources.pencil
Dim point = New Point(
Width - 2 * IconSize (IconSize - image.Width) \ 2,
e.Bounds.Y (ItemHeight - image.Height) \ 2)
g.DrawImage(image, point)
image = My.Resources.Trash_16x16
point = New Point(
Width - IconSize (IconSize - image.Width) \ 2,
e.Bounds.Y (ItemHeight - image.Height) \ 2)
g.DrawImage(image, point)
End If
End If
e.DrawFocusRectangle()
End Sub
這是視覺部分。現在我們必須檢測下拉按鈕上的滑鼠點擊,并在點擊時引發事件。
Dim isDroppedDown As Boolean
Public Event Button1Clicked()
Public Event Button2Clicked()
Protected Overrides Sub OnDropDown(e As EventArgs)
isDroppedDown = True
MyBase.OnDropDown(e)
End Sub
Protected Overrides Sub OnDropDownClosed(e As EventArgs)
isDroppedDown = False
MyBase.OnDropDownClosed(e)
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
Const WM_COMMAND = &H111
If LicenseManager.UsageMode = LicenseUsageMode.Runtime And isDroppedDown And
m.Msg = WM_COMMAND And (CType(m.WParam, Int64) >> 16) = 1 Then
Dim button = ButtonClicked()
' If the user clicked a button (skipping default item)
If button <> 0 And SelectedIndex > 0 Then
m.Result = New IntPtr(1)
If button = 1 Then
RaiseEvent Button1Clicked()
Else
RaiseEvent Button2Clicked()
End If
Return
End If
End If
MyBase.WndProc(m)
End Sub
Private Function ButtonClicked() As Integer
Dim pos = PointToClient(MousePosition)
If pos.X > Size.Width - IconSize Then
Return 2
ElseIf pos.X > Size.Width - 2 * IconSize Then
Return 1
End If
Return 0
End Function
編譯您的專案后,這個新ComboBoxEx的出現在 winforms 工具箱中,您可以將其拖放到您的表單中。
然后您可以在表單中處理按鈕事件
Private Sub ComboBoxEx1_Button1Clicked() Handles ComboBoxEx1.Button1Clicked
Label1.Text = $"Pen clicked. Item = {ComboBoxEx1.SelectedItem.ToString()}"
End Sub
Private Sub ComboBoxEx1_Button2Clicked() Handles ComboBoxEx1.Button2Clicked
Label1.Text = $"Trash bin clicked. Item = {ComboBoxEx1.SelectedItem.ToString()}"
End Sub
您可能需要調整圖示大小、文本大小等以滿足您的需要。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/517681.html
