I’m making a borderless form based on WinForms with rounded corners on Windows 7. To make its edges are visible, I used the following code to drop a shadow on it. This does work, but I’m finding that there’s an almost transparent thing around the form that makes the rounded corners less noticeable.
private const int WM_NCPAINT = 0x0085;
private const int CS_DROPSHADOW = 0x00020000;
private void MainForm_SizeChanged(object sender, EventArgs e)
{
Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 30, 30));
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
if (!AeroEnabled)
{
cp.ClassStyle |= CS_DROPSHADOW;
}
return cp;
}
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCPAINT)
{
var v = 2;
DwmSetWindowAttribute(Handle, 2, ref v, 4);
MARGINS margins = new()
{
bottomHeight = 1,
leftWidth = 0,
rightWidth = 0,
topHeight = 0
};
DwmExtendFrameIntoClientArea(Handle, ref margins);
}
base.WndProc(ref m);
}
[DllImport("dwmapi.dll")]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
[DllImport("gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
public static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
public struct MARGINS
{
public int leftWidth;
public int rightWidth;
public int topHeight;
public int bottomHeight;
}
The one on the left is my form. The one on the right is the system window, which has a more rounded shadow. On the yellow background it’s obvious that there’s something transparent around my form, and on the white background it looks more like a right angle. So how can I create a rounded shadow? By the way, how to add a border to this borderless, rounded-corner form? I’ve tried most methods found online, but none of them work. Thanks in advance.