Let's say I wanna create a window using CreateWindowEx and not using VB forms.
How do I handle the events of that window?
Should I SetWindowLong to a handling procedure?
Printable View
Let's say I wanna create a window using CreateWindowEx and not using VB forms.
How do I handle the events of that window?
Should I SetWindowLong to a handling procedure?
CreateWindowEx returns the hWnd of the created window.
To handle the events you have to subclass the window using that hWnd.
That means you have to write an enormous Select Case statement.
I'm sorry but I have to ask; Why the heck do you want to do this? I can understand using the CreateWindowEx to create ActiveX controls but not for Forms.
I didn't think it would be so complicated. :rolleyes:
I just wanted to play with the API a little, nothing special.
I guess letting other people do the work for you is the easiest way :p
Besides, the Select Case won't be enormous, it'll only contain the messages I wanna handle.
You need to subclass the WM_COMMAND event to add events. (The lParam parameter will specify the hWnd of the child window)
Code for a Module
Code for a FormCode:Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong 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
Const GWL_WNDPROC = (-4)
Const WM_COMMAND = &H111
Public Retval As Long
Global WndProcOld As Long
Public Function WindProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If wMsg = WM_COMMAND Then
If lParam = Retval Then
MsgBox ("You pressed button 1")
End If
End If
WindProc = CallWindowProc(WndProcOld&, hwnd&, wMsg&, wParam&, lParam&)
End Function
Sub SubClassWnd(hwnd As Long)
WndProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindProc)
End Sub
Sub UnSubclassWnd(hwnd As Long)
SetWindowLong hwnd, GWL_WNDPROC, WndProcOld&
WndProcOld& = 0
End Sub
Code:Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle _
As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle _
As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight _
As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const WS_CHILD = &H40000000
Private Sub Form_Load()
Retval = CreateWindowEx(0&, "Button", "Button1", WS_CHILD, 32, 32, 64, 64, Me.hwnd, 0, App.hInstance, ByVal 0&)
ShowWindow Retval, 1
SubClassWnd hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnSubclassWnd hwnd
End Sub
What kind of message is WM_COMMAND?
When is it sent to a window?
WM_COMMAND is a non-standard message.
It is sent to a window when an event occurs on one of the controls on that window.
For example, when you click a button, the window receives WM_COMMAND with HiWord(wParam) = BN_CLICKED and lParam = the hWnd of the button.
MSDN says... :rolleyes:
Quote:
WM_COMMAND
The WM_COMMAND message is sent when the user selects a command item from a menu, when a control sends a notification message to its parent window, or when an accelerator keystroke is translated.
WM_COMMAND
wNotifyCode = HIWORD(wParam); // notification code
wID = LOWORD(wParam); // item, control, or accelerator identifier
hwndCtl = (HWND) lParam; // handle of control
Parameters
wNotifyCode
Value of the high-order word of wParam. Specifies the notification code if the message is from a control. If the message is from an accelerator, this parameter is 1. If the message is from a menu, this parameter is 0.
wID
Value of the low-order word of wParam. Specifies the identifier of the menu item, control, or accelerator.
hwndCtl
Value of lParam. Identifies the control sending the message if the message is from a control. Otherwise, this parameter is NULL.
Return Values
If an application processes this message, it should return zero.
Remarks
Accelerator keystrokes that select items from the window menu are translated into WM_SYSCOMMAND messages.
If an accelerator keystroke occurs that corresponds to a menu item when the window that owns the menu is minimized, no WM_COMMAND message is sent. However, if an accelerator keystroke occurs that does not match any of the items in the window’s menu or in the window menu, a WM_COMMAND message is sent, even if the window is minimized.
If an application enables a menu separator, the system sends a WM_COMMAND message with the low-word of the wParam parameter set to zero when the user selects the separator.
See Also
WM_SYSCOMMAND