subclassing in vb

Posted on 2003-02-26
Medium Priority
Last Modified: 2010-04-07
i want to know about subclassing and their requirement?
Question by:mmalwan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 8023868
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!

Expert Comment

ID: 8024576
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.

Author Comment

ID: 8024831
thanks all
these are good knowlege but i want to know some theory about subclassing and why to use it

Accepted Solution

Julian_K earned 210 total points
ID: 8024976
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.


Author Comment

ID: 8025363
Good Code and good Explanation
Thanks for all of you


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question