2014-06-18 99 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); 
}