Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


In this article you will learn how to modify system menu (control menu). You will also learn how to handle event of newly added menu item.

To modify system menu first you have to call GetSystemMenu api to get handle of system menu and then you can all other menu APIs (i.e. GetMenuItemCount, AppendMenu, RemoveMenu, ModifyMenu etc.)

Step-By-Step Example

- Create a standard exe project
- Add a module to the project
- Place 3 commandbuttons on the form1
- Add the following code to form1 code window

Form1.frm

Click here to copy the following block
Private Sub Command1_Click()
  '// add about menu
  lhSysMenu = GetSystemMenu(hwnd, 0&)
  lRet = AppendMenu(lhSysMenu, MF_SEPARATOR, 0&, vbNullString)
  lRet = AppendMenu(lhSysMenu, MF_STRING, IDM_ABOUT, "About...")
End Sub

Private Sub Command2_Click()
  RemoveSystemMenu Me.hwnd
End Sub

Sub RemoveSystemMenu(hwnd As Long)
  On Error Resume Next
  Dim hMenu As Long
  Dim itemCount As Long

  ' get the handle of the system menu
  hMenu = GetSystemMenu(hwnd, 0)


  '//MF_BYPOSITION : Indicates that uPosition gives the zero-based
  '//        relative position of the menu item.

  '//get the number of items in the menu
  'itemCount = GetMenuItemCount(hMenu)

  'RemoveMenu hMenu, itemCount - 1, MF_BYPOSITION 'remove Close
  'RemoveMenu hMenu, itemCount - 2, MF_BYPOSITION 'remove separator line
  'RemoveMenu hMenu, itemCount - 3, MF_BYPOSITION 'remove maximize
  'RemoveMenu hMenu, itemCount - 4, MF_BYPOSITION 'remove minimize
  'RemoveMenu hMenu, itemCount - 5, MF_BYPOSITION 'remove size
  'RemoveMenu hMenu, itemCount - 6, MF_BYPOSITION 'remove the move

  '//MF_BYCOMMAND : Indicates that uPosition gives the identifier of the menu item.

  RemoveMenu hMenu, SC_NEXTWINDOW, MF_BYCOMMAND
  RemoveMenu hMenu, SC_MOVE, MF_BYCOMMAND  'remove the move
  RemoveMenu hMenu, SC_RESTORE, MF_BYCOMMAND  'remove Close
  RemoveMenu hMenu, SC_CLOSE, MF_BYCOMMAND  'remove Close
  RemoveMenu hMenu, SC_SEPARATOR, MF_BYCOMMAND  'remove separator line
  RemoveMenu hMenu, SC_MAXIMIZE, MF_BYCOMMAND  'remove maximize
  RemoveMenu hMenu, SC_MINIMIZE, MF_BYCOMMAND  'remove minimize
  RemoveMenu hMenu, SC_SIZE, MF_BYCOMMAND  'remove size
End Sub

Private Sub Command3_Click()
  Unload Me
End Sub

'// form_load event. Catch all those messages!
Private Sub Form_Load()
  Dim lhSysMenu As Long, lRet As Long
  On Error Resume Next

  '// saves the previous window message handler. Always restore this value
  '// AddressOf command sends the address of the WindowProc procedure
  '// to windows
  ProcOld = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub

'// form_queryunload event. Return control to windows/vb
Private Sub Form_Unload(Cancel As Integer)
  '// give message processing control back to VB
  '// if you don't do this you WILL crash!!!
  Call SetWindowLong(hwnd, GWL_WNDPROC, ProcOld)
End Sub

- Add the following code to module1

Module1.bas

Click here to copy the following block
'// variable that stores the previous message handler
Public ProcOld As Long

'// Menu item count
Public Declare Function GetMenuItemCount Lib "user32" ( _
    ByVal hMenu As Long) As Long

'// Windows API Call for catching messages
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( _
    ByVal hwnd As Long, _
    ByVal nIndex As Long, _
    ByVal dwNewLong As Long) As Long

'// Windows API to retrieve information about the specified window.
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
    ByVal hwnd As Long, _
    ByVal nIndex As Long) As Long

'// Windows API call for calling window procedures
Public 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

'// menu windows api
Public Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" ( _
    ByVal hMenu As Long, _
    ByVal wFlags As Long, _
    ByVal wIDNewItem As Long, _
    ByVal lpNewItem As String) As Long

Public Declare Function GetSystemMenu Lib "user32" ( _
    ByVal hwnd As Long, _
    ByVal bRevert As Long) As Long

Public Declare Function RemoveMenu Lib "user32.dll" ( _
    ByVal hMenu As Long, _
    ByVal nPosition As Long, _
    ByVal wFlags As Long) As Long

'//DeleteMenu is same as RemoveMenu
Public Declare Function DeleteMenu Lib "user32.dll" ( _
    ByVal hMenu As Long, _
    ByVal nPosition As Long, _
    ByVal wFlags As Long) As Long

'// windows api constants
Public Const WM_SYSCOMMAND = &H112
Public Const GWL_WNDPROC = (-4)
Public Const IDM_ABOUT As Long = 1010

Public Const WS_MAXIMIZE = &H1000000
Public Const WS_MAXIMIZEBOX = &H10000
Public Const WS_MINIMIZE = &H20000000
Public Const WS_MINIMIZEBOX = &H20000

Public Const MF_SEPARATOR = &H800&
Public Const MF_STRING = &H0&

Public Const MF_BYCOMMAND = &H0&
Public Const MF_BYPOSITION = &H400&

Public Const MF_ENABLED = &H0
Public Const MF_GRAYED = &H1
Public Const MF_DISABLED = &H2

'//Positions of different menu items in system menu
'//Used with MF_BYCOMMAND flag

Public Const SC_ARRANGE = &HF110
Public Const SC_CLOSE = &HF060
Public Const SC_HOTKEY = &HF150
Public Const SC_HSCROLL = &HF080
Public Const SC_KEYMENU = &HF100
Public Const SC_MAXIMIZE = &HF030
Public Const SC_MINIMIZE = &HF020
Public Const SC_MOVE = &HF010
Public Const SC_NEXTWINDOW = &HF040
Public Const SC_PREVWINDOW = &HF050
Public Const SC_RESTORE = &HF120
Public Const SC_SIZE = &HF000

Public Function WindowProc(ByVal hwnd As Long, ByVal iMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
  '// ----WARNING----
  '// do not attempt to debug this procedure!!
  '// ----WARNING----

  '// this is our implementation of the message handling routine
  '// determine which message was recieved
  Select Case iMsg
    Case WM_SYSCOMMAND
      If wParam = IDM_ABOUT Then
        MsgBox "VB Web Append to System Menu Example", vbInformation, "About"
        Exit Function
      End If
  End Select
  '// pass all messages on to VB and then return the value to windows
  WindowProc = CallWindowProc(ProcOld, hwnd, iMsg, wParam, lParam)
End Function



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.