Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a mouse click to a button in the taskbar using C#

In an application that I am currently working on, a requirement is to bring a window of an external application to the foreground. Making Win32 API calls such as BringWindowToTop and SetForeground window do not work all the time. This is due to some restrictions within Windows XP. What I would like to do instead is send simulate a mouse click the window's button on the taskbar which I am hoping will bring the window to the front. Does anyone know how this is possible?

like image 992
adeel825 Avatar asked Aug 20 '08 18:08

adeel825


2 Answers

Check out the section "How to steal focus on 2K/XP" at http://www.codeproject.com/KB/dialog/dlgboxtricks.aspx, as this is exactly what you need. I wouldn't go the taskbar route as the taskbar could be hidden or simply not there.

like image 162
Joel Lucsy Avatar answered Oct 11 '22 17:10

Joel Lucsy


It's possible. But it's extremely sketchy. Your application may also break with the next version of Windows, since it's undocumented. What you need to do is find the window handle of the taskbar, then find the window handle of the child window representing the button, then send it a WM_MOUSEDOWN (I think) message.

Here's a bit on finding the window handle of the taskbar:

http://www.codeproject.com/

FWIW, the restrictions on BringWindowToTop/SetForeground are there because it's irritating when a window steals focus. That may not matter if you're working on a corporate environment. Just keep it in mind. :)

like image 31
TheSmurf Avatar answered Oct 11 '22 19:10

TheSmurf