tags:

views:

178

answers:

5

My application needs to identify every form of every opened application. For example, if i press "Ctrl+f1" when i am in any application(lets say microsoft outlook), and if i am in the "new message" form, i need to send a message to my application with the following information: process=outlook.exe form="new message" form id? (or whatever identifies the form uniquely? is that possible?

In other words, are windows forms uniquely identified?

Oh, and window handles are not the solution because they change everytime, i need something that identifies the form and that doesnt change if i close the application and reopen it.

A: 

Every window has window handle. This is unique int32 value. This value is using by Win32 API to identify window.

Andrey Shvydky
NOOOO, guys, please read before posting, it's true they are unique, but they don't keep the same value (check a window handle, turn off and on your machine, and check the handle again, is it the same?)
Zee99
+3  A: 

Windows have an HWND that is unique.

This thread talks about how to get information for the currently active window.

You could do something like that, and then get the window title.

taylonr
+1  A: 

Well - you can not really identify a window of a foreign process by its type if the titles are identical.

You can use the window's handle - however it will be different on same "types" of windows and is only valid while the window exists.

Your idea looks like some kind of logging - and logging handles is not very reasonable IMO.

winSharp93
+3  A: 

Beware that windows in other applications, especially Outlook, are rarely ever a Windows Forms form. You'll need lots of P/Invoke to make this work. First thing you'll need is SetWindowsHookEx() to set a WH_KEYBOARD_LL hook so that you can detect the keystroke. That googles well.

Next, you need GetForegroundWindow(), that gets you the window handle of the window that has the focus.

Next, you need GetWindowThreadProcessId(), that lets you discover the process ID of the process that owns the window. You can then use Process.GetProcessById() to get the Process object for the program. Lots of info there, the Name property tells you it is Outlook.exe

Info about the window itself is harder to obtain. Not much there, but you can use GetWindowText() to retrieve the text displayed on the caption bar. If useful at all, you could use EnumChildWindows to enumerate the child controls in the window.

Visit pinvoke.net for the required P/Invoke declarations.

Hans Passant
Thanks noBugz, whats important to me is to get some unique identifier for the window and save it in my application. For the moment all i can do is, like you say, get the window Text, but not much of a solution for me, wondering if there is a window unique identifier, somewhere...
Zee99
Erm, the handle value you get from GetForegroundWindow is unique. No two windows will ever have the same handle value.
Hans Passant
will a window get always the same handle when it opens?(what i mean by unique is "always the same value (always, say after a reboot for example)"? think of it as i have a database where i want to memorize for each window its unique identifier so i can be able to identify it when info is sent to my application.
Zee99
No, the handle value will be different. The caption text might be different too.
Hans Passant
A: 

Although window handles are unique, but they are never the same value for the same form, they are only unique between themselves, but a window will never have the same and unique value of its handle, that will identify it.

Windows forms can NOT be uniquely identified.

Zee99