- 1 year ago
How do I make my Form title bar follow the windows dark theme?
To make the title bar of your Windows Form follow the Windows dark theme, you can use the DwmExtendFrameIntoClientArea
function from the Windows API to extend the frame into the client area of your form. Here's an example of how you can achieve this in C#:
csharpusing System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public partial class MainForm : Form
{
// Import the necessary Windows API functions
[DllImport("dwmapi.dll")]
private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMargins);
[StructLayout(LayoutKind.Sequential)]
private struct MARGINS
{
public int Left;
public int Right;
public int Top;
public int Bottom;
}
public MainForm()
{
InitializeComponent();
// Call the DwmExtendFrameIntoClientArea function to extend the frame into the client area
MARGINS margins = new MARGINS { Left = -1, Right = -1, Top = -1, Bottom = -1 };
DwmExtendFrameIntoClientArea(this.Handle, ref margins);
}
}
This code snippet imports the DwmExtendFrameIntoClientArea
function from the dwmapi.dll
library using the DllImport
attribute. It also defines the MARGINS
struct that represents the margins of the extended frame.
In the constructor of your Form
class, you can create an instance of the MARGINS
struct with negative values for all sides to extend the frame into the client area. Finally, you call the DwmExtendFrameIntoClientArea
function passing in the form's handle (this.Handle
) and the MARGINS
struct.
When you run your application, the title bar of your form should follow the Windows dark theme. Note that this method requires Windows Vista or later and may not work correctly on all Windows versions or with custom window styles.