允许调整大小的无边框窗口显示相同的行为:
然而,不可调整大小的无边界窗口在它处于活动状态和非活动状态时总是看起来好像没有聚焦(如上图3所示).它总是看起来发白:
这是我如何设置玻璃样式的一些示例代码:
public MainWindow(){ InitializeComponent(); windowstyle = windowstyle.None; ResizeMode = ResizeMode.noresize; Height = 200; Background = Brushes.transparent; Loaded += MainWindow_Loaded;}voID MainWindow_Loaded(object sender,RoutedEventArgs e){ var windowInteropHelper = new WindowInteropHelper(this); var handle = windowInteropHelper.Handle; var mainwindowsrc = HwndSource.FromHwnd(handle); if (mainwindowsrc != null) if (mainwindowsrc.CompositionTarget != null) mainwindowsrc.CompositionTarget.Backgroundcolor = color.FromArgb(0,0); var glassparams = new DWmAPI.DWmBlurbehind { DWFlags = DWmAPI.DWmBlurbehind.DWM_BB_ENABLE,fEnable = true,hRegionBlur = IntPtr.Zero }; IntPtr dis = new IntPtr(2); DWmAPI.DWmSetwindowAttribute(mainwindowsrc.Handle,DWmAPI.DWmWindowAttribute.DWMWA_LAST,dis,sizeof(uint)); DWmAPI.DWmEnableBlurBehinDWindow( handle,glassparams );}
我试着聚焦窗口,但这似乎并没有影响行为.关于如何解决这个问题的任何想法或指示?
解决方法 我没有在DWm APIs中工作太多,所以我可能错了,但我认为默认的DWm渲染策略是使用windowstyle来计算渲染.如果您禁用该策略,它的行为与您期望的一样.添加以下位来修复你的蓝调xD
const int DWMWA_NCRENDERING_POliCY = 2;int DWMNCRP_Disabled = 2;DWmSetwindowAttribute(hwnd,DWMWA_NCRENDERING_POliCY,ref DWMNCRP_Disabled,sizeof(int));
有关此伏都教的详细信息,请查看以下资源:
DwmSetWindowAttribute function
DWMWINDOWATTRIBUTE enumeration
MainWindow.xaml
<Window x:Class="DWm.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="MainWindow" Height="350" WIDth="525"/>
MainWindow.xaml.cs
using System;using System.Runtime.InteropServices;using System.windows;using System.windows.Interop;using System.windows.Media;namespace DWm{ public partial class MainWindow { [Dllimport("DWmAPI.dll",PreserveSig = false)] public static extern voID DWmEnableBlurBehinDWindow(IntPtr hwnd,ref DWmBlurbehind blurBehind); [Dllimport("DWmAPI.dll",PreserveSig = true)] private static extern int DWmSetwindowAttribute(IntPtr hwnd,int attr,ref int attrValue,int attrSize); public MainWindow() { InitializeComponent(); } protected overrIDe voID OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); var bb = new DWmBlurbehind { DWFlags = CoreNativeMethods.DWmBlurBehindDWFlags.DWmBbEnable,Enabled = true }; windowstartupLocation = windowstartupLocation.CenterScreen; Background = Brushes.transparent; ResizeMode = ResizeMode.noresize; windowstyle = windowstyle.None; Focus(); var hwnd = new WindowInteropHelper(this).Handle; HwndSource.FromHwnd(hwnd).CompositionTarget.Backgroundcolor = colors.transparent; DWmEnableBlurBehinDWindow(hwnd,ref bb); const int DWmwaNcrenderingPolicy = 2; var DWmncrpDisabled = 2; DWmSetwindowAttribute(hwnd,DWmwaNcrenderingPolicy,ref DWmncrpDisabled,sizeof(int)); } [StructLayout(LayoutKind.Sequential)] public struct DWmBlurbehind { public CoreNativeMethods.DWmBlurBehindDWFlags DWFlags; public bool Enabled; public IntPtr BlurRegion; public bool TransitionOnMaximized; } public static class CoreNativeMethods { public enum DWmBlurBehindDWFlags { DWmBbEnable = 1,DWmBbBlurRegion = 2,DWmBbTransitionOnMaximized = 4 } } }}
屏幕截图有焦点
没有焦点的截图
测试环境
*** 作系统:windows 8 – x64
主题:标准窗户
总结以上是内存溢出为你收集整理的c# – 没有边框的WPF玻璃窗口,没有焦点重新调整大小全部内容,希望文章能够帮你解决c# – 没有边框的WPF玻璃窗口,没有焦点重新调整大小所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)