public partial class Form3 : Form
{
const int WM_NCHITTEST = 0x0084;
const int HT_LEFT = 10;
const int HT_RIGHT = 11;
const int HT_TOP = 12;
const int HT_TOPLEFT = 13;
const int HT_TOPRIGHT = 14;
const int HT_BOTTOM = 15;
const int HT_BOTTOMLEFT = 16;
const int HT_BOTTOMRIGHT = 17;
const int HT_CAPTION = 2;
public Form3()
{
InitializeComponent();
rectRegion = new System.Drawing.Rectangle(0, 0, this.Width, this.Height);
//SetWindowRegion();
}
private System.Drawing.Rectangle rectRegion;
private static int x, y = 0;
public void SetWindowRegion()
{
System.Drawing.Drawing2D.GraphicsPath FormPath;
FormPath = new System.Drawing.Drawing2D.GraphicsPath();
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, this.Width, this.Height);
FormPath = GetRoundedRectPath(rect,10);
this.Region = new Region(FormPath);
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
System.Drawing.Drawing2D.GraphicsPath FormPath;
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, this.Width, this.Height);
FormPath = GetRoundedRectPath(rect, 10);
Pen mypen = new Pen(Color.Gray, 2);
g.DrawPath(mypen, FormPath);
base.OnPaint(e);
}
private GraphicsPath GetRoundedRectPath(System.Drawing.Rectangle rect, int radius)
{
int diameter = radius;
System.Drawing.Rectangle arcRect = new System.Drawing.Rectangle(rect.Location, new Size(diameter, diameter));
GraphicsPath path = new GraphicsPath();
// 左上角
path.AddArc(arcRect, 180, 90);
// 右上角
arcRect.X = rect.Right - diameter;
path.AddArc(arcRect, 270, 90);
// 右下角
arcRect.Y = rect.Bottom - diameter;
path.AddArc(arcRect, 0, 90);
// 左下角
arcRect.X = rect.Left;
path.AddArc(arcRect, 90, 90);
path.CloseFigure();//闭合曲线
return path;
}
protected override void WndProc(ref Message Msg)
{
if (Msg.Msg == WM_NCHITTEST)
{
//获取鼠标位置
int nPosX = (Msg.LParam.ToInt32() & 65535);
int nPosY = (Msg.LParam.ToInt32() >> 16);
//右下角
if (nPosX >= this.Right - 6 && nPosY >= this.Bottom - 6)
{
Msg.Result = new IntPtr(HT_BOTTOMRIGHT);
return;
}
//左上角
else if (nPosX <= this.Left + 6 && nPosY <= this.Top + 6)
{
Msg.Result = new IntPtr(HT_TOPLEFT);
return;
}
//左下角
else if (nPosX <= this.Left + 6 && nPosY >= this.Bottom - 6)
{
Msg.Result = new IntPtr(HT_BOTTOMLEFT);
return;
}
//右上角
else if (nPosX >= this.Right - 6 && nPosY <= this.Top + 6)
{
Msg.Result = new IntPtr(HT_TOPRIGHT);
return;
}
else if (nPosX >= this.Right - 2)
{
Msg.Result = new IntPtr(HT_RIGHT);
return;
}
else if (nPosY >= this.Bottom - 2)
{
Msg.Result = new IntPtr(HT_BOTTOM);
return;
}
else if (nPosX <= this.Left + 2)
{
Msg.Result = new IntPtr(HT_LEFT);
return;
}
else if (nPosY <= this.Top + 2)
{
Msg.Result = new IntPtr(HT_TOP);
return;
}
else
{
Msg.Result = new IntPtr(HT_CAPTION);
return;
}
}
base.WndProc(ref Msg);
}
private void Form3_Resize(object sender, EventArgs e)
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
this.Refresh();
//SetWindowRegion();
}
private void btnclose_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnMax_Click(object sender, EventArgs e)
{
this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
this.WindowState = FormWindowState.Maximized;
}
private void btnMin_Click(object sender, EventArgs e)
{
this.MinimumSize = new Size(rectRegion.Width, rectRegion.Height);
this.WindowState = FormWindowState.Normal;
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
x = e.X - 1;
y = e.Y - 1;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Left = Cursor.Position.X - x;
this.Top = Cursor.Position.Y - y;
}
}
}
Demo 下载地址<a target=_blank href="http://download.csdn.net/detail/u011470119/7589233">http://download.csdn.net/detail/u011470119/7589233</a>