灰色显示桌面屏幕除了在C#中的选择的控制 [英] Gray out the Desktop screen except a selected control in c#

查看:127
本文介绍了灰色显示桌面屏幕除了在C#中的选择的控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要选择被点击UI控件和灰色,桌面屏幕的其余部分半透明。我想画位图的整个屏幕,但它是非常缓慢的过程。我想有人在那里知道WPF如何做到这一点。我没有的experiance在WPF。我想做到这一点在Windows 7上。

I want to select a UI control to be clicked and gray out the rest of the desktop screen semi-transparently. I was thinking bitmap painting the whole screen but it is very slow process. I guess someone out there know in WPF how to do that. I don't have experiance in WPF. I wanted to do this on Windows 7.

推荐答案

基本上你想展示顶级全屏幕透明窗口是不可作为焦点,并且不响应输入。然后,您可以使用此窗口来手动绘制覆盖。我认为,最简单的方法是覆盖的OnRender窗口,并绘制塞满了整个窗户,但使用剪贴蒙版(通过drawingContext.PushClip)排除要离开发现的区域的矩形。

Basically you want to show a top-level full screen transparent window which is not focusable and doesn't respond to input. You can then use this window to draw the overlay manually. I think the easiest way would be to override OnRender on the Window and to draw a rectangle that fills the whole window but uses a clipping mask (via drawingContext.PushClip) to exclude the area you want to leave uncovered.

编辑:

下面是一个例子:

窗口或许应该建立这样的:

The Window should probably be set up like this:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        AllowsTransparency="True"
        WindowStyle="None"
        WindowState="Maximized"
        Topmost="False"
        Background="{x:Null}"
        IsHitTestVisible="False">
</Window>

WindowStyle 的WindowState 设置将导致最大化和任务栏重叠的窗口。
朵蒙特设置为true,将导致窗口是在所有其他窗口的顶部和 IsHitTestVisible 将导致鼠标点击来通过下跌。

The WindowStyle and WindowStatesettings will cause the window to be maximized and overlapping the task bar. Topmost set to true will cause the window to be on top of all other windows and IsHitTestVisible will cause the mouse clicks to 'fall through'.

警告:如果设置了这个,你会被套牢,你不能关闭,因为它不听键盘命令一个最顶层的窗口。 您将需要一个地方来手动关闭该窗口中的code。有可能你想创建一个全局鼠标钩子听鼠标和一个键盘钩子听ESC或东西。

Warning: If you set this you will be stuck with a topmost window that you can't close since it doesn't listen to keyboard commands. You will need to manually close the window somewhere in your code. Likely you want to create a global mouse hook to listen to mouse up and a keyboard hook to listen to ESC or something.

要保存自己我在上面的例子中最顶层设置为False人。如果你已经知道如何/时,将在code某处窗口只更改为true。

背景设置为null,这样就可以使用自定义图形中的code的后面。

The Background is set to null so that you can use your custom drawing in code behind.

背后的code类似于这样:

the code behind similar to this:

public MainWindow()
{
    InitializeComponent();
}

protected override void OnRender(DrawingContext drawingContext)
{
    base.OnRender(drawingContext);
    var screenGeometry = new RectangleGeometry(new Rect(0, 0, ActualWidth, ActualHeight));
    var excludeRectangle = new RectangleGeometry(new Rect(200, 200, 150, 150));
    drawingContext.PushClip(CombinedGeometry.Combine(screenGeometry,excludeRectangle, GeometryCombineMode.Exclude,null));
    drawingContext.PushOpacity(.8);
    drawingContext.DrawRectangle(Brushes.Black, null, new Rect(0, 0, ActualWidth, ActualHeight));
    drawingContext.Pop();
    drawingContext.Pop();
}

在这里你将用于excludeRectangle正确的位置和大小。

where you would use the proper location and size for the excludeRectangle.

如果您运行此code,你会看到变灰,除了左上角一个小矩形屏幕。

If you run this code you will see the screen grayed out except for a little rectangle in the top left.

这篇关于灰色显示桌面屏幕除了在C#中的选择的控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆