using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace 无边框窗体_类封装
{
/// <summary>
/// 名称:无边框窗体类
/// 功能:实现无边框窗体风格界面
/// 用法:
/// 1、项目中添加类文件NoneStyleForm.cs,内容为本文件
/// 2、修改命名空间为解决方案相同名称
/// 3、修改类名为程序窗体类相同名称
/// 4、窗体初始化函数中调用InitNoneStyleForm ( ),在这个函数中可以进一步设置窗体初始化参数。
/// 5、在布局中设置底层容器控件的位置与大小
/// 6、在颜色中设置控件的背景颜色 /// </summary>
public partial class Form1
{
#region 无边框窗体
#region 初始化
private void InitNoneStyleForm ( )
{
//生成标题栏控件组
InitializeFormComponent();
//设置无边框
FormBorderStyle = FormBorderStyle.None;
//设置是否可拖动大小
IsFormSizeable = true;
//设置背景颜色
FormColor(Color.DodgerBlue);
//FormColor(Color.FromArgb(Properties.Settings.Default.BackColorOfRGB));
//减少闪烁
//SetStyle(ControlStyles.SupportsTransparentBackColor , true);
//SetStyle(ControlStyles.AllPaintingInWmPaint , true);
//SetStyle(ControlStyles.UserPaint , true);
//双缓冲
SetStyle(ControlStyles.DoubleBuffer , true);
SetStyle(ControlStyles.ResizeRedraw , true);
//this.Refresh();
//其它
CompenentsDragForm.bindControl(labelFormTitle);//控件带动窗体
//Toolbar1.Renderer = new ToolstripStyle();//工具样样式
//Toolbar2.Renderer = new ToolstripStyle();//
}
#endregion
#region 布局
/// <summary>
/// 窗体底层控件/容器的布局 须手动定制
/// </summary>
private void CompenentsLayout ( )
{
#region 标题区(不须修改)
//最小化
panelFormMin.BackColor = Color.Yellow;
panelFormMin.Top = 0;
panelFormMin.Left = this.Width - 66;
panelFormMin.Width = 20;
panelFormMin.Height = 10;
//最大化
panelFormMax.BackColor = Color.Green;
panelFormMax.Top = 0;
panelFormMax.Left = this.Width - 46;
panelFormMax.Width = 20;
panelFormMax.Height = 10;
//关闭
panelFormClose.BackColor = Color.Red;
panelFormClose.Top = 0;
panelFormClose.Left = this.Width - 26;
panelFormClose.Width = 20;
panelFormClose.Height = 10;
//选色
panelFormColor.BackColor = this.BackColor;
panelFormColor.Top = 0;
panelFormColor.Left = 0;
panelFormColor.Width = 20;
panelFormColor.Height = 10;
//标题
labelFormTitle.BackColor = this.BackColor;
labelFormTitle.Top = 10;
labelFormTitle.Left = 20;
labelFormTitle.AutoSize = true;
#endregion
#region 主体区(须定制)
////工具栏
//Toolbar1.Top = 32;
//Toolbar1.Left = 8;
//Toolbar1.Width = this.Width - 16;
//Toolbar1.Height = 60;
////拆分窗
//splitContainer1.Top = 92;
//splitContainer1.Left = 8;
//splitContainer1.Width = this.Width - 16;
//splitContainer1.Height = this.Height - 100;
//Width = 623;//窗体大小
//Height = 370;
//tabs.Top = 32;
//tabs.Left = 8;
//tabs.Width = 607;
//tabs.Height = 332;
#endregion
}
#endregion
#region 拖放
/// <summary>
/// 窗体是否可以改变大小和双击最大化
/// </summary>
private bool IsFormSizeable = false;
/// <summary>
/// 拖放窗体 改变大小 双击最大化
/// </summary>
/// <param name="Msg"></param>
protected override void WndProc ( ref Message Msg )
{
const int WM_NCHITTEST = 0x0084;//移动鼠标,按住或释放鼠标时发生
const int WM_NCLBUTTONDBLCLK = 0xA3;//双击
const int HT_CLIENT = 0x01;
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;
if (!IsFormSizeable)
{
{
switch (Msg.Msg)
{
case 0x4e:
case 0xd:
case 0xe:
case 0x14:
base.WndProc(ref Msg);
break;
case WM_NCHITTEST://鼠标点任意位置后可以拖动窗体
this.DefWndProc(ref Msg);
if (Msg.Result.ToInt32() == HT_CLIENT)
{
Msg.Result = new IntPtr(HT_CAPTION);
return;
}
break;
case WM_NCLBUTTONDBLCLK://禁止双击最大化
Console.WriteLine(this.WindowState);
return;
default:
base.WndProc(ref Msg);
break;
}
}
}
else
{
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);
}
}
#endregion
#region 圆角
public void SetWindowRegion ( )
{
System.Drawing.Drawing2D.GraphicsPath FormPath;
FormPath = new System.Drawing.Drawing2D.GraphicsPath();
Rectangle rect = new Rectangle(0 , 0 , this.Width , this.Height);
FormPath = GetRoundedRectPath(rect , 10);
this.Region = new Region(FormPath);
}
private GraphicsPath GetRoundedRectPath ( Rectangle rect , int radius )
{
int diameter = radius;
Rectangle arcRect = new 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;
}
#endregion
#region 事件
/// <summary>
/// 最小化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMin_Click ( object sender , EventArgs e )
{
WindowState = FormWindowState.Minimized;
}
/// <summary>
/// 最大化与还原
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMax_Click ( object sender , EventArgs e )
{
if (WindowState == FormWindowState.Normal)
{
WindowState = FormWindowState.Maximized;
Invalidate();
}
else if (WindowState == FormWindowState.Maximized)
{
WindowState = FormWindowState.Normal;
Invalidate();
}
}
/// <summary>
/// 关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormClose_Click ( object sender , EventArgs e )
{
Close();
}
/// <summary>
/// 窗体重绘
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Resize ( object sender , EventArgs e )
{
SetWindowRegion();//圆角
CompenentsLayout();//布局
}
#endregion
#region 控件
private System.Windows.Forms.Panel panelFormMin;
private System.Windows.Forms.Panel panelFormMax;
private System.Windows.Forms.Panel panelFormClose;
private System.Windows.Forms.Panel panelFormColor;
private System.Windows.Forms.Label labelFormTitle;
/// <summary>
/// 标题栏控件自动生成
/// </summary>
private void InitializeFormComponent ( )
{
this.panelFormMin = new System.Windows.Forms.Panel();
this.panelFormMax = new System.Windows.Forms.Panel();
this.panelFormClose = new System.Windows.Forms.Panel();
this.panelFormColor = new System.Windows.Forms.Panel();
this.labelFormTitle = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// panelFormMin
//
this.panelFormMin.Location = new System.Drawing.Point(173 , 0);
this.panelFormMin.Name = "panel1";
this.panelFormMin.Size = new System.Drawing.Size(29 , 26);
this.panelFormMin.TabIndex = 0;
//
// panelFormMax
//
this.panelFormMax.Location = new System.Drawing.Point(208 , 0);
this.panelFormMax.Name = "panel2";
this.panelFormMax.Size = new System.Drawing.Size(29 , 26);
this.panelFormMax.TabIndex = 1;
//
// panelFormClose
//
this.panelFormClose.Location = new System.Drawing.Point(243 , 0);
this.panelFormClose.Name = "panel3";
this.panelFormClose.Size = new System.Drawing.Size(29 , 26);
this.panelFormClose.TabIndex = 1;
//
// panelFormColor
//
this.panelFormColor.Location = new System.Drawing.Point(2 , 0);
this.panelFormColor.Name = "panel4";
this.panelFormColor.Size = new System.Drawing.Size(29 , 26);
this.panelFormColor.TabIndex = 1;
//
// labelFormTitle
//
this.labelFormTitle.AutoSize = true;
this.labelFormTitle.Location = new System.Drawing.Point(37 , 9);
this.labelFormTitle.Name = "label1";
this.labelFormTitle.Size = new System.Drawing.Size(41 , 12);
this.labelFormTitle.TabIndex = 2;
this.labelFormTitle.Text = this.Text;
//
// Form1
//
this.Controls.Add(this.labelFormTitle);
this.Controls.Add(this.panelFormColor);
this.Controls.Add(this.panelFormMax);
this.Controls.Add(this.panelFormClose);
this.Controls.Add(this.panelFormMin);
this.ResumeLayout(false);
this.PerformLayout();
this.Resize += new System.EventHandler(MainForm_Resize);
panelFormMin.Click += new System.EventHandler(FormMin_Click);
panelFormMax.Click += new System.EventHandler(FormMax_Click);
panelFormClose.Click += new System.EventHandler(FormClose_Click);
panelFormColor.Click += new System.EventHandler(FormColor_Click);
}
#endregion
#region 颜色
/// <summary>
/// 改变窗体底色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormColor_Click ( object sender , EventArgs e )
{
ColorDialog cd = new ColorDialog(); //新建一个颜色选择窗口
cd.ShowDialog(); //让用户选择颜色
//Pen p = new Pen(cd.Color); //定义一个Pen,使用这种颜色
FormColor(cd.Color);
}
/// <summary>
/// 改变窗体底色
/// </summary>
/// <param name="c"></param>
private void FormColor ( Color c )
{
this.BackColor = c;
//Toolbar1.BackColor = this.BackColor;
//Toolbar2.BackColor = this.BackColor;
panelFormColor.BackColor = this.BackColor;
labelFormTitle.BackColor = this.BackColor;
//labelFormTitle.ForeColor = Color.White;
}
#endregion
#endregion
}
#region 相关类封装
#region CompenentsDragForm类
//控件带动窗体移动,可绑定控件及其子控件,用法:在窗体初始化时bindControl(toolStrip1)。
public class CompenentsDragForm
{
private static bool isMouseDown = false;
private static Point mouseOffset;
private static Form _form;
public CompenentsDragForm ( )
{
}
public static void bindControl ( Control control )
{
if (control == null)
{
return;
}
_form = control.FindForm();
control.MouseMove += new MouseEventHandler(control_MouseMove);
control.MouseDown += new MouseEventHandler(control_MouseDown);
control.MouseUp += new MouseEventHandler(control_MouseUp);
}
private static void control_MouseDown ( object sender , MouseEventArgs e )
{
if (e.Button == MouseButtons.Left)
{
Control control = sender as Control;
int offsetX = -e.X;
int offsetY = -e.Y;
//判断是窗体还是控件,从而改进鼠标相对于窗体的位置
if (!(control is System.Windows.Forms.Form))
{
offsetX = offsetX - control.Left;
offsetY = offsetY - control.Top;
}
//判断窗体有没有标题栏,从而改进鼠标相对于窗体的位置
if (_form.FormBorderStyle != FormBorderStyle.None)
{
offsetX = offsetX - SystemInformation.FrameBorderSize.Width;
offsetY = offsetY - SystemInformation.FrameBorderSize.Height - SystemInformation.CaptionHeight;
}
mouseOffset = new Point(offsetX , offsetY);
isMouseDown = true;
}
}
private static void control_MouseMove ( object sender , MouseEventArgs e )
{
if (isMouseDown)
{
Point mouse = Control.MousePosition;
mouse.Offset(mouseOffset.X , mouseOffset.Y);
_form.Location = mouse;
}
}
private static void control_MouseUp ( object sender , MouseEventArgs e )
{
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
}
#endregion
#region ToolstripStyle类
/// <summary>
/// 使用系统工具栏样式,用法:窗体初始化时 Toolbar1.Renderer = new ToolstripStyle();
/// </summary>
public class ToolstripStyle : ToolStripSystemRenderer
{
protected override void OnRenderToolStripBorder ( ToolStripRenderEventArgs e )
{
//base.OnRenderToolStripBorder(e);
}
}
#endregion
#endregion
}
|