在Windows 11中,Windows Basic(非Aero)风格在某些地方仍然存在。该窗体在 Visual Studio 设计器以及 MDI 应用程序中显示时,显示 Windows 7 样式的 Windows Basic 主题。
您可以通过为任何您想要的应用程序启用 Windows XP 兼容模式来使该样式显示在该程序上,并且它将仅显示该程序的 Windows 基本样式。但是,问题是它必须在 XP 兼容模式下运行,并以管理员身份运行。
是否可以通过编程方式仅针对一种表单或程序禁用 DWM/Aero 样式而不启用兼容模式?
我尝试了 P/Invoking
DwmEnableComposition()
,但问题是根据 Microsoft 文档,它不再适用于 Windows 8 及更高版本。而且,即使它有效,它也会禁用整个桌面的 DWM,而不仅仅是一个程序或表单。
DWM
为此公开了一个API:DwmSetWindowAttribute
,您可以使用它为窗口设置各种属性,其中之一是非客户区渲染策略(DWMWA_NCRENDERING_POLICY
)。
将
DWMWA_NCRENDERING_POLICY
属性值设置为 DWMNCRP_DISABLED
会禁用 DWM
的非客户端渲染,从而提供您想要的基本窗口样式。
这是示例代码。
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace DisableDWMRendering
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
DWMNCRENDERINGPOLICY renderingPolicy = DWMNCRENDERINGPOLICY.DWMNCRP_DISABLED;
int hr;
hr = DwmSetWindowAttribute(Handle, DWMWINDOWATTRIBUTE.DWMWA_NCRENDERING_POLICY, renderingPolicy, sizeof(DWMNCRENDERINGPOLICY));
if (hr != 0)
{
throw Marshal.GetExceptionForHR(hr);
}
}
private enum DWMNCRENDERINGPOLICY
{
DWMNCRP_USEWINDOWSTYLE,
DWMNCRP_DISABLED,
DWMNCRP_ENABLED,
DWMNCRP_LAST
}
private enum DWMWINDOWATTRIBUTE : uint
{
DWMWA_NCRENDERING_ENABLED = 1, // [get] Is non-client rendering enabled/disabled
DWMWA_NCRENDERING_POLICY, // [set] DWMNCRENDERINGPOLICY - Non-client rendering policy
DWMWA_TRANSITIONS_FORCEDISABLED, // [set] Potentially enable/forcibly disable transitions
DWMWA_ALLOW_NCPAINT, // [set] Allow contents rendered in the non-client area to be visible on the DWM-drawn frame.
DWMWA_CAPTION_BUTTON_BOUNDS, // [get] Bounds of the caption button area in window-relative space.
DWMWA_NONCLIENT_RTL_LAYOUT, // [set] Is non-client content RTL mirrored
DWMWA_FORCE_ICONIC_REPRESENTATION, // [set] Force this window to display iconic thumbnails.
DWMWA_FLIP3D_POLICY, // [set] Designates how Flip3D will treat the window.
DWMWA_EXTENDED_FRAME_BOUNDS, // [get] Gets the extended frame bounds rectangle in screen space
DWMWA_HAS_ICONIC_BITMAP, // [set] Indicates an available bitmap when there is no better thumbnail representation.
DWMWA_DISALLOW_PEEK, // [set] Don't invoke Peek on the window.
DWMWA_EXCLUDED_FROM_PEEK, // [set] LivePreview exclusion information
DWMWA_CLOAK, // [set] Cloak or uncloak the window
DWMWA_CLOAKED, // [get] Gets the cloaked state of the window
DWMWA_FREEZE_REPRESENTATION, // [set] BOOL, Force this window to freeze the thumbnail without live update
DWMWA_PASSIVE_UPDATE_MODE, // [set] BOOL, Updates the window only when desktop composition runs for other reasons
DWMWA_USE_HOSTBACKDROPBRUSH, // [set] BOOL, Allows the use of host backdrop brushes for the window.
DWMWA_USE_IMMERSIVE_DARK_MODE = 20, // [set] BOOL, Allows a window to either use the accent color, or dark, according to the user Color Mode preferences.
DWMWA_WINDOW_CORNER_PREFERENCE = 33, // [set] WINDOW_CORNER_PREFERENCE, Controls the policy that rounds top-level window corners
DWMWA_BORDER_COLOR, // [set] COLORREF, The color of the thin border around a top-level window
DWMWA_CAPTION_COLOR, // [set] COLORREF, The color of the caption
DWMWA_TEXT_COLOR, // [set] COLORREF, The color of the caption text
DWMWA_VISIBLE_FRAME_BORDER_THICKNESS, // [get] UINT, width of the visible border around a thick frame window
DWMWA_SYSTEMBACKDROP_TYPE, // [get, set] SYSTEMBACKDROP_TYPE, Controls the system-drawn backdrop material of a window, including behind the non-client area.
DWMWA_LAST
}
[DllImport("dwmapi.dll", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
private static extern int DwmGetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE dwAttribute, ref DWMNCRENDERINGPOLICY pvAttribute,
uint cbAttribute);
[DllImport("dwmapi.dll", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE dwAttribute, in DWMNCRENDERINGPOLICY pvAttribute,
uint cbAttribute);
}
}