1

Okay guys, so I have a problem with WPF application. So far I managed to make a window with transparent background ( + no brush ). Also I added function, if my window is focused. So obviously my window should never been focused (because of transparency). This is working, but when I add lets say rectangle (on Canvas):

                        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);

The rectangle is clickable and if I click on it, my app is focused (applicationFocus function display messageBox) and I don't want that. I already found solution for Win forms, but not for WPF, thats why I'm asking this here. Solution for win forms is here: WINFORM SOLUTION

Okay now example what I'm trying to achieve: example image

So the red zone is my window (WPF APP) size. Background is transparent (obviously). Background application is notepad. We can see text and rectangle on Canvas. Now, if I click on 1.(first) arrow, this is btw transparent area, nothing happens (thats good). If I click on 2.(second) arrow, MessageBox appear, which means that my WPF APP is focused and that is what I dont want.

Community
  • 1
  • 1
Janck7
  • 155
  • 1
  • 1
  • 7
  • Try to set rectangle's `Fill` property to `{x:Null}` which is Null brush. Null brush is different from Transparent brush and doesn't react to mouse clicks. – Blablablaster Jun 18 '14 at 21:37
  • and what is testRectangleForText.Fill = null; ? Anyway the Rectangle is still clickable (because of "Stroke"), stroke is Black. It has to be, so I can see the rectangle :). – Janck7 Jun 18 '14 at 23:17

2 Answers2

3

This worked for me:

From here: 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

I've figured out how to do this. The key being the WS_EX_TRANSPARENT flag for the window's extended style.You can set the topmost property like you normally would, then this code takes care of making the window transparent to mouse clicks:

Code Snippet

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);
}
Derek
  • 7,615
  • 5
  • 33
  • 58
0

Try to set the Focusable attribute in your XAML-Code:

<Window ... Focusable="False">
    < ... />
</Window>
myst3rium
  • 154
  • 12
  • Not working to me.. Okay I guess problem is somewhere else. Im adding primitives in this.Dispatcher.Invoke((Action)(() => { .... })); from another thread. But I don't see a connection between thread and Canvas (or primitives) attributes. Ty in advance. – Janck7 Jun 18 '14 at 23:09