How to uniquely Identify windows forms?
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 i开发者_运维知识库f 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.
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.
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.
Every window has window handle. This is unique int32 value. This value is using by Win32 API to identify window.
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.
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.
精彩评论