2013-05-22 97 views
0

我有一个应用程序需要覆盖另一个应用程序的窗口。随着覆盖移动,我需要我的应用程序与它一起移动。C# - Windows窗体 - Windows 7 - 覆盖另一个应用程序的窗口

我正在使用下面的代码来获取窗口并将窗口放在它上面。

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); 

    } 

我还将form.TopMost设置为true。 我遇到的问题是叠加层将焦点从覆盖窗口中移开。 我只是想让我的覆盖层坐在这个窗口的顶部,而不是偷取焦点。如果用户点击覆盖窗口,我希望它能够像放置覆盖之前一样工作。 但是,如果用户点击叠加层,我需要在覆盖层上捕获鼠标。

任何想法? 感谢

回答

1

在的WinForms,你可以通过重写ShowWithoutActivation

protected override bool ShowWithoutActivation 
{ 
    get { return true; } 
} 

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showwithoutactivation.aspx

+0

我试过了,它确实有效,但是当我移动覆盖窗口时它仍然在偷窃焦点。我有一个解决方案。 –

+0

@JimBucciferro太好了!您应该将修复作为自我回答发布给其他人稍后阅读,并且还要编辑您的问题以在移动时也包括无焦点要求。 – Henrik

+0

我的解决方案可以工作,但它将覆盖层保留在所有窗口的顶部,而不仅仅是我要覆盖的窗口。 –

0

Floating Controls, Tooltip-style避免对焦设置,试试这个您的覆盖形式:

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); 
    } 
} 
0

我能通过更新SetWindowPos代码来使用覆盖窗体的左,上,右,和Bottom属性而不是使用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); 

此代码沿着主窗口的底部边缘对齐覆盖窗口。 我现在面临的问题是,我的叠加层位于所有窗口的顶部,而不仅仅是我要覆盖的窗口。我已经尝试过使用HWND_TOP来做同样的事情,并且覆盖了窗口句柄,它将我的覆盖层放在窗口下面。

任何想法 - 我需要使用SetParent()吗?

+0

我认为这个问题是关于点击重点表单的重点。 – LarsTech

+0

是的。用以前的代码,它会消除焦点。新代码修复了这个问题,但现在显示在所有窗口之上,我只希望它显示在选定窗口的上方。 –

相关问题