如何在第二台显示器(屏幕)上隐藏部分窗口

时间:2016-04-01 20:57:04

标签: c# wpf windows xaml screens

窗口转到第二台显示器,但不应该是可见的。如何隐藏该区域?

Example image

1 个答案:

答案 0 :(得分:0)

不清楚为什么需要这个,但可以通过一些努力来实现。诀窍是OpacityMask属性,它允许使元素部分透明。一些代码可以给你粗略的想法:

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