Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

subclassing in vb

i want to know about subclassing and their requirement?
  • 2
  • 2
1 Solution
not much requirement,
first you declare the following functions and constants:
const GWL_WNDPROC = -4

then you need a public .bas module for subclass code

'in a .bas module:
'OldProc restores the original procedure for the window,should be public
public OldProc as long
'the subclassing windowproc for you to handle
public function myWndProc(byval hWnd as long, byval uMsg as long, byval wParam as long, byval lParam as long)as long

'do your handling here

'after your handling,you need to call original windowproc to process the unhandled messages
End Function

in form code where you want to subclass(typically Form_Load), type in:
OldProc=SetWindowLong(me.hWnd,GWL_WNDPROC,addressof myWndProc)

and in form code where you want to unsubclass it(typically Form_Unload), type:
SetWindowLong me.hWnd, GWL_WNDPROC, OldProc

Good luck!
And after all that you pray not to have bugs, couse if the program is interrupted while in subclassing part, the VB environment says good-bye.
mmalwanAuthor Commented:
thanks all
these are good knowlege but i want to know some theory about subclassing and why to use it
All user controls, forms, etc. has a specific structure, which we call window. It is used by the system (Windows, etc.) to send them events. For example when a window needs to be redrawn, or when the mouse is clicked over a button - in these (and hell of a lot more) events, the system sends a message to the window which must do something regarding to the event. In most cases you don't have access to these messages. (In fact you have access to some of them - they are implemented as events in VB - like "Button_Click" or "Button_MouseMove" and so on)

Subclassing is process of intercepting these messages which the system sends, when you need to use them, but VB does not allows you to do so directly.
All these messages are sent to a function, which memory address is stored in the window structure. You replace this address with the address of your own function and recieve the events instead of the owner window. Don't forget to call the previous window function if you recieve events that are not of interest to you.

If you need more specifics, drop a comment.

mmalwanAuthor Commented:
Good Code and good Explanation
Thanks for all of you


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now