tags:

views:

258

answers:

1

Hi all,

I have a program that has a MDI host and I would like to be able to get which of it children just got focus, bassiclly I would like to make a window focus changed event for this application.

The application is a 3rd party and I don't have the source, I have the window handle to the main program and the MDI host part.

I know I'll have to use Win32 API just not sure which ones.

I am writing my application in C#

Thanks.

+1  A: 

I guess what you're looking for is intercepting WM_SETFOCUS and WM_KILLFOCUS messages

The real problem is how are you going to do this. I guess the easiet way is to install a hook which is a subroutine to monitor the message traffic in the system and process certain types of messages before they reach the target window procedure. You're doing it by using SetWindowsHookEx winapi function with WH_CALLWNDPROC or WH_CALLWNDPROCRET types of hooks. There some are examples posted on codeproject; also there is one on msdn: How to set a Windows hook in Visual C# .NET

What is not really clear in your post is where your code running: in the same process with the MDI windows or is it a separate application\service? In case it is you would also need to inject your code into the remote process. Check this link for details on how you can do it: Three Ways to Inject Your Code into Another Process

hope this helps, regards

serge_gubenko
Since he has the handles, he can inherit from `System.Windows.Forms.NativeWindow` and call `AssignHandle(IntPtr)`. He can then listen to the messages for the particular window using `WndProc`.
Zach Johnson
agree, NativeWindow should also work; I would still stay with a hook as it IMHO less hassle to assign and it works for the whole application; besides it's not known if his window handles belong to the same process where his code is running
serge_gubenko
My plug in runs in it's own appdomian within the main application, would this cause a problem?
Nathan W
separate appdomain shouldn't be a problem. Also, pls, disregard "injection" portion of my post; I believe you don't need it for your task
serge_gubenko
Thanks, I have looked into it a bit, but haven't gotten very far. I'm not very strong in the Win32 API section.
Nathan W
serge_gubenko