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.
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
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
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.
The object inherits from
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.
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
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
mobjSubclassedSystemMenu = New _
Next you need to implement the
Private Sub mobjSubclassedSystemMenu_LaunchDialog() _
Dim frmNew As New frmAbout
And that's it really!
01-31-2004: Initial version.