Click here to Skip to main content
15,890,438 members
Articles / Programming Languages / Visual Basic
Article

Subclassed System Menu

Rate me:
Please Sign up or sign in to vote.
4.45/5 (11 votes)
17 Feb 20042 min read 72.3K   718   35   8
Implementation of a subclassed system menu for non standard UIs.

Sample Image

Introduction

The reason for this article is to show a method of subclassing an application's system menu, so that extra menu items can be added and click events can be easily handled. This is my first article submitted to CodeProject and all suggestions, criticisms etc. are welcome.

Background

This all came about after working on an application that didn't have a standard UI, e.g. menus. I needed to show an About dialog for the application, but I didn't have the usual menus and there didn't seem a natural place from which to launch the dialog, so I created this. I have tried to make it as generic as possible so that it adds a menu separator, then a user defined menu item.

When the object is created, it will retrieve the handle for the parent window and append two new menu items, the first being a separator and the second being "About...". Once this has been completed, an overridden WndProc is used to receive the WM_SYSCOMMAND (&H112) message. If the message is invoked by the user selecting the "About..." menu item, the LaunchDialog event is raised.

Approach

The object inherits from System.Windows.Forms.NativeWindow to receive the window messages. Also the IDisposible interface is implemented, so we can release our reference to the window handle when the object is destroyed or goes out of scope.

Using the code

The implementation of the object is straight forward and uses a couple of Win32 API calls, e.g. GetSystemMenu and AppendMenu. The object uses standard VB.NET events to notify the parent form that the new system menu item has been clicked. There are many different ways that the event notifications can be implemented including using an interface. There are many other ways but this way does the job.

The code is very easy to use, you just need to declare a module level variable using WithEvents.

VB
Private WithEvents mobjSubclassedSystemMenu As SubclassedSystemMenu

Then instantiate the declared object, passing in two parameters, the first is the parent window handle and the second being the text for the new menu item.

VB
mobjSubclassedSystemMenu = New _ 
  SubclassedSystemMenu(Me.Handle.ToInt32, "&About...")

Next you need to implement the SubclassedSystemMenu event (LaunchDialog).

VB
Private Sub mobjSubclassedSystemMenu_LaunchDialog() _
                         Handles mobjSubclassedSystemMenu.LaunchDialog
    Dim frmNew As New frmAbout

    frmNew.ShowDialog(Me)
End Sub

And that's it really!

History

01-31-2004: Initial version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMark Woan Subclassed System Menu Demo app Pin
Member 135696505-Apr-20 5:09
Member 135696505-Apr-20 5:09 
GeneralI like Pin
Skylinc6-Aug-08 22:09
Skylinc6-Aug-08 22:09 
QuestionHow do I open up the file? Pin
augiesasso10-Apr-05 11:09
augiesasso10-Apr-05 11:09 
GeneralCode is good... Pin
robdogg116-Mar-04 8:34
robdogg116-Mar-04 8:34 
GeneralRe: Code is good... Pin
Mark Woan16-Mar-04 12:47
Mark Woan16-Mar-04 12:47 
GeneralRe: Code is good... and now better... Pin
Radgar9-Jun-04 15:38
Radgar9-Jun-04 15:38 
Mark, I found your code very useful... Doesn't matter if it has bugs or does less than I want; the point is you gave me the idea to achive what I want... Below, I modified the code and enabled adding more than 1 items... (I may have changed some variables and class name as well..)

Regards
Onur YILDIRIM

'--------------------- INSERT INTO FORM --------------------------

#Region "System Menu Custom Items"

Private WithEvents SysMenuAbout As SystemMenuSubClass
Private WithEvents SysMenuOptions As SystemMenuSubClass

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
SysMenuOptions = New SystemMenuSubClass(me.Handle.ToInt32, 1000, "&Options...")
SysMenuAbout = New SystemMenuSubClass(me.Handle.ToInt32, 1001, "&About...")
End Sub

Private Sub SysMenuOptions_LaunchDialog() Handles SysMenuOptions.LaunchDialog
'i.e.: frmOptions.Show()
End Sub

Private Sub SysMenuAbout_LaunchDialog() Handles SysMenuAbout.LaunchDialog
'i.e.: frmAbout.Show()
End Sub

#End Region


'--------------------- CLASS --------------------------

Public Class SystemMenuSubClass

Inherits System.Windows.Forms.NativeWindow
Implements IDisposable

#Region "Win32 API Declares"
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Int32, _
ByVal bRevert As Boolean) As Int32

Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Int32, _
ByVal wFlags As Int32, _
ByVal wIDNewItem As Int32, _
ByVal lpNewItem As String) As Int32
#End Region

#Region "Constants"
Private Const MF_STRING As Int32 = &H0 ' Menu string format
Private Const MF_SEPARATOR As Int32 = &H800 ' Menu separator
Private Const WM_SYSCOMMAND As Int32 = &H112 ' System menu
#End Region

#Region "Member Variables"
Private mintSystemMenu As Int32 = 0 ' Parent system menu handle
Private mintHandle As Int32 = 0 ' Local parent window handle
Private mstrMenuItemText As String = String.Empty ' New menu item text
Private mintItem As Int32 ' Our ID for the new menu item
#End Region

#Region "Events"
Public Event LaunchDialog()
#End Region

#Region "Constructor"
'========================================================
'
' Method Name: New
' Description: Constructor. Creates menu items and assigns subclass
'
' Inputs: intWindowHandle : Parent window handle for message
' subclass and adding new menu items
' to parent system menu
'
' Return Value: None
'
'========================================================
Public Sub New(ByVal intWindowHandle As Int32, ByVal itemID As Int32, ByVal strMenuItemText As String)

Me.AssignHandle(New IntPtr(intWindowHandle))

mintHandle = intWindowHandle
mstrMenuItemText = strMenuItemText
mintItem = itemID


' Retrieve the system menu handle
mintSystemMenu = GetSystemMenu(mintHandle, 0)

If AddNewSystemMenuItem() = False Then
Throw New Exception("Unable to add new system menu items")
End If

End Sub
#End Region

#Region "Methods"
'========================================================
'
' Method Name: WndProc
' Description: Subclassed window message delegate
'
' Inputs: m : Window Message
'
' Return Value: None
'
'========================================================
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

Select Case m.Msg
Case WM_SYSCOMMAND

MyBase.WndProc(m)

If m.WParam.ToInt32 = mintItem Then
If mintSystemMenu <> 0 Then
RaiseEvent LaunchDialog()
End If
End If

Case Else
MyBase.WndProc(m)
End Select

End Sub

'========================================================
'
' Method Name: Dispose
' Description: IDispose interface implementation
'
' Inputs: None
'
' Return Value: None
'
'========================================================
Public Sub Dispose() Implements System.IDisposable.Dispose

If Not Me.Handle.Equals(IntPtr.Zero) Then
Me.ReleaseHandle()
End If

End Sub

'========================================================
'
' Method Name: AddNewSystemMenuItem
' Description: Adds system menu items
'
' Inputs: None
'
' Return Value: True if successful, False else
'
'========================================================
Private Function AddNewSystemMenuItem() As Boolean
Try
' Append the extra system menu items
Return AppendToSystemMenu(mintSystemMenu, mstrMenuItemText)

Catch ex As Exception
Return False
End Try
End Function

'========================================================
'
' Method Name: AppendToSystemMenu
' Description: Adds system menu items (Separator & About...?)
'
' Inputs: intHandle : System Menu handle
' strText : Text for new menu item
'
' Return Value: True if successful, False else
'
'========================================================
Private Function AppendToSystemMenu(ByVal intHandle As Int32, ByVal strText As String) As Boolean

Try
' Add the seperator menu item
Dim intRet As Int32 = AppendMenu(intHandle, MF_SEPARATOR, 0, String.Empty)

' Add the About... menu item
intRet = AppendMenu(intHandle, MF_STRING, mintItem, strText)

If intRet = 1 Then
Return True
Else
Return False
End If

Catch ex As Exception
Return False
End Try
End Function
#End Region

End Class


Regards
Onur YILDIRIM
GeneralRe: Code is good... and now better... Pin
Mark Woan12-Jun-04 0:43
Mark Woan12-Jun-04 0:43 
GeneralRe: Code is good... Pin
achavez24-Feb-05 15:46
achavez24-Feb-05 15:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.