wpf - 临时透明的 wpf 应用程序 - 用于工具提示的 Visual Studio Ctrl

标签 wpf transparency

上下文:我正在开发一个 WPF 应用程序,用户经常需要从其他程序手动转录数据位。没有双显示器,这涉及大量的 Alt 键。

我认为如果工具提示具有与 Visual Studio 中相同的功能会非常好 - 您可以在其中按 Ctrl 使它们略微透明。

我遇到的“问题”是,如果您的窗口 AllowsTransparency,您只能(有用地)玩透明,而这又只有在 WindowStyle = None 时才有可能。我真的不想陷入必须在我的应用程序上重新创建标题栏等的所有复杂问题。

有什么好的选择吗?

最佳答案

使用一点 Win32 Interop,您可以获得窗口的 HWND 并使用 CreateRgn 并为您的窗口定义一个剪辑区域,这将根据您的需要呈现窗口的任何部分“透视”。它还会修改 HitTest ,因此,窗口不仅是“透视”的,而且是“点击通过的”。

这是我的一个宠物项目的一些片段,它消除了窗口的客户区。此处的某些代码可能特定于 WPF,但最终此处显示的 Win32 API 自 90 年代初就已存在。

    [DllImport("gdi32.dll")]
    internal static extern IntPtr CreateRectRgnIndirect([In] ref Mubox.Win32.Windows.RECT lprc);

    [DllImport("gdi32.dll")]
    internal static extern IntPtr CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);

    [DllImport("user32.dll")]
    internal static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);

    [DllImport("gdi32.dll")]
    internal static extern int CombineRgn(IntPtr hrgnDest, IntPtr hrgnSrc1, IntPtr hrgnSrc2, CombineRgnStyles fnCombineMode);

    public enum CombineRgnStyles : int
    {
        RGN_AND = 1,
        RGN_OR = 2,
        RGN_XOR = 3,
        RGN_DIFF = 4,
        RGN_COPY = 5,
        RGN_MIN = RGN_AND,
        RGN_MAX = RGN_COPY
    }

    #endregion

                    IntPtr windowRegion = Control.Windows.CreateRectRgn(0, 0, parkingWindowRect.Width, parkingWindowRect.Height);
                    Mubox.Win32.Windows.RECT clipRect = new Mubox.Win32.Windows.RECT();

                        Mubox.Win32.Windows.GetClientRect(this.Handle, out clipRect);
                        clipRect.Left = (parkingWindowRect.Width - clipRect.Width) / 2;
                        clipRect.Right += clipRect.Left;
                        clipRect.Top = (parkingWindowRect.Height - clipRect.Height) - clipRect.Left;
                        clipRect.Bottom = parkingWindowRect.Height - clipRect.Left;


                    IntPtr clipRegion = Control.Windows.CreateRectRgnIndirect(ref clipRect);
                    Control.Windows.CombineRgn(windowRegion, windowRegion, clipRegion, Windows.CombineRgnStyles.RGN_XOR);
                    Control.Windows.DeleteObject(clipRegion);
                    Control.Windows.SetWindowRgn(this.Handle, windowRegion, true);

关于wpf - 临时透明的 wpf 应用程序 - 用于工具提示的 Visual Studio Ctrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1491115/

相关文章:

wpf - StockTrader RI > Controller 、演示者、WTF?

wpf - 从 xaml 获取 RowIndex

c# - WPF 更改聚焦的文本框背景颜色

r - 具有透明度的重叠条形图

c# - 是什么让 Winform 职位最初变得陈旧?

ffmpeg - 使用ffmpeg缩放webm文件时如何保持透明度

javascript - Threejs - 使用 MeshDepthMaterial 绘制透明纹理

wpf - ElementName vs.RelativeResource?

WPF类加载错误?添加自定义类时崩溃

html - 重叠部分透明元素的不透明度