上下文:我正在开发一个 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/