Reputation: 1
I have an application that needs to overlay another application's window. As the overlayed moves I need my application to move along with it.
I am using the following code to get the window and position my window over it.
public static void DockToWindow(IntPtr hwnd, IntPtr hwndParent)
{
RECT rectParent = new RECT();
GetWindowRect(hwndParent, ref rectParent);
RECT clientRect = new RECT();
GetWindowRect(hwnd, ref clientRect);
SetWindowPos(hwnd, hwndParent, rectParent.Left,
(rectParent.Bottom - (clientRect.Bottom -
clientRect.Top)), // Left position
(rectParent.Right - rectParent.Left),
(clientRect.Bottom - clientRect.Top),
SetWindowPosFlags.SWP_NOZORDER);
}
I also set the form.TopMost to true. The problem I am having is that the overlay takes focus away from the overlayed window. I just want my overlay to sit on top of this window but not steal focus. If the user clicks on the overlayed window I want it to work as it did before I placed the overlay. However, if the user clicks on the overlay I need to capture the mouse on the overlay.
Any ideas? Thanks
Upvotes: 0
Views: 1461
Reputation: 1
I was able to find a solution to this issue by updating the SetWindowPos code to use the overlay form's Left,Top,Right, and Bottom properties instead of using GetWindowRect.
RECT rect = new RECT();
GetWindowRect(hostWindow, ref rect);
SetWindowPos(this.Handle, NativeWindows.HWND_TOPMOST,
rect.Left+10,
rect.Bottom - (Bottom - Top),
(rect.Right - rect.Left),
Bottom - Top,
0);
This code aligns the overlay window along the bottom edge of the host window. The problem I have now is that my overlay is on top of all windows, not just the window I want to overlay. I've tried HWND_TOP which does the same thing, and the overlayed window handle, which places my overlay beneath the window.
Any ideas - do I need to use SetParent() ?
Upvotes: 0
Reputation: 81610
From Floating Controls, Tooltip-style, try this on your overlay form:
private const int WM_NCHITTEST = 0x0084;
private const int HTTRANSPARENT = (-1);
/// <summary>
/// Overrides the standard Window Procedure to ensure the
/// window is transparent to all mouse events.
/// </summary>
/// <param name="m">Windows message to process.</param>
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCHITTEST)
{
m.Result = (IntPtr) HTTRANSPARENT;
}
else
{
base.WndProc(ref m);
}
}
Upvotes: 0
Reputation: 1428
In winforms, you can avoid focus-setting by overriding ShowWithoutActivation
protected override bool ShowWithoutActivation
{
get { return true; }
}
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showwithoutactivation.aspx
Upvotes: 1