本片博客主要讲的事最近总结的一些关于窗体的自定义:
1.首先创建一个BaseForm窗体,通过设置属性FormBorderStyle=None为无边框窗体,代码如下:
public partial class BaseForm : Form
{
// 图片名称
public const String IMG_MIN = "btn_min";
public const String IMG_MAX = "btn_max";
public const String IMG_RESTORE = "btn_restore";
public const String IMG_CLOSE = "btn_close";
public const String IMG_BG = "img_bg";
{
// 图片名称
public const String IMG_MIN = "btn_min";
public const String IMG_MAX = "btn_max";
public const String IMG_RESTORE = "btn_restore";
public const String IMG_CLOSE = "btn_close";
public const String IMG_BG = "img_bg";
// 图片缓存
private Bitmap closeBmp = null;
private Bitmap minBmp = null;
private Bitmap maxBmp = null;
private Bitmap restoreBmp = null;
private Bitmap closeBmp = null;
private Bitmap minBmp = null;
private Bitmap maxBmp = null;
private Bitmap restoreBmp = null;
public BaseForm()
{
InitializeComponent();
initForm();
}
{
InitializeComponent();
initForm();
}
// 初始化界面的方法
private void initForm()
{
// 获取最大化、最小化、关闭的背景图片
this.minBmp = ResUtils.GetResAsImage(IMG_MIN);
this.maxBmp = ResUtils.GetResAsImage(IMG_MAX);
this.closeBmp = ResUtils.GetResAsImage(IMG_CLOSE);
this.restoreBmp = ResUtils.GetResAsImage(IMG_RESTORE);
private void initForm()
{
// 获取最大化、最小化、关闭的背景图片
this.minBmp = ResUtils.GetResAsImage(IMG_MIN);
this.maxBmp = ResUtils.GetResAsImage(IMG_MAX);
this.closeBmp = ResUtils.GetResAsImage(IMG_CLOSE);
this.restoreBmp = ResUtils.GetResAsImage(IMG_RESTORE);
// 设置Tip提示信息
this.TipMain.SetToolTip(this.BtnClose, "关闭");
this.TipMain.SetToolTip(this.BtnMin, "最小化");
this.TipMain.SetToolTip(this.BtnMax, "最大化");
if (this.WindowState == FormWindowState.Maximized)
{
this.TipMain.SetToolTip(this.BtnMax, "还原");
this.BtnMax.BackgroundImage = this.restoreBmp;
this.BtnMax.Invalidate();
}
}
this.TipMain.SetToolTip(this.BtnClose, "关闭");
this.TipMain.SetToolTip(this.BtnMin, "最小化");
this.TipMain.SetToolTip(this.BtnMax, "最大化");
if (this.WindowState == FormWindowState.Maximized)
{
this.TipMain.SetToolTip(this.BtnMax, "还原");
this.BtnMax.BackgroundImage = this.restoreBmp;
this.BtnMax.Invalidate();
}
}
// 重写窗体消息轮询方法(不熟悉的可以搜索下win32api)
//protected void WndProc2(ref Message m){//TODO下篇}
//protected void WndProc2(ref Message m){//TODO下篇}
// 鼠标单击(这里为节省代码篇幅将最小化、最大化、关闭的事件统一处理,也可以分单处理)
private void BtnWnd_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (sender == this.BtnClose)
{
this.Close();
}
else if (sender == this.BtnMax)
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Maximized;
this.BtnMax.BackgroundImage = this.restoreBmp;
private void BtnWnd_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (sender == this.BtnClose)
{
this.Close();
}
else if (sender == this.BtnMax)
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Maximized;
this.BtnMax.BackgroundImage = this.restoreBmp;