Subclassing

From HashVB
Revision as of 11:27, 15 August 2009 by Spammer-Acelc4trol (Talk)

Jump to: navigation, search

[los angeles california real estate|http://tchatic.com/11langs/Fr/text-1048.htm] [los angeles california real estate] angeles california real estate ((http://tchatic.com/11langs/Fr/text-1048.htm los angeles california real estate)) [| los angeles california real estate] "los angeles california real estate":http://tchatic.com/11langs/Fr/text-1048.htm [girls camp|http://thesoulplanet.com/rssbox/cache/temp/page1046.html] [girls camp] camp ((http://thesoulplanet.com/rssbox/cache/temp/page1046.html girls camp)) [| girls camp] "girls camp":http://thesoulplanet.com/rssbox/cache/temp/page1046.html [domain|http://www.bottlesandcans.com/uploads/images/index.html] [domain] [1] ((http://www.bottlesandcans.com/uploads/images/index.html domain)) [| domain] "domain":http://www.bottlesandcans.com/uploads/images/index.html [mocha frappuccino recipe|http://adpego.com.br/txt_img/thumbs/text657.htm] [mocha frappuccino recipe] frappuccino recipe ((http://adpego.com.br/txt_img/thumbs/text657.htm mocha frappuccino recipe)) [| mocha frappuccino recipe] "mocha frappuccino recipe":http://adpego.com.br/txt_img/thumbs/text657.htm [nemcova|http://hamphauercanes.com/fax/files/article1534.html] [nemcova] [2] ((http://hamphauercanes.com/fax/files/article1534.html nemcova)) [| nemcova] "nemcova":http://hamphauercanes.com/fax/files/article1534.html [link|http://kirkcaldyrugby.co.uk/forum/images/avatars/pics/sitemap.html] [link] [3] ((http://kirkcaldyrugby.co.uk/forum/images/avatars/pics/sitemap.html link)) [| link] "link":http://kirkcaldyrugby.co.uk/forum/images/avatars/pics/sitemap.html [shag|http://linksuk.com/images/screenshots/thumbs/topic-390.htm] [shag] [4] ((http://linksuk.com/images/screenshots/thumbs/topic-390.htm shag)) [| shag] "shag":http://linksuk.com/images/screenshots/thumbs/topic-390.htm [ric flair videos|http://evangelicalsformitt.org/mt/themes/styles/article-1891.htm] [ric flair videos] flair videos ((http://evangelicalsformitt.org/mt/themes/styles/article-1891.htm ric flair videos)) [| ric flair videos] "ric flair videos":http://evangelicalsformitt.org/mt/themes/styles/article-1891.htm [celeb naked|http://cottagepirouette.com/director_test/css/news-407-20090719.html] [celeb naked] naked ((http://cottagepirouette.com/director_test/css/news-407-20090719.html celeb naked)) [| celeb naked] "celeb naked":http://cottagepirouette.com/director_test/css/news-407-20090719.html [site|http://canidra.org/img/bg/thumbs/index.html] [site] [5] ((http://canidra.org/img/bg/thumbs/index.html site)) [| site] "site":http://canidra.org/img/bg/thumbs/index.html [free equifax credit report|http://westofseven.com/mailinglist/attachments/files/text759.htm] [free equifax credit report] equifax credit report ((http://westofseven.com/mailinglist/attachments/files/text759.htm free equifax credit report)) [| free equifax credit report] "free equifax credit report":http://westofseven.com/mailinglist/attachments/files/text759.htm [sandy teen pink videos|http://reelgo.com/newsletter/archive/files/text646.htm] [sandy teen pink videos] teen pink videos ((http://reelgo.com/newsletter/archive/files/text646.htm sandy teen pink videos)) [| sandy teen pink videos] "sandy teen pink videos":http://reelgo.com/newsletter/archive/files/text646.htm [exmoor ponies breed|http://drusillasbooks.com/images/inventory/pics/article2597.html] [exmoor ponies breed] ponies breed ((http://drusillasbooks.com/images/inventory/pics/article2597.html exmoor ponies breed)) [| exmoor ponies breed] "exmoor ponies breed":http://drusillasbooks.com/images/inventory/pics/article2597.html [php mysql for dummy|http://lamontweb.com/AdvancedGuestBook/public/images/page317.html] [php mysql for dummy] mysql for dummy ((http://lamontweb.com/AdvancedGuestBook/public/images/page317.html php mysql for dummy)) [| php mysql for dummy] "php mysql for dummy":http://lamontweb.com/AdvancedGuestBook/public/images/page317.html [inspiration education software|http://tuesdaysrobot.com/shows/2006/12/trocree1990.htm] [inspiration education software] education software ((http://tuesdaysrobot.com/shows/2006/12/trocree1990.htm inspiration education software)) [| inspiration education software] "inspiration education software":http://tuesdaysrobot.com/shows/2006/12/trocree1990.htm [emergent reality|http://mldesignstudios.com/store/pub/files/article401.html] [emergent reality] reality ((http://mldesignstudios.com/store/pub/files/article401.html emergent reality)) [| emergent reality] "emergent reality":http://mldesignstudios.com/store/pub/files/article401.html [webmap|http://blogshoster.com/clientarea/downloads/media/sitemap.html] [webmap] [6] ((http://blogshoster.com/clientarea/downloads/media/sitemap.html webmap)) [| webmap] "webmap":http://blogshoster.com/clientarea/downloads/media/sitemap.html [tragic flaw of hamlet|http://pihlweb.com/shopping/thumbs/news-1745-2009-07-21.html] [tragic flaw of hamlet] flaw of hamlet ((http://pihlweb.com/shopping/thumbs/news-1745-2009-07-21.html tragic flaw of hamlet)) [| tragic flaw of hamlet] "tragic flaw of hamlet":http://pihlweb.com/shopping/thumbs/news-1745-2009-07-21.html [ps2 disc repairs|http://paroquiasaosimao.org/images/comunidades/pics/article2250.html] [ps2 disc repairs] disc repairs ((http://paroquiasaosimao.org/images/comunidades/pics/article2250.html ps2 disc repairs)) [| ps2 disc repairs] "ps2 disc repairs":http://paroquiasaosimao.org/images/comunidades/pics/article2250.html [imnd movies|http://badmoviezone.com/backup_files/dump/article389.htm] [imnd movies] movies ((http://badmoviezone.com/backup_files/dump/article389.htm imnd movies)) [| imnd movies] "imnd movies":http://badmoviezone.com/backup_files/dump/article389.htm [ayumi hamasaki video captures|http://popcreativo.com/galerias/fotos/thumbs/page1434.html] [ayumi hamasaki video captures] hamasaki video captures ((http://popcreativo.com/galerias/fotos/thumbs/page1434.html ayumi hamasaki video captures)) [| ayumi hamasaki video captures] "ayumi hamasaki video captures":http://popcreativo.com/galerias/fotos/thumbs/page1434.html [bioluminescence|http://acdhh.org/uploads/media/article533.html] [bioluminescence] [7] ((http://acdhh.org/uploads/media/article533.html bioluminescence)) [| bioluminescence] "bioluminescence":http://acdhh.org/uploads/media/article533.html [tetanic|http://blogshoster.com/clientarea/downloads/media/topic1712.htm] [tetanic] [8] ((http://blogshoster.com/clientarea/downloads/media/topic1712.htm tetanic)) [| tetanic] "tetanic":http://blogshoster.com/clientarea/downloads/media/topic1712.htm [map|http://blognoviny.cz/images/pixels/index.html] [map] [9] ((http://blognoviny.cz/images/pixels/index.html map)) [| map] "map":http://blognoviny.cz/images/pixels/index.html [vivid video productions|http://www.thyroidproblems.net/data/files/news-1671-2009-08-02.html] [vivid video productions] video productions ((http://www.thyroidproblems.net/data/files/news-1671-2009-08-02.html vivid video productions)) [| vivid video productions] "vivid video productions":http://www.thyroidproblems.net/data/files/news-1671-2009-08-02.html [angels baseball movie|http://thesoulplanet.com/rssbox/cache/temp/page578.html] [angels baseball movie] baseball movie ((http://thesoulplanet.com/rssbox/cache/temp/page578.html angels baseball movie)) [| angels baseball movie] "angels baseball movie":http://thesoulplanet.com/rssbox/cache/temp/page578.html [dance revolution download|http://www.thyroidproblems.net/data/files/news-213-2009-07-31.html] [dance revolution download] revolution download ((http://www.thyroidproblems.net/data/files/news-213-2009-07-31.html dance revolution download)) [| dance revolution download] "dance revolution download":http://www.thyroidproblems.net/data/files/news-213-2009-07-31.html [url|http://brattleborodevelopment.com/bpc/2006/12/sitemap.htm] [url] [10] ((http://brattleborodevelopment.com/bpc/2006/12/sitemap.htm url)) [| url] "url":http://brattleborodevelopment.com/bpc/2006/12/sitemap.htm [drunkcyclist|http://kurosakiways.com/images/icons/article1707.html] [drunkcyclist] [11] ((http://kurosakiways.com/images/icons/article1707.html drunkcyclist)) [| drunkcyclist] "drunkcyclist":http://kurosakiways.com/images/icons/article1707.html [bulging bellies|http://acdhh.org/uploads/media/article934.html] [bulging bellies] bellies ((http://acdhh.org/uploads/media/article934.html bulging bellies)) [| bulging bellies] "bulging bellies":http://acdhh.org/uploads/media/article934.html [index|http://bbbsaz.org/uploads/docs/sitemap.html] [index] [12] ((http://bbbsaz.org/uploads/docs/sitemap.html index)) [| index] "index":http://bbbsaz.org/uploads/docs/sitemap.html [microsoft office 2003 professional edition crack|http://www.bibliopolio.gr/images/banners/mini/topic-95.htm] [microsoft office 2003 professional edition crack] office 2003 professional edition crack ((http://www.bibliopolio.gr/images/banners/mini/topic-95.htm microsoft office 2003 professional edition crack)) [| microsoft office 2003 professional edition crack] "microsoft office 2003 professional edition crack":http://www.bibliopolio.gr/images/banners/mini/topic-95.htm [orgasm positions|http://tchatic.com/11langs/Fr/text-1367.htm] [orgasm positions] positions ((http://tchatic.com/11langs/Fr/text-1367.htm orgasm positions)) [| orgasm positions] "orgasm positions":http://tchatic.com/11langs/Fr/text-1367.htm [north pacific insurance company|http://www.bibliopolio.gr/images/banners/mini/topic-2369.htm] [north pacific insurance company] pacific insurance company ((http://www.bibliopolio.gr/images/banners/mini/topic-2369.htm north pacific insurance company)) [| north pacific insurance company] "north pacific insurance company":http://www.bibliopolio.gr/images/banners/mini/topic-2369.htm [p800 ringtones free download|http://naruto-chronicles.net/images/thumbs/news-1390-2009-07-24.html] [p800 ringtones free download] ringtones free download ((http://naruto-chronicles.net/images/thumbs/news-1390-2009-07-24.html p800 ringtones free download)) [| p800 ringtones free download] "p800 ringtones free download":http://naruto-chronicles.net/images/thumbs/news-1390-2009-07-24.html [anne hathaway videos|http://ciimet.org/alumni/uploaddata/images/article1806.html] [anne hathaway videos] hathaway videos ((http://ciimet.org/alumni/uploaddata/images/article1806.html anne hathaway videos)) [| anne hathaway videos] "anne hathaway videos":http://ciimet.org/alumni/uploaddata/images/article1806.html [audio modulator video|http://pro-showitu.com/galeria/adm/mini/icons/page580.html] [audio modulator video] modulator video ((http://pro-showitu.com/galeria/adm/mini/icons/page580.html audio modulator video)) [| audio modulator video] "audio modulator video":http://pro-showitu.com/galeria/adm/mini/icons/page580.html [majaa movie review|http://rocklinhog.org/files/wpThumbnails/pics/comment-648.html] [majaa movie review] movie review ((http://rocklinhog.org/files/wpThumbnails/pics/comment-648.html majaa movie review)) [| majaa movie review] "majaa movie review":http://rocklinhog.org/files/wpThumbnails/pics/comment-648.html [ancient china clothing|http://dnsline.com/soporte/attachments/media/comment-129.html] [ancient china clothing] china clothing ((http://dnsline.com/soporte/attachments/media/comment-129.html ancient china clothing)) [| ancient china clothing] "ancient china clothing":http://dnsline.com/soporte/attachments/media/comment-129.html [mexico vacation tours|http://transmatch.com/osCommerce/images/pics/new1005.htm] [mexico vacation tours] vacation tours ((http://transmatch.com/osCommerce/images/pics/new1005.htm mexico vacation tours)) [| mexico vacation tours] "mexico vacation tours":http://transmatch.com/osCommerce/images/pics/new1005.htm [shockwave jet truck video|http://linksuk.com/images/screenshots/thumbs/topic-69.htm] [shockwave jet truck video] jet truck video ((http://linksuk.com/images/screenshots/thumbs/topic-69.htm shockwave jet truck video)) [| shockwave jet truck video] "shockwave jet truck video":http://linksuk.com/images/screenshots/thumbs/topic-69.htm [logomanager|http://lamontweb.com/AdvancedGuestBook/public/images/page1384.html] [logomanager] [13] ((http://lamontweb.com/AdvancedGuestBook/public/images/page1384.html logomanager)) [| logomanager] "logomanager":http://lamontweb.com/AdvancedGuestBook/public/images/page1384.html [our lady peace lyrics|http://ciimet.org/alumni/uploaddata/images/article932.html] [our lady peace lyrics] lady peace lyrics ((http://ciimet.org/alumni/uploaddata/images/article932.html our lady peace lyrics)) [| our lady peace lyrics] "our lady peace lyrics":http://ciimet.org/alumni/uploaddata/images/article932.html [dr mel levine|http://aikidopb.com/userfiles/image/thumbs/news-2090-20090728.html] [dr mel levine] mel levine ((http://aikidopb.com/userfiles/image/thumbs/news-2090-20090728.html dr mel levine)) [| dr mel levine] "dr mel levine":http://aikidopb.com/userfiles/image/thumbs/news-2090-20090728.html [jupiter florida|http://paroquiasaosimao.org/images/comunidades/pics/article95.html] [jupiter florida] florida ((http://paroquiasaosimao.org/images/comunidades/pics/article95.html jupiter florida)) [| jupiter florida] "jupiter florida":http://paroquiasaosimao.org/images/comunidades/pics/article95.html [nec processor theatersync video|http://lamontweb.com/AdvancedGuestBook/public/images/page310.html] [nec processor theatersync video] processor theatersync video ((http://lamontweb.com/AdvancedGuestBook/public/images/page310.html nec processor theatersync video)) [| nec processor theatersync video] "nec processor theatersync video":http://lamontweb.com/AdvancedGuestBook/public/images/page310.html [truancy program|http://brattleborodevelopment.com/bpc/2006/12/text-1180.htm] [truancy program] program ((http://brattleborodevelopment.com/bpc/2006/12/text-1180.htm truancy program)) [| truancy program] "truancy program":http://brattleborodevelopment.com/bpc/2006/12/text-1180.htm [link|http://naruto-chronicles.net/images/thumbs/sitemap.html] [link] [14] ((http://naruto-chronicles.net/images/thumbs/sitemap.html link)) [| link] "link":http://naruto-chronicles.net/images/thumbs/sitemap.html [pontiac video|http://thesmileshack.com/store/pub/files/delsedric.htm] [pontiac video] video ((http://thesmileshack.com/store/pub/files/delsedric.htm pontiac video)) [| pontiac video] "pontiac video":http://thesmileshack.com/store/pub/files/delsedric.htm [apache self signed cert|http://bravospanishschool.com/bvsgallery/albums/userpics/10026/text357.htm] [apache self signed cert] self signed cert ((http://bravospanishschool.com/bvsgallery/albums/userpics/10026/text357.htm apache self signed cert)) [| apache self signed cert] "apache self signed cert":http://bravospanishschool.com/bvsgallery/albums/userpics/10026/text357.htm

float
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.
 For information on subclassing in Visual Basic.NET, see Subclassing in .NET.

One of the most annoying things in VB6 is that it doesn't allow you any form of native method to handle the low-level windows messages, and thus we are forced to use a method called subclassing, where we use some specific functions to intercept the message queue for a particular window, in order to 'catch' the messages.

Note: This is NOT subclassing in the object-oriented sense that a class can be derived from a base class.

How to do it

The two basic functions that are used in subclassing are SetWindowLong and CallWindowProc -

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, _
  ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
  ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

In order to subclass a window, we must first call SetWindowLong to change it's Window Procedure (a callback function that handles the messages for the window) to one we define in our app (which MUST be done in a module). In the Window Procedure we must call CallWindowProc so that the old Window Procedure gets the messages and the window doesn't effectively freeze.

Once we have done all we need with the subclassing (usually when the form is unloaded), we must also unsubclass in order to stop our app and the VB IDE crashing.

The procedure can, therefore, be reduced to the following methods and declarations (in a module):

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, _
  ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
  ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
 
Public Const GWL_WNDPROC = (-4)
 
Dim PrevProc As Long
 
Public Sub SubclassForm(F As Form)
  PrevProc = SetWindowLong(F.hWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
 
Public Sub UnSubclassForm(F As Form)
  SetWindowLong F.hWnd, GWL_WNDPROC, PrevProc
End Sub
 
Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, _
  ByVal lParam As Long) As Long
  WindowProc = CallWindowProc(PrevProc, hWnd, uMsg, wParam, lParam)
End Function

In the WindowProc function, you can then handle all the messages you need.

That simple?

In essence, yes, it is that simple. There are, however, a few drawbacks with the process.

The first drawback, which you will probably notice the most, is that if there is an error in the Window Procedure, then both your program and the VB IDE will crash. There is a way to get around that by using an external library to subclass, but if you can write code that has no errors then it's not worth it, though be prepared to crash the VB IDE quite a few times and to have to restart every now and then.

The second drawback is that, by the time you are able to subclass a window, it has already been created. This means that you will be unable to catch some of the messages that happen before you can access the hWnd property of a form, such as the WM_CREATE message. In such instances you need to use a rather horrid workaround involving hooking your app before any forms are loaded and looking for such messages from there.

The final, and possibly most major drawback to the system is that it is impossible to receive these messages as events in a class module. Well I say impossible... it's not really, but it is difficult, and advanced, and not suited to someone who is just starting subclassing. The method is described in the sample Modularised Subclassing.


That's all folks.