Find which MDI child just got focus Win32 API - c#

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.

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

Related

Forms application not receiving mouse or keyboard events

I have a C# .NET 2.0 application running on client kiosk machine (Windows XP) that occasionally puts up a window for user input. The other kiosk software seems to be interfering with it somehow because while it is running our window will not receive Mouse or Keyboard events despite it being the top window. When the other software is not running all works as expected. Does anyone have any insight as to what might be going on here?
Thanks
If in doubt - use Spy++ provided with Visual Studio to see what messages your window receives from the system.
If the other application blocks all input, it's not conforming to the Win32 API. That's why the low level hooks timeout was introduced in Vista and newer Win OS. Meaning a process would be kicked out of the low level hook chain, if it held onto a hook too long before calling CallNextHookEx() and not receive any low level hook messages anymore.
On Windows XP, there is no such limitation. A process can take as much time as they want to process a hooking message. The other program is either buggy, or evil. If it's essential that your application has input, then just close the other one programmatically or contact the author of it and explain the situation.
Take a look here
http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C
Using global keyboard hook should do just fine and some sources are included as well. Also, some user seem to solve similar problem by using ManagedSpyLib:
https://stackoverflow.com/a/8829286/1284902
After some digging, it is possible that the other windows forms program is utilizing a low level keyboard hook similar to one found here http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx. After following a link in that article, I came across a summary of that code snippet
For a concrete example of this, consider the (buggy) code in Figure 4. Using a low-level keyboard windows hook, the code intercepts all WM_KEYDOWN messages sent to any window and prints out the corresponding key.
Source: http://msdn.microsoft.com/en-us/magazine/cc163606.aspx

How do I manage an external Windows application in C#?

What is the best way to manage an external windows application in C# (or .NET)?
So far my I've been able to launch a process using System.Diagnostics.Process, however this simply allows me to launch/kill a process. (from what I've gathered)
I noticed System.Diagnostics.Process has a CloseMainWindow() routine which will send a request to a process' window. Can I use this Process class to send different messages? (if so, can anyone point me in direction of where I can learn about these windows messages)
I need to be able to manage an external program and manipulate it the following ways:
1) Launch
2) Kill Process
3) Show Application (Fullscreen and in taskbar)
4) Hide Application (Fullscreen and in taskbar)
Further details:
Windows 7, Restricted to .Net 3.5 Framework
You might be able to use interop and use SendMessage to do all of your functionality. See this: http://pinvoke.net/default.aspx/user32.SendMessage
Try sending a message to that window. Take a look at SendMessage . The messages you need are SW_MINIMIZE,SW_RESTORE and SW_SHOWMAXIMIZED.
You're going to need to use some Win32 P/Invoke stuff to send window messages in order to do what you want.
See this code sample on ShowWindow or SendMessage which tells an external window to show or hide itself. You'll first need to get the window handle you want with FindWindowEx.
you can do all you want in different ways, once you start the process yourself or you find it in the list of running processes, using classes or methods of System.Diagnostics.Process, then you can follow different options...
for example consider that once you have this handle: Process.MainWindowHandle you can send messages to that handle, with SendMessage and PostMessage and this allows you to do very very much; or you can use PInvoke APIs like SetWindowPos or ShowWindow or SetWindowLong and do basically really everything...
see this for example:
How to use the ShowWindow API to hide and show a form
I can send you more links but won't like to refer to the whole MSDN ;-)

Register to window messages using 'hwnd'

Assuming I have a valid hWnd to an active window (it was retrieved using ‘GetForegroundWindow’ or FindWindow, for example), can I register to windows message of this hWnd, from another application, of course? Is there any Win32 API for this, or a built-in C# method?
Example: In my console application I have a hWnd to the ‘Notepad’ window (running in the background). Can I register somehow to messages indicating the ‘Notepad’ window had moved, changed size, minimized, closed, etc.?
Thanks,
Elad
You have to inject a DLL into the process that owns the window. Use SetWindowsHookEx(), WH_CALLWNDPROC hook. You cannot write such a DLL using the C# language, the process won't have a CLR initialized and ready to run your code. Native code is required. Check this project.
If you just need to know about it moving or closing, then check out SetWinEventHook. You'll have to write a P/Invoke wrapper for this, since there's no equivalent in the .Net API. You can use this without needing to inject a DLL (use the WINEVENT_OUTOFCONTEXT
flag), so can keep everything in C# (and it also avoids the hassle of having to communicate between a separate DLL and your main process).
You'll likely want the events EVENT_OBJECT_LOCATIONCHANGE and EVENT_OBJECT_DESTROY. Note that you get these for all HWNDs on the thread you are listening to, so you'll need to filter by HWND in your callback.
Note that when you use WINEVENT_OUTOFCONTEXT, your own code will need to have a message loop running on the thread that calls SetWinEventHook. Also, the notifications that you receive are not delivered instantly, but with a slight delay (similar to PostMessage), but that may still be fast enough for your purposes that you can avoid doing an in-proc hook.
You can use the Accessible Event Watcher app (AccEvent) that's part of the Windows SDK to see what messages are available from notepad before you start writing code.

how to close a window from a different process

I have a c# application which I want to instruct to shutdown nicely, from a different process.
I also want to be able to ask it to open its main window.
I have a reference to its main window handle.
I know I can do it using elaborate schemes such as remoting or WCF.
the question is can I do it using simpler mechanisms such as window messages, or the OnClose event handlers of the window in the c# application
Pinvoke SendMessage() to send the WM_CLOSE message to the app's main window. Asking it to open its main window is probably going to be quite difficult, you cannot obtain the window handle until the window is created. Process.Start() would be the normal way.
A low cost alternative to WCF and superior to pinvoke is a named pipe or a socket to interface with the app. This requires being able to modify the source code of the app.
Process.CloseMainWindow
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.closemainwindow.aspx
I think that you could probably use FindWindow to find the correct child window and then SendMessage or PostMessage to send a WM_CLOSE.
Here's another StackOverflow question that deals with doing this in C#.
Edit: Though as the other answer says in that question, you might be able to use Process.CloseMainWindow instead.
I would first make my target application "aware" of the fact that some other process might like to trigger its closing or opening of a window or doing any action. This should be implemented similar to calling any method on another process through the target app's api or public methods. ..unless you are trying to do something with 3rd party applications, I think you shouldn't attempt to directly send them messages to close or shutdown.

C# - Determine if user is moving a window

I am going to be checking if the user is moving any window around (my application does not have an interface) and respond accordingly. What do you think is the best way to do this? Can I determine if the user is clicking on a titlebar? Can I determine if a window is being moved? I then need to grab the hWnd of the window after I know it's being moved.
To get notifications for all windows, not just Windows Forms ones, you'll need to use a hook set by the SetWindowsHookEx() API function. You'll need a WH_CALLWNDPROC hook so you can see the WM_MOVE message that Windows sends to the window.
Unfortunately, that's a global hook. The code that implements the hook callback needs to be packaged into a DLL so that it can be injected into all target processes. That shoots a hole into your plans to use C# for this, you can't inject the CLR. The DLL must be written in unmanaged code.
This code project offers an approach, including the unmanaged injectable DLL you'll need.
here is a technique to spy on window handles. You can inspect all the handles which are open and wait for the move messages.
EDIT
.NET spy code.

Categories

Resources