2016-10-04 53 views
5

我正在設計一個始終在屏幕上並且大約20%不透明的窗口。它被設計成一種狀態窗口,所以它總是處於頂層,但我希望人們能夠通過窗口點擊下面的任何其他應用程序。這裏的坐在這上面不透明窗口,張貼,因爲我現在鍵入:Windows窗體:通過部分透明的始終在頂部窗口傳遞點擊

Example

請參閱灰色條?這會阻止我此刻在標籤框中輸入內容。

+1

不可能的WinForms。 –

+0

您是否有任何證據支持該答案?我覺得很難相信... –

+1

相信它。我的「證據」是超過十年使用勝利形式的經驗。 –

回答

12

您可以創建一個窗口,通過將WS_EX_LAYEREDWS_EX_TRANSPARENT樣式添加到其擴展樣式來進行點擊。也使其始終在頂部設置了TopMosttrue並使其半透明使用適合Opacity值:

using System; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     this.Opacity = 0.5; 
     this.TopMost = true; 
    } 
    [DllImport("user32.dll", SetLastError = true)] 
    static extern int GetWindowLong(IntPtr hWnd, int nIndex); 
    [DllImport("user32.dll")] 
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); 
    const int GWL_EXSTYLE = -20; 
    const int WS_EX_LAYERED = 0x80000; 
    const int WS_EX_TRANSPARENT = 0x20; 
    protected override void OnLoad(EventArgs e) 
    { 
     base.OnLoad(e); 
     var style = GetWindowLong(this.Handle, GWL_EXSTYLE); 
     SetWindowLong(this.Handle,GWL_EXSTYLE , style | WS_EX_LAYERED | WS_EX_TRANSPARENT); 
    } 
} 

示例結果

enter image description here

+0

@ rory.ap這似乎是工作。謝謝你Reza! –

+5

@SamWeaver - 證明你總是可以學習新的東西:) –

+1

你可以重寫CreateParams並使用P/Invoke來設置ExStyle。 – TnTinMn