Elshan
Elshan

Reputation: 7693

How to make windows form in c# without rounded corners

I want to make a windows form without rounded corners as below image. Are there any Windows API functions or any other methods to make it?

No rounded corners

I don't want to use IMAGES OR FormBorderStyle = none Properties.

WHEN REMOVE ROUNDED CORNERS,WINDOWS THEME COLOR SHOULD BE APPLY CORRECTLY.IT IS NOT BE WINDOWS CLASSIC MODE WINDOWS FORM

Upvotes: 1

Views: 2457

Answers (2)

egonrp
egonrp

Reputation: 29

Answer for 2022 / Windows 11.

More information here: https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/apply-rounded-corners

Sample Code - C# WinForms (more examples in link above):

using System.Runtime.InteropServices;

namespace WinFormsAppNet1
{
    public partial class Form1 : Form
    {    
        // START: Change window border
        // The enum flag for DwmSetWindowAttribute's second parameter, which tells the function what attribute to set.
        // Copied from dwmapi.h
        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.
        // Copied from dwmapi.h
        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, PreserveSig = false)]
        internal static extern void DwmSetWindowAttribute(IntPtr hwnd,
                                                         DWMWINDOWATTRIBUTE attribute,
                                                         ref DWM_WINDOW_CORNER_PREFERENCE pvAttribute,
                                                         uint cbAttribute);
        // END: Change window border

        public Form1()
        {
            InitializeComponent();

            // START: Change window border
            var attribute = DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE;
            var preference = DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_DONOTROUND; // DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND
            DwmSetWindowAttribute(this.Handle, attribute, ref preference, sizeof(uint));
            // END: Change window border
        }
    }
}

Upvotes: 2

Moha Dehghan
Moha Dehghan

Reputation: 18472

If all you want is to eliminate the rounded corders, you can set the Region property of the form:

this.Region = new Region(new Rectangle(0, 0, Width, Height));

Upvotes: 3

Related Questions