[WPF] C# Modify the windows border color.

Roy 21 Reputation points
2021-09-29T02:12:58.687+00:00

So I am currently on Windows 11 using the https://github.com/MahApps/MahApps.Metro Framework which by default makes the window squared like windows 10, so I saw a post by Microsoft here https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/apply-rounded-corners which applies rounded corners to an application. But my question is how can I go about modifying the gray border that is drawn by the system on my window, to perhaps a different color for example?

Below is my MainWindow.cs

 /// <summary>  
    /// Interaction logic for MainWindow.xaml  
    /// </summary>  
    public partial class MainWindow  
  
    {  
         
        #region Window Corner Round  
  
        public enum DWMWINDOWATTRIBUTE  
        {  
            DWMWA_WINDOW_CORNER_PREFERENCE = 33  
        }  
  
        // The DWM_WINDOW_CORNER_PREFERENCE enum for DwmSetWindowAttribute's third parameter, which tells the function  
        // what value of the enum to set.  
        public enum DWM_WINDOW_CORNER_PREFERENCE  
        {  
            DWMWCP_DEFAULT = 0,  
            DWMWCP_DONOTROUND = 1,  
            DWMWCP_ROUND = 2,  
            DWMWCP_ROUNDSMALL = 3  
        }  
  
        // Import dwmapi.dll and define DwmSetWindowAttribute in C# corresponding to the native function.  
        [DllImport("dwmapi.dll", CharSet = CharSet.Unicode, SetLastError = true)]  
        private static extern long DwmSetWindowAttribute(IntPtr hwnd,  
            DWMWINDOWATTRIBUTE attribute,  
            ref DWM_WINDOW_CORNER_PREFERENCE pvAttribute,  
            uint cbAttribute);  
        #endregion  
        public MainWindow()  
        {  
            InitializeComponent();  
  
            DataContext = this;  
  
            #region Window Rounding Initialization  
  
            IntPtr hWnd = new WindowInteropHelper(GetWindow(this)).EnsureHandle();  
            var attribute = DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE;  
            var preference = DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND;  
            DwmSetWindowAttribute(hWnd, attribute, ref preference, sizeof(uint));  
  
            #endregion  
        }  
    }  

  

136085-border-color.png

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,795 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,110 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.