VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Subclassing without using the AdressOf-Operator

by Stephan Kirchmaier (8 Submissions)
Category: Coding Standards
Compatability: Visual Basic 3.0
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (5 Votes)

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!)

API Declarations
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.!

Rate Subclassing without using the AdressOf-Operator

'*****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

Download this snippet    Add to My Saved Code

Subclassing without using the AdressOf-Operator Comments

No comments have been posted about Subclassing without using the AdressOf-Operator. Why not be the first to post a comment about Subclassing without using the AdressOf-Operator.

Post your comment

Subject:
Message:
0/1000 characters