I am taking a control and, via the SetParent() function, I am placing it on the desktop. Now, how can I assign an event to that control (once it moves, its no longer responds to events)?
Printable View
I am taking a control and, via the SetParent() function, I am placing it on the desktop. Now, how can I assign an event to that control (once it moves, its no longer responds to events)?
You mean block/filter out events??
Can I see the code you are using to put the control onto the desktop? I just tried putting a textbox onto the desktop and the events still work fine.
i think i wanna use SetWindowLong, but im unsure of the correct params.Code:Call SetParent (Command1.hWnd, GetDesktopWindow)
You don't want to use SetWindowLong to set the window's parent. SetParent is the right API to use.
Try this:
SetParent Command1.hwnd, FindWindow("Progman", vbNullString)
This is the declare for the FindWindow API:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
The only problem is it may not work right when someone has some alternate shell installed..
No, u dont understand. I realize that i should use setparent. My concern was whether I would need to use SetWindowLong to subclass the command button's event with an event in my project.
Add to a Module
Add to a Form with 2 CommandButtons.Code: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)
Private Const WM_LBUTTONUP = &H202
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_LBUTTONUP Then MsgBox "PresseD"
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 GetDesktopWindow Lib "user32" () As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Sub Command1_Click()
SetParent Command2.hwnd, GetDesktopWindow
End Sub
Private Sub Form_Load()
SubClassWnd Command2.hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnSubclassWnd Command2.hwnd
End Sub
Oh, ok.
Try my code. If you still don't get the events, you can try Megatron's code to subclass. I don't think you need to subclass the control, though.
And if you have trouble getting the events, I doubt subclassing will help. But I could be wrong..
THANK YOU Megatron! IT WORKED!!!!!!!!!