Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Notify when form is created

Status
Not open for further replies.

javierbalk

Programmer
Feb 27, 2002
22
0
0
AR
Hi all:

I am making an ActiveX control and I need to be notified when a new form is created in the client application. Now I am subclassing the form where my ActiveX control is placed and I found that the WM_WINDOWPOSCHANGING message fires every time a new form appear, and then I look in the Forms collection every time I have this message in order to see if there is a new form, but it seems to be not sure, so I am looking for another message or a way to subclass the entire program and find a message that notify me about a new window. Any ideas?
Thanks in advance,

Javier
 
Yes, this can be done. I'm away from my development machine ATM, so I can't provide example code. As a pointer in the meantime, I'd suggest you look at the CBT hooks...
 
Yes strongm, when you go to your development machine please send me the code please. If you want to send it by email my addess is javierbalk@hotmail.com. Thanks,

Javier
 
Ok, here's an example using the CBT hook. This code should go in a module:
[tt]
Option Explicit

Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long

' We could trap any of these events, but in this example we'll
' focus on HCBT_CREATEWND
Public Const HCBT_ACTIVATE = 5 ' Window activating
Public Const HCBT_CREATEWND = 3 ' Window about to be created
Public Const HCBT_DESTROYWND = 4


Public Const WH_CBT = 5

Public hHook As Long

Public Function CBTProc(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim Classname As String
Dim Result As Long



If lMsg = HCBT_CREATEWND Then
' We'll spot the creation of ALL windows created by this thread, so we
' need to filter out everything except forms
Classname = Space(128)
Result = GetClassName(wParam, Classname, 128)
Classname = Left$(Classname, Result)
If Classname = "ThunderFormDC" Then
Debug.Print "Form created"
End If
End If
CBTProc = False ' Continue processing...
End Function

Public Sub HookIt()
' Don't hook more than once
If hHook = 0 Then
hHook = SetWindowsHookEx(WH_CBT, AddressOf CBTProc, App.hInstance, GetCurrentThreadId) 'Just windows owned by our thread
End If
End Sub

Public Sub UnhookIt()
' Don't unhook if we are not hooked
If hHook <> 0 Then
UnhookWindowsHookEx hHook
hHook = 0
End If
End Sub
[/tt]
And the code below just demonstartes it in use (requires two forms, first one with a button on it):
[tt]
Option Explicit

Private Sub Command1_Click()
Form2.Show
End Sub

Private Sub Form_Load()
HookIt
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
UnhookIt
End Sub
[/tt]
WARNING: don't try and debug in the IDE when the hook is still in place.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top