Rubrik: Oberfläche · Menüs | VB-Versionen: VB5, VB6 | 25.01.05 |
System-Menü eines Forms erweitern II Hier erfahren Sie, wie sich das System-Menü einer Form um weitere Menüeinträge erweitern lässt | ||
Autor: Stefan Mähr | Bewertung: | Views: 11.012 |
www.visualsoft-net.de | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Dieser Tipp zeigt Ihnen, wie Sie das Systemmenü eines Formulares beliebig erweitern können und auf das Click-Ereignis des neuen Eintrages reagieren.
Kopieren Sie den folgenden Code in ein Modul:
Option Explicit ' API-Funktionen für das Erstellen der Menüeinträge Private Declare Function GetSystemMenu Lib "user32" ( _ ByVal hwnd As Long, _ ByVal bRevert As Long) As Long Private Declare Function InsertMenu Lib "user32" _ Alias "InsertMenuA" ( _ ByVal hMenu As Long, _ ByVal nPosition As Long, _ ByVal wFlags As Long, _ ByVal wIDNewItem As Long, _ ByVal lpNewItem As Any) As Long ' API-funktionen für das Subclassing Private Declare Function SetWindowLong Lib "user32" _ Alias "SetWindowLongA" ( _ ByVal hwnd As Long, _ ByVal nIndex As Long, _ ByVal dwNewLong As Long) As Long Private Declare Function CallWindowProc Lib "user32" _ Alias "CallWindowProcA" ( _ ByVal lpPrevWndFunc As Long, _ ByVal hwnd As Long, _ ByVal Msg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long Private Const MF_SEPARATOR = &H800& Private Const MF_BYPOSITION = &H400& Private Const GWL_WNDPROC = (-4) Private Const WM_SYSCOMMAND = &H112 Private lngPrevProc As Long Private intItemID As Integer Public Enum MenuInsertType Separator = 0 MenuByPosition = 1 End Enum
' neuen Menü-Eintrag hinzufügen Public Function CreateMenuEntry(ByVal strName As String, _ ByVal lngFormHwnd As Long, _ ByVal intMenuPosition As Integer, _ ByVal mnuType As MenuInsertType) As Boolean Dim lngMnuHandle As Long Dim lngRetValue As Long Dim intFlag As Integer On Error GoTo errHandler ' Handle des SystemMenu lngMnuHandle = GetSystemMenu(lngFormHwnd, False) ' Eintragtyp aus Enum ermitteln Select Case mnuType Case MenuInsertType.MenuByPosition intFlag = MF_BYPOSITION Case MenuInsertType.Separator intFlag = MF_SEPARATOR End Select If lngMnuHandle Then ' Menüeintrag erstellen lngRetValue = InsertMenu(lngMnuHandle, intMenuPosition, _ intFlag, intItemID, strName) End If CreateMenuEntry = True Exit Function errHandler: CreateMenuEntry = False Err.Clear End Function
' einen Hook auf das Form erstellen Public Sub CreateHook(ByVal lngHwnd As Long) lngPrevProc = SetWindowLong(lngHwnd, GWL_WNDPROC, AddressOf WindowProc) End Sub
' WICHTIG: den Hook wieder freigeben Public Sub ReleaseHook(ByVal lngHwnd As Long) SetWindowLong lngHwnd, GWL_WNDPROC, lngPrevProc End Sub
' Subclassing Public Function WindowProc(ByVal hwnd As Long, _ ByVal uMsg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long WindowProc = CallWindowProc(lngPrevProc, hwnd, uMsg, wParam, lParam) If uMsg = WM_SYSCOMMAND Then If wParam = intItemID Then MsgBox "Neuer Menüeintrag Click ... " End If End If End Function
Um nun unser neues Modul zu testen, müssen Sie ein neues Formular erstellen und folgenden Code einfügen:
Private Sub Form_Load() ' Menü-Eintrag hinzufügen If CreateMenuEntry("Testeintrag", Me.hwnd, 5, MenuByPosition) = True Then Call MsgBox("Menüeintrag erfolgreich erstellt!") End If Call CreateHook(Me.hwnd) End Sub
Private Sub Form_Unload(Cancel As Integer) ' WICHTIG! Korrrektes Beenden des Subclassing! Call ReleaseHook(Me.hwnd) End Sub