Search code examples
c#winformsnonclient-area

How to use DWMWA_ALLOW_NCPAINT


I would like to use the DWMWA_ALLOW_NCPAINT to draw in the Non Client area but I cant figure out how to use it, can anyone help me?


Solution

  • It is a constant that you can pass it into the Win32 API function DwmSetWindowAttribute. You cannot use it with the function DwmGetWindowAttribute. It is "set-only".

    To call a native function from managed code, in your case, it is DwmSetWindowAttribute, you should use P/Invoke.

    In the below code, you can see how you can define the signature of native function DwmSetWindowAttribute and pass the constant DWMWA_ALLOW_NCPAINT into it.

    PS: However, I don't think it is enough to use DWMWA_ALLOW_NCPAINT to draw on Non-client Area. There are many of that topic on SO. You can search it.

    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace DWMWA_ALLOW_NCPAINT
    {
        public partial class MainForm : Form
        {
            public Form1()
            {
                InitializeComponent();
            } 
            private IntPtr TRUE;
            private IntPtr FALSE;
      
            protected override void OnHandleCreated(EventArgs e)
            {
                TRUE = Marshal.AllocHGlobal(sizeof(int));
                Marshal.WriteInt32(TRUE, 1);
                FALSE = Marshal.AllocHGlobal(sizeof(int));
                Marshal.WriteInt32(TRUE, 0); 
    
                EnableNCPaint(Handle);
                //DisableNCPaint(Handle);
                base.OnHandleCreated(e);
            } 
            private void EnableNCPaint(IntPtr hwnd)
            {  
                DwmSetWindowAttribute(hwnd, (int)DWMWINDOWATTRIBUTE.DWMWA_ALLOW_NCPAINT, TRUE, sizeof(int)); 
            }
            private void DisableNCPaint(IntPtr hwnd)
            { 
                DwmSetWindowAttribute(hwnd, (int)DWMWINDOWATTRIBUTE.DWMWA_ALLOW_NCPAINT, FALSE, sizeof(int)); 
            } 
            enum DWMNCRENDERINGPOLICY
            {
                DWMNCRP_USEWINDOWSTYLE,
                DWMNCRP_DISABLED,
                DWMNCRP_ENABLED,
                DWMNCRP_LAST
            };
            [Flags]
            enum DWMWINDOWATTRIBUTE : uint
            {
                DWMWA_NCRENDERING_ENABLED = 1,
                DWMWA_NCRENDERING_POLICY,
                DWMWA_TRANSITIONS_FORCEDISABLED,
                DWMWA_ALLOW_NCPAINT,
                DWMWA_CAPTION_BUTTON_BOUNDS,
                DWMWA_NONCLIENT_RTL_LAYOUT,
                DWMWA_FORCE_ICONIC_REPRESENTATION,
                DWMWA_FLIP3D_POLICY,
                DWMWA_EXTENDED_FRAME_BOUNDS,
                DWMWA_HAS_ICONIC_BITMAP,
                DWMWA_DISALLOW_PEEK,
                DWMWA_EXCLUDED_FROM_PEEK,
                DWMWA_CLOAK,
                DWMWA_CLOAKED,
                DWMWA_FREEZE_REPRESENTATION,
                DWMWA_LAST
            };
            [DllImport("dwmapi.dll")]
            private static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, IntPtr pvAttribute, int cbAttribute);
    
            [DllImport("dwmapi.dll")]
            private static extern int DwmSetWindowAttribute(IntPtr hwnd, int dwAttribute, IntPtr pvAttribute, int cbAttribute); 
        }
    }