2014-06-18 96 views
0

好吧,我有WPF應用程序的問題。到目前爲止,我設法制作了一個透明背景的窗口(+無刷子)。此外,我添加了功能,如果我的窗口是重點。所以顯然我的窗戶應該永遠不會集中(因爲透明度)。這是工作,但是當我加入可以說矩形(畫布):WPF全透明+不可點擊

     Rectangle testRectangleForText = new Rectangle(); 
         testRectangleForText.Stroke = Brushes.Black; 
         testRectangleForText.StrokeThickness = 5; 
         testRectangleForText.Fill = null; 
         testRectangleForText.Height = 300; 
         testRectangleForText.Width = 300; 
         Canvas.SetLeft(testRectangleForText, 0); 
         Canvas.SetTop(testRectangleForText, 20); 

         myCanvas.Children.Add(testRectangleForText); 

的矩形是可以點擊的,如果我點擊它,我的應用程序集中(applicationFocus功能顯示消息框),我不想那。我已經找到了Win表單的解決方案,但不是WPF的,這就是爲什麼我在這裏問這個問題。解決方案雙贏形式是在這裏:WINFORM SOLUTION我想要實現

現在沒事例如: example image

所以紅色區域是我的窗口(WPF應用程序)的大小。背景是透明的(顯然)。後臺應用程序是記事本。我們可以在畫布上看到文字和矩形。 現在,如果我點擊1.(第一個)箭頭,這是btw透明區域,沒有任何反應(那很好)。如果我點擊2.(第二個)箭頭,MessageBox就會出現,這意味着我的WPF APP是專注的,這就是我不想要的。

+0

嘗試將矩形的'Fill'屬性設置爲空{{:Null}''爲空畫筆。空筆刷與透明筆刷不同,不會對鼠標點擊產生反應。 – Blablablaster

+0

什麼是testRectangleForText.Fill = null; ?無論如何,矩形仍然是可點擊的(因爲「筆畫」),筆畫是黑色的。它必須是,所以我可以看到矩形:)。 – Janck7

回答

0

嘗試設置Focusable屬性,在XAML的代碼:

<Window ... Focusable="False"> 
    < ... /> 
</Window> 
+0

不工作給我..好吧我猜問題是在別的地方。我在這個Dispatcher.Invoke添加原語((Action)(()=> {....}));來自另一個線程。但是我沒有看到線程和畫布(或基元)屬性之間的聯繫。 Ty提前。 – Janck7

1

這爲我工作:

從這裏:https://social.msdn.microsoft.com/Forums/vstudio/en-US/41ca3605-247c-4c5b-ac5d-74ce5abd7b92/making-a-window-invisible-to-mouse-events-ishittestvisiblefalse-not-working?forum=wpf

我已經找到了如何做到這一點。關鍵是爲窗口的擴展style.You的WS_EX_TRANSPARENT標誌可以設置最上面的屬性像往常一樣,那麼這個代碼將使得窗口透明的鼠標點擊的護理:

代碼段

public const int WS_EX_TRANSPARENT = 0x00000020; 
public const int GWL_EXSTYLE = (-20); 

[DllImport("user32.dll")] 
public static extern int GetWindowLong(IntPtr hwnd, int index); 

[DllImport("user32.dll")] 
public static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle); 

protected override void OnSourceInitialized(EventArgs e) 
{ 
base.OnSourceInitialized(e); 

// Get this window's handle 
IntPtr hwnd = new WindowInteropHelper(this).Handle; 

// Change the extended window style to include WS_EX_TRANSPARENT 
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE); 
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT); 
}