티스토리 뷰

C#

C#: WinForm TitleBar에 DarkMode 적용하기

개태형님 2021. 2. 20. 20:30

DWM(Desktop Window Manager)를 통하여 TitleBar에 DarkMode를 적용할 수 있다.

 

1. 적용 전

 

2. 적용 후

 

 

    public class DWM
    {
        private const int DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19;
        private const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;

        [DllImport("dwmapi.dll")]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

        private bool IsWindows10OrGreater(int build = -1)
        {
            var version = Environment.OSVersion.Version;
            return version.Major >= 10 && version.Build >= build;
        }

        public void SetTheme(Form form, bool isDarkMode)
        {
            if (IsWindows10OrGreater(17763))
            {
                int attribute = DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1;
                if (IsWindows10OrGreater(18985))
                {
                    attribute = DWMWA_USE_IMMERSIVE_DARK_MODE;
                }

                int useImmersiveDarkMode = isDarkMode ? 1 : 0;
                DwmSetWindowAttribute(form.Handle, attribute, ref useImmersiveDarkMode, sizeof(int));
            }
        }
    }
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday