2011-02-11 35 views
0

在.NET中,是否可以創建可以點擊的非透明表單?我認爲應該有某種API將鼠標點擊傳送到窗體後面的窗口。哪一個?.NET中的非透明點擊形式

+0

WinForms或WPF? (這也看起來像一個*非常奇怪的請求,用戶通常期望不透明的窗口會接收鼠標事件。) – 2011-02-11 00:24:05

回答

2

要創建窗體點擊,您需要從Windows API調用幾個函數並設置窗體的extended window styles。我擅自選擇在VB.NET中代表示例代碼。如果這不是您的偏好,它很容易轉換爲C#。

GetWindowLong function開始,您將使用它來檢索擴展窗口樣式。

Public Const GWL_EXSTYLE As Integer = -20 

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _ 
Public Shared Function GetWindowLong(ByVal hWnd As IntPtr, _ 
            ByVal nIndex As Integer) As Integer 
End Function 

您還需要其姐妹功能SetWindowLong來指定其他擴展窗口樣式。

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _ 
Public Shared Function SetWindowLong(ByVal hWnd As IntPtr, _ 
            ByVal nIndex As Integer, _ 
            ByVal dsNewLong As Integer) As Integer 
End Function 

和恆定的擴展窗口風格,將需要設置:

Public Const WS_EX_TRANSPARENT As Integer = &H20 


而現在使用的這一切,你可以重寫表單的OnLoad method,並添加以下行:

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs) 
    ''# Call the base class implementation 
    MyBase.OnLoad(e) 

    ''# Grab the current extended style information for this form 
    Dim initialStyles As Integer = GetWindowLong(Me.Handle, GWL_EXSTYLE) 

    ''# Add the transparent extended window style 
    Dim newStyles As Integer = initialStyles Or WS_EX_TRANSPARENT 

    ''# Update the form's extended window styles 
    SetWindowLong(Me.Handle, GWL_EXSTYLE, newStyles) 
End Sub 

當然,請注意,現在用戶無法與表單上的元素進行交互, 極其很難讓他們關閉它。仔細考慮這是否真的是你想要做的。