Subclassing

Introduction

If you have no experience on 'windows messages', I recommend that you read my article on sending messages, before you read this one.

Visual Basic is like a worried mother at times. It shields you from the rough and tumble of the real world. However, sometimes you want to get out there and can't. Each control has a selected number of events and properties. If you were programming in C++, there would be many more such events. When you move your mouse over a menu item, a message is sent to the VB framework. However, as there is no MouseOver event for menu items, VB it ignores it and does not pass it on to you. Subclassing is all about catching these messages using Windows API, before Visual Basic gets its hands on them and chucks most of them in the bin. Each message has a unique number, and most are listed as constants in the API Text viewer, and normally start with WM_ .

Please read the following warnings, but don't let them put you off, as Subclassing opens up a whole new world. Alternatively, you can use the SSUBTMR.DLL file, and not have to worry about them at all!

WARNING 1!
Please note before continuing that when you subclass messages, you have 'signed' an agreement with windows, so that windows will pass on all messages to you. In order to terminate this agreement, you need to un-subclass your program. This occurs in the Form_Unload event (when you press the X button). However, if you use the stop button on the toolbar in Visual Basic, , it will not call this procedure. Visual Basic, and your program will crash, and you will lose any unsaved work. 

It is advisable to always save your work before running your application. To avoid temptation, you can remove the stop icon from your VB toolbar using the Customize Menu command.

WARNING 2!
When you receive a warning saying that the changes you have made will reset your project, press Cancel. Pressing yes will end your program, and is the same as pressing the stop button (see above)

WARNING 3!
Do not enter any breakpoints into the WindowProc procedure, or try to debug it during run time, as Visual Basic will crash again!

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only 3 numbers of interest to a computer scientist: 1, 0 and infinity”