C#快速创建自定义样式的窗体
程序员文章站
2022-06-08 22:18:22
...
winform开发时, 很多时候都需要创建非默认样式的窗体(因为很丑),而创建非默认样式的窗体又相当耗费时间。
为了节省时间,加快开发速度,这边将用过的一些代码,以及做自定义窗体过程中的一些思路整合了起来,做成一个工具类,其中包含设置边框,设置窗体的拖拽移动,设置最小化/关闭按钮等功能, 可以在一两分钟之内完成一个不错的自定义窗体。
下面是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
namespace Style
{
/// <summary>
///设定窗口边框(包括关闭及最小化),以及窗口移动。
/// </summary>
public class Border
{
#region 私有字段
/// <summary>
/// 边框对象现在操作的窗体
/// </summary>
Form form;
/// <summary>
/// 指示是否启用拖放
/// </summary>
private bool dragEnable = true;
/// <summary>
/// 用于关闭的label
/// </summary>
Label closeLabel = new Label();
/// <summary>
/// 用于最小化的lable
/// </summary>
Label miniLabel = new Label();
/// <summary>
/// 包含4个pictureBox边框的集合
/// </summary>
PictureBox[] picArray;
/// <summary>
/// 边框宽
/// </summary>
int borderWidth;
#endregion
#region 属性
/// <summary>
/// 指示是否启用拖放
/// </summary>
public bool DragEnable
{
get { return this.dragEnable; }
set { this.dragEnable = value; }
}
/// <summary>
/// 用于关闭的label
/// </summary>
public Label CloseLabel
{
get { return closeLabel; }
set { closeLabel = value; }
}
/// <summary>
/// 用于最小化的lable
/// </summary>
public Label MiniLabel
{
get { return miniLabel; }
set { miniLabel = value; }
}
/// <summary>
/// 边框宽
/// </summary>
public int BorderWidth
{
get { return borderWidth; }
set { borderWidth = value; }
}
/// <summary>
/// 包含4个pictureBox边框的集合
/// </summary>
public PictureBox[] PicArray
{
get { return picArray; }
set { picArray = value; }
}
#endregion
#region 公共方法
/// <summary>
/// 为面板设置窗体拖动效果
/// </summary>
/// <param name="panel"></param>
/// <param name="form"></param>
public void SetDragMove(Panel panel, Form form)
{
this.form = form;
panel.MouseDown += panel_MouseDown;
}
/// <summary>
/// 为窗体设置拖动效果
/// </summary>
/// <param name="form"></param>
public void SetFormDragMove(Form form) {
this.form = form;
form.MouseDown += form_MouseDown;
}
/// <summary>
/// 为窗体设置一个条式边框,好处是兼容性高,不影响其他。必须通过方法才能做到
/// </summary>
/// <param name="form"></param>
/// <param name="width"></param>
/// <param name="color"></param>
public void SetBoder(Form form, int width, Color color)
{
this.borderWidth = width;
PictureBox pic1 = new PictureBox();
PictureBox pic2 = new PictureBox();
PictureBox pic3 = new PictureBox();
PictureBox pic4 = new PictureBox();
picArray = new PictureBox[] { pic1, pic2, pic3, pic4 };
SetPicBorder(picArray, form, width);
foreach (PictureBox item in picArray)
{
item.BackColor = color;
form.Controls.Add(item);
item.BringToFront();
}
}
/// <summary>
/// 重设边框,如果未设置边框,则返回false
/// </summary>
/// <param name="form"></param>
/// <param name="width"></param>
public bool ResetBoder(Form form, int width)
{
if (picArray == null)
{
return false;
}
SetPicBorder(picArray, form, width);
//前置4个pictureBox
foreach (PictureBox item in picArray)
{
item.BringToFront();
}
return true;
}
/// <summary>
/// 设置一个lable形式的最小化按钮
/// </summary>
/// <param name="form"></param>
public void SetMiniLabel(Form form)
{
miniLabel.AutoSize = true;
miniLabel.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
miniLabel.ForeColor = System.Drawing.Color.Gray;
miniLabel.Name = "borderStyle-miniLabel";
miniLabel.Size = new System.Drawing.Size(20, 20);
miniLabel.Location = new Point(form.Width - miniLabel.Width - closeLabel.Width - 5, 7);
miniLabel.Text = "-";
miniLabel.BackColor = Color.Transparent;
miniLabel.Cursor = Cursors.Hand;
form.Controls.Add(miniLabel);
miniLabel.BringToFront();
miniLabel.Click += MinimizeForm;
}
/// <summary>
/// 重设lable形式的最小化按钮的位置
/// </summary>
public void ResetMiniLabel()
{
miniLabel.Location = new Point(form.Width - miniLabel.Width - closeLabel.Width - 5, 7);
}
/// <summary>
/// 设置一个Label形式的关闭按钮
/// </summary>
/// <param name="form"></param>
public void SetCloseLabel(Form form)
{
SetCloseLabel(form, false);
}
/// <summary>
/// 设置一个Label形式的关闭按钮
/// </summary>
/// <param name="form">窗体</param>
/// <param name="isHide">是否隐藏</param>
public void SetCloseLabel(Form form, bool isHide)
{
closeLabel = new Label();
closeLabel.AutoSize = true;
closeLabel.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
closeLabel.ForeColor = System.Drawing.Color.Gray;
closeLabel.Name = "borderStyle-closeLabel";
closeLabel.Size = new System.Drawing.Size(25, 15); //25,15
//需要width
closeLabel.Location = new Point(form.Width - closeLabel.Width - BorderWidth, 5);
closeLabel.Text = "×";
closeLabel.BackColor = Color.Transparent;
closeLabel.Cursor = Cursors.Hand;
form.Controls.Add(closeLabel);
closeLabel.BringToFront();
if (isHide)
{
closeLabel.Click += HideForm;
}
else {
closeLabel.Click += CloseForm;
}
}
/// <summary>
/// 重置Label形式的关闭按钮的位置
/// </summary>
public void ResetCloseLabel()
{
closeLabel.Location = new Point(form.Width - closeLabel.Width - BorderWidth, 5);
}
#endregion
#region 私有方法
[DllImport("user32.dll")]
private static extern bool ReleaseCapture();
[DllImport("user32.dll")]
private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
//面板的窗体拖动效果
private void panel_MouseDown(object sender, MouseEventArgs e)
{
if (this.dragEnable == true)
{
ReleaseCapture();
SendMessage(this.form.Handle, 0x112, 0xf012, 0);
}
}
//窗体的拖动效果
private static void form_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage((sender as Form).Handle, 0x112, 0xf012, 0);
}
/// <summary>
/// 对边框的4个pictureBox进行设置
/// </summary>
/// <param name="picArray">包含四个pic的数组</param>
private void SetPicBorder(PictureBox[] picArray, Form form, int width)
{
PictureBox pic1 = picArray[0];
PictureBox pic2 = picArray[1];
PictureBox pic3 = picArray[2];
PictureBox pic4 = picArray[3];
pic1.Size = new Size(form.Width, width);
pic2.Size = new Size(width, form.Height);
pic3.Size = new Size(form.Width, width);
pic4.Size = new Size(width, form.Height);
pic1.Location = new Point(0, 0);
pic2.Location = new Point(form.Width - width, 0);
pic3.Location = new Point(0, form.Height - width);
pic4.Location = new Point(0, 0);
}
//最小化窗体
private static void MinimizeForm(object sender, EventArgs e)
{
(sender as Control).FindForm().WindowState = FormWindowState.Minimized;
}
//隐藏窗体
private static void HideForm(object sender, EventArgs e)
{
(sender as Control).FindForm().Hide();
}
//关闭窗体
private static void CloseForm(object sender, EventArgs e)
{
(sender as Control).FindForm().Close();
}
#endregion
}
}
效果图:
边框的颜色、宽度,窗体可以拖拽的部位都可以在开始设置。
使用范例:
Border border;
private void LoginForm_Load(object sender, EventArgs e)
{
border = new Border();
border.SetFormDragMove(this);
border.SetBoder(this, 1, Color.Gray);
border.SetCloseLabel(this, true); //设置一个点击后隐藏窗体的关闭标签
border.SetMiniLabel(this);
}
使用时可以将相应代码复制到窗体的load事件中。
上一篇: php获取后台Job管理的实现代码