窗口内的透明矩形 [英] Transparent rectangle inside window

查看:36
本文介绍了窗口内的透明矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有最顶层的全屏窗口

AllowsTransparency="True" WindowStyle="None" Topmost="True" WindowState="Maximized" Left="0" Top="0"
OpacityMask="#2B000000" Background="Black" Cursor="Cross"

当用户按住 LMB 并移动鼠标时,我正在绘制矩形(这是选择,就像我的问题中的屏幕截图 选择屏幕区域的最简单方法 ).

and I'm drawing rectangle when user holds LMB and moves mouse (it's selection, like on screenshot in this my question Easiest way to select screen region ).

我想让矩形完全透明,以查看窗口后面的内容.但我不能让它比父窗口更透明.我该怎么办?

I want to make rectangle completely transparent, to see content behind window. But I can't make it more transparent than parent window. What should I do?

推荐答案

尝试类似下面的操作,并在某些鼠标事件处理程序中动态更改第二个 RectangleGeometry (selectRect) 的大小和位置.也许还可以将第一个 RectangleGeometry 的大小调整为您的屏幕大小.

Try something like below and dynamically change the size and location of the second RectangleGeometry (selectRect) in some mouse event handler. And perhaps also adjust the size of the first RectangleGeometry to your screen size.

<Window x:Class="TransparentRectangle.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            WindowStyle="None" WindowState="Maximized"
            AllowsTransparency="True" Background="Transparent">
    <Grid>
        <Path Fill="Black" Opacity="0.5">
            <Path.Data>
                <CombinedGeometry GeometryCombineMode="Exclude">
                    <CombinedGeometry.Geometry1>
                        <RectangleGeometry x:Name="screenRect" Rect="0,0,2000,2000"/>
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <RectangleGeometry x:Name="selectRect" Rect="100,100,200,100"/>
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </Path.Data>
        </Path>
    </Grid>
</Window>

然而,一个问题可能是您不会在 CombinedGeometry 的排除部分中获得任何鼠标事件.为了避免这种情况,您可以将鼠标处理程序附加到窗口(而不是路径)并为其提供几乎透明的背景.

A problem might however be that you won't get any mouse events in the excluded part of the CombinedGeometry. To avoid that you could attach the mouse handlers to the Window (instead of the Path) and give it a nearly transparent background.

<Window ... Background="#01000000" MouseMove=... etc>
    ...
</Window>

一个更简单的解决方案可能是边框.您可以独立调整 BorderThickness 的四个分量.

An even simpler solution might be a Border. You could independently adjust the four components of the BorderThickness.

<Grid ...>
    <Border BorderBrush="Black" Opacity="0.5" BorderThickness="100,100,200,400"/>
</Grid>

这篇关于窗口内的透明矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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