Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
c# visual studio

Is it possible to capture a window title through an e.location and how?
Posted
Comments
Richard MacCutchan 1-Mar-15 13:24pm    
No, you need the Window's handle.

In addition to Solution 1, which will hardly be enough:

You can do the following:
  1. Get HWND of the desktop window: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633504%28v=vs.85%29.aspx.
  2. Iterate through all desktop children: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633494%28v=vs.85%29.aspx.
  3. For each child window, get its bounding rectangle: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633519%28v=vs.85%29.aspx.
  4. For each child window iterated window, determine if the point is screen coordinates lies inside the rectangle.
  5. Now, remember that windows can overlap, so the iteration steps described in two previous items may give you more that one window. Choose one of them (https://en.wikipedia.org/wiki/Axiom_of_choice :-)). For this purpose, you may need to consider Z-order of those windows. Instead of iteration explained above, you can take to top window on the desktop and then create a ordered list of all children on Z-order. Please see:
    https://msdn.microsoft.com/en-us/library/ms633514%28VS.85%29.aspx,
    https://msdn.microsoft.com/en-us/library/windows/desktop/ms633509%28v=vs.85%29.aspx.
  6. Finally, do what Solution 1 suggested: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633520%28v=vs.85%29.aspx.
  7. PROFIT!


Yes, you need P/Invoke, but all the P/Invoke declarations are already created for you. Just Google on each function name. For example, for first function, it will be https://msdn.microsoft.com/en-us/library/windows/desktop/ms633504%28v=vs.85%29.aspx.

Remember: if you do all that to uniquely identify some external window or a process, you cannot really rely on window's title. Anyone can give you some application with some stupid window title like "Notepad" or "Explorer". If you explain your ultimate goals, chances are, we can help you better.

—SA
 
Share this answer
 
v4
Comments
Maciej Los 27-Mar-15 9:48am    
What can i say, Maestro? You quote my answer twice, but i do not see any benefits of this... ;(
Upvoted!
Thank you, Sergey, i'm fine. Last days i've been bit busy, but i like it! I hope to meet you on Skype this weekend...
Sergey Alexandrovich Kryukov 27-Mar-15 11:23am    
Thank you, Maciej.
Yes, let's try to meet; probably I'll have to ask a couple of questions.
—SA
As Richard MacCutchan mentioned, you have to get window's handle.

Have a look here: http://www.pinvoke.net/default.aspx/user32.getwindowtext[^]. Link to MSDN documentation: GetWindowText function[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Mar-15 15:41pm    
Well, not bad, but sorry, you explained no more then some 14% of what's involved. The most missing item is the canonical #7 :-) — please see my answer.
Anyway, your solution is credited.
How are you doing?
—SA
Maciej Los 27-Mar-15 9:51am    
Is it possible to measure/count how much worth is my answer? :laugh:
Only 14%? I think that only 14% is missing to 100.
Sergey Alexandrovich Kryukov 27-Mar-15 11:28am    
I just counted the number of items. How cares what are they? :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900