Library tutorials & articles

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!

Comments

  1. 25 Feb 2004 at 20:18

    Pretty hacky functions... Try these:


    Code:
    Public Function GetLowWord(Word As Long) as Long
      GetLowWord = Word Mod 65536
    End Function


    Public Function GetHighWord(Word As Long)
       GetHighWord = (Word \ 65536) Mod 65536
    End Function



    Those will chop off the 1st/2nd and 3rd/4th bytes respectively using pure math. Much faster and more universal.

  2. 08 Aug 2003 at 22:28

    Hummmm.....  This code looks surprisingly like the code in the GETMINMAXINFO example at  http://www.mvps.org/vbvision/  Right down to the exact same comments!  Coincidence?  You be the judge!

  3. 07 Mar 2003 at 06:31
  4. 07 Mar 2003 at 05:42

    the link Download the SSubTmr project code (no DLL) (9kb) not working.

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of Subclassing.

Leave a comment

Sign in or Join us (it's free).