Advertisement
2002VB Coding Standards #19153

Subclassing without using the AdressOf-Operator

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

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
'*****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
Original Comments (3)
Recovered from Wayback Machine