WinForm自定义窗体
来源:互联网 发布:铝合金门 知乎 编辑:程序博客网 时间:2024/06/06 03:42
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>
1 0
- WinForm自定义窗体
- C# WinForm自定义拖动窗体
- Winform自定义无边框窗体
- C# Winform 自定义鼠标拖动窗体类
- WinForm 自定义窗体的拖动方法
- winform中自定义的窗体移动代码
- Winform 使用panel 自定义拖动窗体
- WinForm窗体继承自定义的模板窗体出错
- C# Winform 窗体美化(十、自定义窗体)
- Winform自定义窗体与控件的外观形状
- Winform下ToolStrip承载自定义控件或 Windows 窗体控件。
- [转]winForm:利用窗体的自定义属性实现窗体传值
- winform窗体显示自定义位置,任意位置,窗体显示在屏幕下方
- 打印WinForm窗体,复制窗体。
- 打印 WinForm 窗体
- winform窗体添加背景音乐
- WinForm不规则窗体
- C# WinForm窗体间传值
- MSYS、MinGW、Cgywin
- app Widget 介绍及其例子一
- CSocket 和CAsyncSocket类介绍
- [LeetCode] Remove Duplicates from Sorted Array II
- JAVA学习篇--JSTL基础
- WinForm自定义窗体
- OSG中使用png图片显示透明效果
- Linux 内存详细信息
- java将异常堆栈转换为字符串
- ACM:回溯法,子集生成
- xcode环境变量
- QT在WIN7下开发环境的搭建
- Ubuntu Linux中vsFTP添加用户
- C语言中的static 详细分析