Tipp 2.12 - Systemmenü ein/aus

Wie kann ich das Systemmenü ein- und ausschalten?

Mit dieser Funktion können Sie das Systemmenü sowie die Min-/Max-Schaltflächen während der Laufzeit in einem Formular ein- und ausschalten. Kopieren Sie einfach nachfolgenden Quellcode in die Zwischenablage und fügen Sie anschließend den Inhalt der Zwischenablage in ein neues Modul ein. Die Aufrufparameter finden Sie im Quellcode beschrieben.

Api-AufrufeVerwendete Win32-Api-Aufrufe und Typen: SetWindowLong, GetWindowLong, SendMessage

Beispiel:

' Api-Deklarationen
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 GetWindowLong _
                          Lib "user32" Alias "GetWindowLongA" _
                              (ByVal hwnd As Long, _
                               ByVal nIndex As Long) _
                               As Long
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
 
Const GWL_STYLE = (-16)
 
' Window Styles
Const WS_SYSMENU = &H80000
Const WS_MINIMIZEBOX = &H20000
Const WS_MAXIMIZEBOX = &H10000
 
' Windows Nachricht
Private Const WM_NCPAINT = &H85
 
Function A2XFrmSystems(pFrm As Form, _
                       ByVal pbShowSysMenu As Boolean, _
                       ByVal pbShowMax As Boolean, _
                       ByVal pbShowMin As Boolean) As Boolean
  '// =====================================================
  '// Methode   | Schaltet während der Laufzeit das System-
  '               menü sowie die Min/Max-Buttons ein/aus
  '// -----------------------------------------------------
  '// Parameter | pFrm          - gewünschtes Formobjekt
  '               pbShowSysMenu - Systemmenü Ja/Nein
  '               pbShowMax     - Maxbutton Ja/Nein
  '               pbShowMin     - Minbutton ja/Nein
  '// -----------------------------------------------------
  '// Rückgabe  | Boolean - True=Erfolgreich
  '// -----------------------------------------------------
  '// Erstellt  | Manuela Kulpa
  '//           | EDV Innovation & Consulting - Dormagen
  '// -----------------------------------------------------
  '// Beispielaufruf:
  '// ?A2XFrmSystems(Me,True,False,False)
  '// =====================================================
  Dim lHwnd As Long
   On Error GoTo A2XFrmSystems_Error
 
  lHwnd = pFrm.hwnd
  A2XHandleStyles lHwnd, _
                  pbShowSysMenu, _
                  pbShowMax, _
                  pbShowMin
 
  A2XFrmSystems = True
 
A2XFrmSystems_Exit:
   On Error GoTo 0
   Exit Function
 
A2XFrmSystems_Error:
  Select Case Err.Number
    Case Else
      MsgBox "Fehler " & Err.Number & ": " & _
             Err.Description, vbCritical, _
             "basControl.A2XFrmSystems"
  End Select
  Resume A2XFrmSystems_Exit
 
End Function
 
Private Function A2XHandleStyles(ByVal plHwnd As Long, _
                                 pbShowSysMenu As Boolean, _
                                 pbShowMax As Boolean, _
                                 pbShowMin As Boolean) As Long
  '// =====================================================
  '// Methode   | Hilfsfunktion mit API-Aufrufen
  '// -----------------------------------------------------
  '// Parameter | plHwnd        - Handling der Form
  '               pbShowSysMenu - Systemmenü Ja/Nein
  '               pbShowMax     - Maxbutton Ja/Nein
  '               pbShowMin     - Minbutton ja/Nein
  '// -----------------------------------------------------
  '// Rückgabe  | Long - Rückgabe des API-Aufrufes
  '// -----------------------------------------------------
  '// Erstellt  | Manuela Kulpa
  '//           | EDV Innovation & Consulting - Dormagen
  '// =====================================================
 
  Dim lStylesOn As Long
  Dim lStylesOff As Long
 
  On Error GoTo A2XHandleStyles_Error
 
  lStylesOn = 0
  lStylesOff = &HFFFFFFFF
 
  ' Hinterlege vorab die entsprechenden Attribute
  If pbShowSysMenu Then
    lStylesOn = lStylesOn Or WS_SYSMENU
  Else
    lStylesOff = lStylesOff And Not WS_SYSMENU
  End If
 
  If pbShowMin Then
    lStylesOn = lStylesOn Or WS_MINIMIZEBOX
  Else
    lStylesOff = lStylesOff And Not WS_MINIMIZEBOX
  End If
 
  If pbShowMax Then
    lStylesOn = lStylesOn Or WS_MAXIMIZEBOX
  Else
    lStylesOff = lStylesOff And Not WS_MAXIMIZEBOX
  End If
 
  ' Setze das Handling
  A2XHandleStyles = SetWindowLong(plHwnd, GWL_STYLE, _
                                  (GetWindowLong(plHwnd, _
                                                 GWL_STYLE) And _
                                                 lStylesOff) _
                                                 Or lStylesOn)
 
  ' Zeichne das Formular neu
  Call SendMessage(plHwnd, WM_NCPAINT, 1, 0)
 
A2XHandleStyles_Exit:
  On Error GoTo 0
  Exit Function
 
A2XHandleStyles_Error:
  Select Case Err.Number
    Case Else
      MsgBox "Fehler " & Err.Number & ": " & _
             Err.Description, vbCritical, _
             "basControl.A2XHandleStyles"
  End Select
  Resume A2XHandleStyles_Exit
 
End Function