Click here to Skip to main content
15,849,130 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to detect window changes. Potentially without polling. I have found this article: http://www.relaut.com/detect-active-window-title-without-polling-c/[^].
Works great if the window activation implies putting a new process in foreground. But does not work if I am changing the active browser tab for example.
Is there any similar way that detects this scenario too?

Thank you.
Posted
Comments
BillWoodruff 24-Nov-15 20:45pm    
The context here is your use of some WebBrowser Control ... which one ? ... that does not have Tabs or (if it has Tabs) does not expose TabChanged Events in what context: WinForms ? WPF ?

WPF has a native TabControl that generates 'SelectionChanged Events, and one could put a WPF WebBrowser Control in each TabPage of that Control.
Zoltán Zörgő 25-Nov-15 5:11am    
I am talking about hooking on external process, not a control in my application.

1 solution

I don't think so. This is a totally internal to the browser. For the outside word, there is no such concept as "tab", because they can be implemented in many different ways. It wasn't obvious what you are talking about, but the look at the article you referenced suggests that you are talking about using Windows Hooks for hooking up behavior of the foreign processes. But let's consider what those processes can be. Yes, if they have a main window, it will be accessible with all its top-level events, but how about the general case? Nothing can be told, really.

For example, let's consider WPF WebBrowser control. It does not have tabs itself, so a WPF application may implement them. But WPF tabs, as all other UIElement objects except some object exposing Window to Windows OS, are not window objects. They work without message pumping and interact with OS only on the level of DirectX (which is closest to the hardware); and all event-based UI is connected with OS messaging system only through the top Window object. This way, those tabs will be something totally abstracted from their role, from the OS standpoint, so there is no any predefined way to recognize them. For OS, they simply don't exist.

Now, let's imaging for a second that such tabs are implemented in the control. After all, the class System.Windows.Controls.WebBrowser is based on System.Windows.Interop.ActiveXHost. I don't think the tabs are part of if, but let's just imagine it. So what? Even if the control was totally based on a Windows control, the tabs can play any thinkable role, as there is not a standard telling that a browser should have tabs or some tabs should be part of a browser. Moreover, nothing (except apparent expenses) prevents anyone from implementing a browser from scratch fully based on WPF. Or on DirectX. Or something else. For Windows OS, this would be a noumenon, Kant's unknowable "thing-as-such" (most popular inaccurate English rendering: "thing-in-itself"). :-)

—SA
 
Share this answer
 
v2
Comments
Zoltán Zörgő 25-Nov-15 5:58am    
This is what I was thinking also. What I was hopeing for is that changing the internal "window" changes the title of the windows window also (or triggers something else on process or system level), and that could be hooked.
Well, I solved my original problem with polling. It is not as elegant as it could have been... :)
Sergey Alexandrovich Kryukov 25-Nov-15 9:36am    
Sorry about that. :-)

Wait a minute...

I hope it wasn't a real polling. You can still use hooks. Only you could have done redundant requests to the OS to detect some change, but you don't need to poll by timer or something like that, which would be just nasty. ()
I've used the following technique with Windows hooks: a hook simply put some system-global wait handle object to signaled state. I use a separate thread for "polling", but actually it sleeps all the time spending no CPU time. When the global wait handle object is signaled, it wakes up the thread. So, it happens only when some class of messages is hooked by the Windows hook.

—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