2013-05-22 60 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

是的。用以前的代碼,它會消除焦點。新代碼修復了這個問題,但現在顯示在所有窗口之上,我只希望它顯示在選定窗口的上方。 –

相關問題