Rubrik: Controls · ComboBox | VB-Versionen: VB4, VB5, VB6 | 09.10.03 |
Bildsymbol im Eingabefelder der ComboBox anzeigen Dieser Tipp zeigt, wie sich ein Bildsymbol (Icon) im Eingabefeld einer ComboBox anzeigen lässt. | ||
Autor: Dieter Otter | Bewertung: | Views: 15.087 |
www.tools4vb.de | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
In einigen Dialogfenstern von Windows XP sieht man es immer wieder: eine ComboBox, bei der im Eingabefeld ganz links ein Bildsymbol (Icon) angezeigt wird, wie z.B. im Dialogfenster des Internet-Explorers, wenn eine geschützte Seite geladen werden soll (siehe nachfolgende Abbildung).
Heute zeigen wir Ihnen, wie man die Standard-ComboBox von Visual Basic um genau dieses Feature nachrüstet.
Erstellen Sie ein neues Projekt und platzieren auf die Form eine ComboBox (Combo1), sowie ein PictureBox-Control (Picture1). Fügen Sie nachfolgenden Code in den Codeteil der Form oder auch in ein Modul ein.
Option Explicit ' benötigte API-Deklarationen Private Declare Function SendMessage Lib "user32" _ Alias "SendMessageA" ( _ ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long Private Declare Function FindWindowEx Lib "user32" _ Alias "FindWindowExA" ( _ ByVal hwndParent As Long, _ ByVal hwndChildAfter As Long, _ ByVal lpszClass As String, _ ByVal lpszWindow As String) As Long Private Const EC_LEFTMARGIN = &H1 Private Const EM_SETMARGINS = &HD3
' Bild im Eingabefeld einer ComboBox anzeigen Public Sub ComboBox_AddPicture(oForm As Form, _ oComboBox As ComboBox, _ oPictureBox As PictureBox) Dim nHWnd As Long Dim nLeft As Long ' Handle des Eingabefeldes der Combo-TextBox ermitteln With oComboBox nHWnd = FindWindowEx(.hwnd, 0, "EDIT", vbNullString) If (nHWnd <> 0) Then ' PictureBox-Hintergrund dem Eingabefeld anpassen oPictureBox.BackColor = .BackColor ' PictureBox in das Eingabefeld platzieren oPictureBox.Move .Left + oForm.ScaleX(4, vbPixels, oForm.ScaleMode), _ .Top + oForm.ScaleY(3, vbPixels, oForm.ScaleMode), _ oForm.ScaleX(16, vbPixels, oForm.ScaleMode), _ .Height - oForm.ScaleY(4, vbPixels, oForm.ScaleMode) oPictureBox.ZOrder 0 ' linken Rand des Eingabefeldes der ComboBox neu festlegen nLeft = oForm.ScaleX(oPictureBox.Width, oForm.ScaleMode, vbPixels) + 3 Call SendMessage(nHWnd, EM_SETMARGINS, EC_LEFTMARGIN, nLeft) End If End With End Sub
Das Bildsymbol selbst (Icon) kann entweder zur Entwurfszeit festgelegt werden (Picture-Eigenschaft der PictureBox) oder zur Laufzeit zugeordnet werden, z.B. aus einer ImageList:
Picture1.Picture = ImageList1.ListImages(1).ExtractIcon
Damit das Alles jetzt auch schön aussieht müssen noch folgende Eigenschaften des PictureBox-Controls verändert werden:
AutoSize = True Appearance = 0 - 2D BorderStyle = 0 - None
Jetzt einfach nur noch die Prozedur ComboBox_AddPicture aufrufen...
ComboBox_AddPicture Form1, Combo1, Picture1