Get Previous Window Handle?
-
If my program is running and I start a program such as Internet Explorer, how do I get the window handle of IE when my programs is activated and gets focus again? I want to send keystrokes to differing programs. Currently, the only window I can get is the foreground window using the GetForeGroundWindow API when my program initially starts. If a new program starts while mine is running, in order to send keystrokes to it, I need to close my program and restart it, so I can get the new foreground window. Make sense? Any ideas?
-
If my program is running and I start a program such as Internet Explorer, how do I get the window handle of IE when my programs is activated and gets focus again? I want to send keystrokes to differing programs. Currently, the only window I can get is the foreground window using the GetForeGroundWindow API when my program initially starts. If a new program starts while mine is running, in order to send keystrokes to it, I need to close my program and restart it, so I can get the new foreground window. Make sense? Any ideas?
You can enumerate through the currently running processes and use the Process.MainWindowHandle[^] property to get the window handle. Something like
Process[] processes = Process.GetProcessesByName("notepad");
foreach(Process p in processes)
{
IntPtr handle = p.MainWindowHandle;
}Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
You can enumerate through the currently running processes and use the Process.MainWindowHandle[^] property to get the window handle. Something like
Process[] processes = Process.GetProcessesByName("notepad");
foreach(Process p in processes)
{
IntPtr handle = p.MainWindowHandle;
}Regards Senthil _____________________________ My Blog | My Articles | WinMacro
That doesn't help though. Within my code, I don't know which program just started or which program just had focus. It could be notepad, it could be IE, it could Vis Studio, or FireFox. It doesn't matter what the program is, I want the window handle of the window that had focus prior to my program.
-
That doesn't help though. Within my code, I don't know which program just started or which program just had focus. It could be notepad, it could be IE, it could Vis Studio, or FireFox. It doesn't matter what the program is, I want the window handle of the window that had focus prior to my program.
Unless your application is already running when the focus changes, there's no way to tell which application just had the focus. Your application will start way too late to get any notification of which window just lost the focus. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Unless your application is already running when the focus changes, there's no way to tell which application just had the focus. Your application will start way too late to get any notification of which window just lost the focus. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Yes, exactly. It will already be running. Here's the example again. My program is running, but doesn't have focus. I start IE or MS Word and do blah blah blah in it. I click back or focus my program. What's the handle of the app that just had focus before mine? -- modified at 9:11 Wednesday 12th October, 2005
-
Yes, exactly. It will already be running. Here's the example again. My program is running, but doesn't have focus. I start IE or MS Word and do blah blah blah in it. I click back or focus my program. What's the handle of the app that just had focus before mine? -- modified at 9:11 Wednesday 12th October, 2005
There is no API call you can make to give you the last window that had the focus. The only thing I can think of is to override the WndProc procedure in your main window and handle the WM_ACTIVATE[^] message. You'll have to track the window handle of the window that is being deactivated, then get the parent window of that window handle until you reach the top-level window handle of the application. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
There is no API call you can make to give you the last window that had the focus. The only thing I can think of is to override the WndProc procedure in your main window and handle the WM_ACTIVATE[^] message. You'll have to track the window handle of the window that is being deactivated, then get the parent window of that window handle until you reach the top-level window handle of the application. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
I couldn't seem to get that to work. However, I did find another solution. I found a class that receives all mouse and keyboard events from the system. I hooked up a couple events and now my program is told when someone clicks the mouse or presses a key. Then I just GetForeGroundWindow to get the active window and change the IntPtr my program is using to know what has focus. Works great!