Search code examples
c#wpfwindowsxamlscreens

How to hide part of the window on the second monitor(screen)


The window goes to a second monitor, but there should not be visible. How to hide the area?

Example image


Solution

  • It's not clear why you need this, but it can be achieved with some effort. The trick is OpacityMask property, which allows to make elements partially transparent. Some code to give you rough idea:

    public MainWindow() {
            InitializeComponent();            
            this.WindowStyle = WindowStyle.None; // required for AllowsTransparency
            this.AllowsTransparency = true; // allow window to be transparent            
            var group = new DrawingGroup();
            // make first 100x1000 part of window transparent
            group.Children.Add(new GeometryDrawing() {Brush = Brushes.Transparent, Geometry = new RectangleGeometry(new Rect(0, 0, 100, 1000))});
            // make the rest part white or whatever color you use
            group.Children.Add(new GeometryDrawing() {Brush = Brushes.White, Geometry = new RectangleGeometry(new Rect(100, 0, 1000, 1000))});
            this.OpacityMask = new DrawingBrush(group) {
                Stretch = Stretch.None,
                AlignmentX = AlignmentX.Left,
                AlignmentY = AlignmentY.Top
            };
        }