C#-Windows窗体-Windows 7-覆盖另一个应用程序';窗户

C#-Windows窗体-Windows 7-覆盖另一个应用程序';窗户,c#,.net,C#,.net,我有一个应用程序需要覆盖另一个应用程序的窗口。随着叠加的移动,我需要我的应用程序随之移动 我使用下面的代码来获取窗口并将窗口放置在上面 public static void DockToWindow(IntPtr hwnd, IntPtr hwndParent) { RECT rectParent = new RECT(); GetWindowRect(hwndParent, ref rectParent); RECT clientRec

我有一个应用程序需要覆盖另一个应用程序的窗口。随着叠加的移动,我需要我的应用程序随之移动

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

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。 我遇到的问题是,叠加会将焦点从叠加的窗口移开。 我只想我的覆盖层坐在这个窗口的顶部,但不窃取焦点。如果用户单击覆盖的窗口,我希望它像放置覆盖之前一样工作。 但是,如果用户单击覆盖,我需要捕获覆盖上的鼠标

有什么想法吗?
感谢winforms中的,您可以通过在不激活的情况下覆盖Show来避免焦点设置

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

从中,在覆盖表单上尝试以下操作:

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);
  }
}
private const int WM_nchitest=0x0084;
私有常量int HTTRANSPARENT=(-1);
/// 
///覆盖标准窗口过程以确保
///窗口对所有鼠标事件都是透明的。
/// 
///要处理的Windows消息。
受保护的覆盖无效WndProc(参考消息m)
{
如果(m.Msg==WM_nchitest)
{
m、 结果=(IntPtr)HTTRANSPARENT;
}
其他的
{
基准WndProc(参考m);
}
}

我通过更新SetWindowPos代码,使用覆盖表单的左、上、右和下属性,而不是使用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()吗?

我尝试过这个方法,它确实有效,但当我移动覆盖的窗口时,它仍然在窃取焦点。我有一个解决办法。@Jimbuchiferro太好了!您应该将您的修复作为自我回答发布给其他人,以便稍后阅读,还可以编辑您的问题,以包括移动时无焦点的要求。我的解决方案有效,但它会将我的覆盖保持在所有窗口的顶部,而不仅仅是我要覆盖的窗口。我以为问题是关于单击覆盖窗体将焦点移走。确实如此。在前面的代码中,它会带走焦点。新代码修复了现在显示在所有窗口上方的问题,我只希望它显示在选定窗口上方。