Detect and Prevent Overlapping Windows in C#
Anyone know of an efficient way of detecting movement of any windows currently open on a windows system? I need to detect a window's movement, determine if it collides with my applications Form, and bump it out from underneath if necessary.
I know I can scan through an enumerated list and check each window -- but that is way to intensive to perform constantl开发者_开发技巧y.
Background:
I have a taskbar-esque application that docks on the side of a user's screen. When the "Always on Top" feature is on, maximized windows will take up the remaining available space without covering the toolbar, as expected.
However, if you drag a non-maximized window over the toolbar, the application goes behind the toolbar (also expected), but you can no longer grab onto the title bar to move it back -- the window is stuck unless you disable "Always on Top" and then move it. So, I want to bump the window out from underneath.
Although not a direct answer, one possible solution to this is to create your application as an application desktop toolbar rather than a regular window. From the docs:
An application desktop toolbar(also called an appbar) is a window that is similar to the Microsoft Windows taskbar. It is anchored to an edge of the screen... The system prevents other applications from using the desktop area occupied by an appbar. (emphasis added)
This may not be a great fit for your scenario because it is oriented towards COM and unmanaged code rather than managed apps: however see this CodeProject article for info about using this feature from C#.
Failing that, you could try installing a hook (see SetWindowsHookEx) and listening for move messages but this is pretty low-level...
Try checking your PaintEventArgs ClipRectangle ..
(edit: and/or WindowFromPoint shooting match)
You can get notification of window movements using a CBT Hook: http://msdn.microsoft.com/en-us/library/ms644977(VS.85).aspx
http://www.codeproject.com/KB/dialog/FindWindow.aspx?msg=3262771
"FindWindow By Jörg Bausch"
Will get you the external (not your app's) window ID (IntPtr) the mouse went up over from within your C# application. For the desktop, and everything else on the desktop, it will return the same pointer (you can't distinguish, using this code, between as mouse-up on a folder, the desktop, the Recycle Bin).
http://www.codeproject.com/KB/cs/globalhook.aspx
"Processing Global Mouse and Keyboard Hooks in C# By George Mamaladze"
Will allow you to create GlobalHook for keyboard and mouse-events in C#. I've used it recently in VS 2010 beta 2 : it is NOT USABLE compiled against FrameWork 4.0, but does compile and work okay against FrameWork 3.5 and lower. If you download only George's demo app, be aware the download doesn't include the required dll, and will fail when you launch the .exe file (which I have brought to George's attention).
I've never worked with a "desktop application toolbar;" I hope this is relevant.
best,
精彩评论