Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically disable DWM and make the Windows 7-style Windows Basic window title bars appear for one WinForm or program in Windows 11?

In Windows 11, the Windows Basic (non-Aero) style is still present in some places. The form, when shown in the Visual Studio Designer, as well as in MDI applications, shows the Windows 7-style Windows Basic theme.

You can make this style appear for any application you want by enabling Windows XP compatibility mode for that program, and it will display the Windows Basic style for just that program. But, the problem is that it has to be run in XP compatibility mode, and as admin.

Is it possible to disable DWM/Aero styles programmatically for just one form or program without enabling compatibility mode?

I tried P/Invoking DwmEnableComposition(), but the problem is that it no longer works on Windows 8 and up, according to the Microsoft documentation. And, even if it worked, it disables DWM for the entire desktop, not for just one program or form.

like image 924
Felix An Avatar asked Oct 29 '25 10:10

Felix An


1 Answers

DWM exposes an api for this: DwmSetWindowAttribute, with which you can set various attributes for windows, one of them being Non-client area rendering policy (DWMWA_NCRENDERING_POLICY).

Setting the DWMWA_NCRENDERING_POLICY attribute value to DWMNCRP_DISABLED disables non-client rendering by DWM, thus giving your desired basic window style.

This is especially useful when drawing your own non-client area. The WM_NCPAINT take effect this way.

Here is the example code.

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);
    }
}

like image 191
jtxkopt Avatar answered Nov 01 '25 01:11

jtxkopt