VB icon

Subclassing without using the AdressOf-Operator

Email
Submitted on: 1/7/2015 4:20:00 PM
By: Stephan Kirchmaier (from psc cd)  
Level: Advanced
User Rating: By 5 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages)
Views: 455
 
     This code simulates subclassing without the AdressOf-Operator. It's much safer than the "SetWindowLong-Method". The code shows a MessageBox when you click on the form (it's only a simple example!)
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Subclassing without using the AdressOf-Operator
'**************************************
see code, PS: You must press a button to end the programm and the app must start with Sub Main!
PPS: Vote 4 me , pls.!
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Subclassing without using the AdressOf-Operator
' Description:This code simulates subclassing without the AdressOf-Operator. It's much safer than the "SetWindowLong-Method". The code shows a MessageBox when you click on the form (it's only a simple example!)
' By: Stephan Kirchmaier (from psc cd)
'**************************************

'*****Form1*****'
Option Explicit
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
PostQuitMessage 0&
End Sub
'*****Module1*****'
Option Explicit
Public Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As msg, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Public Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As msg) As Long
Public Declare Function TranslateMessage Lib "user32" (lpMsg As msg) As Long
Public Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)
Public Type POINTAPI
x As Long
y As Long
End Type
Public Type msg
hwnd As Long
message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Public Const PM_REMOVE = &H1
Public Const WM_QUIT = &H12
Public Const WM_RBUTTONDOWN = &H204
Private Sub Main()
Dim tMsg As msg
Load Form1
Form1.Show
Do
If PeekMessage(tMsg, 0, 0, 0, PM_REMOVE) Then
If tMsg.message = WM_QUIT Then Exit Do
If tMsg.message = WM_RBUTTONDOWN Then
MsgBox "You clicked the right mousebutton!" & vbCr & "Press a key to end the app"
End If
TranslateMessage tMsg
DispatchMessage tMsg
Else
'There's nothing to do for your App!
'In a game you could draw a new frame,
'this is much faster than using the Timer!
End If
Loop Until False
Unload Form1
End Sub


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.