Add menu to the system menu

zhaozj2021-02-11  210

Add menu to the system menu

Private sub flow_load ()

OldWindowProc = GetWindowLong (Form1.hwnd, GWL_WNDPROC) 'to obtain the address of the window function Call SetWindowLong (Form1.hwnd, GWL_WNDPROC, AddressOf SubClass1_WndMessage)' instead of using a window function processing message SubClass1_WndMessage

SysmenuhWnd = getSystemMenu (form1.hwnd, false)

Call AppendMenu (SysMenuHwnd, MF_SEPARATOR, 2000, vbNullString) Call AppendMenu (SysMenuHwnd, MF_STRING, 2001, "About this program (& A)") Call AppendMenu (SysMenuHwnd, MF_SEPARATOR, 2002, vbNullString) Call AppendMenu (SysMenuHwnd, MF_STRING, 2003, " Restore System Menu (& R) ") End Sub

Private Sub Form_Unload (Cancel As Integer) IF OldwindowProc <> getWindowlong (Form1.hWnd, GWL_WndProc) THEN CALL SETWINDOWLONG (Form1.hwnd, GWL_WndProc, OldwindowProc) end ifend sub

Option expedition

'API function declaration Public Declare Function GetWindowLong Lib "user32" Alias ​​"GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As LongPublic Declare Function SetWindowLong Lib "user32" Alias ​​"SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long , ByVal dwNewLong As Long) As LongPublic 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

Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As LongPublic Declare Function AppendMenu Lib "user32" Alias ​​"AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As LongPublic Declare Function ShellExecute Lib "shell32.dll" Alias ​​"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long 'constant declaration public const wm_syscommand = & h112' Click the Control Box to generate this message PUBLIC Const mf_separator = & h800 & 'for menu plus a divided line public const mf_string = & h0 &' Add a string public const GWL_WndProc = (-4)

'Global Variable Public OldWindowProc As Long' Save Default Window Function Address Public SysmenuhWnd As Long 'Save System Menu Handle

Public Function SubClass1_WndMessage (ByVal hwnd As Long, ByVal Msg As Long, ByVal wp As Long, ByVal lp As Long) As Long If Msg <> WM_SYSCOMMAND Then SubClass1_WndMessage = CallWindowProc (OldWindowProc, hwnd, Msg, wp, lp) 'If the message is not WM_SYSCOMMAND, send the default window function to handle EXIT FUNCTION END IF

Select Case wp Case 2001 Call MsgBox ( "This procedure has the function to modify the system menu", vbOKOnly vbInformation) Case 2003 Call GetSystemMenu (Form1.hwnd, True) Call SetWindowLong (Form1.hwnd, GWL_WNDPROC, OldWindowProc) Call MsgBox ( " The default system menu has been resumed ", Vbokonly vbinformation) Case else subclass1_wndMessage = CallWindowProc (OldwindowProc, HWND, MSG, WP, LP) EXIT FUNCTION End SELECT

Subclass1_WndMessage = true

END FUNCTION

转载请注明原文地址:https://www.9cbs.com/read-4835.html

New Post(0)