C#自定义Winform无边框窗体
在实际项目中,WinForm窗体或者控件不能满足要求,所以就需要自己设计窗体等,当然设计界面可以用的东西很多,例如WPF、或者一些第三方的库等。本例中将采用WinForm设计一个扁平美观的窗体。
上一篇中我们制作了一个button按钮控件,刚好本例可采用
需要的可以参考:C#自定义Button按钮控件
窗体效果:
接下来就是窗体的设计
1.添加一个窗体继承原来的窗体Form
public partial class FormEX : Form
2.添加窗体属性
/// <summary>
/// 是否允许最大化
/// </summary>
private bool maxVisible = true;
[Description("是否允许最大化")]
public bool MaxVisible
{
get { return maxVisible; }
set
{
maxVisible = value;
if (!maxVisible)
{
this.btnEXMin.Location = new System.Drawing.Point(this.btnEXMax.Location.X, );
this.btnEXMax.Visible = false;
}
else
{
this.btnEXMin.Location = new System.Drawing.Point(this.btnEXMax.Location.X - , );
this.btnEXMax.Visible = true;
}
}
} /// <summary>
/// 窗体标题
/// </summary>
private string titleText;
[Description("窗体标题")]
public string TitleText
{
get { return titleText; }
set
{
titleText = value;
title.Text = titleText; }
}
/// <summary>
/// 窗体标题是否显示
/// </summary>
private bool titleVisible = true;
[Description("窗体标题是否显示")]
public bool TitleVisible
{
get { return titleVisible; }
set
{
titleVisible = value;
title.Visible = titleVisible;
}
} /// <summary>
/// 窗口默认大小
/// FormSize.NORMAL OR FormSize.MAX
/// </summary>
private FormSize defaultFormSize = FormSize.NORMAL;
[Description("窗口默认大小")]
public FormSize DefaultFormSize
{
get { return defaultFormSize; }
set
{
defaultFormSize = value;
if (defaultFormSize == FormSize.MAX)
{
//防止遮挡任务栏
this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
this.WindowState = FormWindowState.Maximized;
//最大化图标切换
this.btnEXMax.ImageDefault = global::BenNHControl.Properties.Resources.MaxNormal;
}
}
}
3.窗体大小*更改
const int WM_NCHITTEST = 0x0084;
const int HTLEFT = ; //左边界
const int HTRIGHT = ; //右边界
const int HTTOP = ; //上边界
const int HTTOPLEFT = ; //左上角
const int HTTOPRIGHT = ; //右上角
const int HTBOTTOM = ; //下边界
const int HTBOTTOMLEFT = 0x10; //左下角
const int HTBOTTOMRIGHT = ; //右下角
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCHITTEST:
base.WndProc(ref m);
Point vPoint = new Point((int)m.LParam & 0xFFFF,
(int)m.LParam >> & 0xFFFF);
vPoint = PointToClient(vPoint);
if (vPoint.X <= )
if (vPoint.Y <= )
m.Result = (IntPtr)HTTOPLEFT;
else if (vPoint.Y >= ClientSize.Height - )
m.Result = (IntPtr)HTBOTTOMLEFT;
else m.Result = (IntPtr)HTLEFT;
else if (vPoint.X >= ClientSize.Width - )
if (vPoint.Y <= )
m.Result = (IntPtr)HTTOPRIGHT;
else if (vPoint.Y >= ClientSize.Height - )
m.Result = (IntPtr)HTBOTTOMRIGHT;
else m.Result = (IntPtr)HTRIGHT;
else if (vPoint.Y <= )
m.Result = (IntPtr)HTTOP;
else if (vPoint.Y >= ClientSize.Height - )
m.Result = (IntPtr)HTBOTTOM;
break;
default:
base.WndProc(ref m);
break;
}
}
4.窗体按钮等事件添加
/// <summary>
/// 最小化按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEXMin_ButtonClick(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
} /// <summary>
/// 最大化按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEXMax_ButtonClick(object sender, EventArgs e)
{
this.MaxNormalSwitch();
} /// <summary>
/// 关闭按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEXClose_ButtonClick(object sender, EventArgs e)
{
this.Close();
} /// <summary>
/// 鼠标按下标题栏
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void titleBar_MouseDown(object sender, MouseEventArgs e)
{
mPoint = new Point(e.X, e.Y);
} /// <summary>
/// 鼠标在移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void titleBar_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
}
} private void titleBar_DoubleClick(object sender, EventArgs e)
{
this.MaxNormalSwitch();
} /// <summary>
/// 最大化和正常状态切换
/// </summary>
private void MaxNormalSwitch()
{
if (this.WindowState == FormWindowState.Maximized)//如果当前状态是最大化状态 则窗体需要恢复默认大小
{
this.WindowState = FormWindowState.Normal;
//
this.btnEXMax.ImageDefault = global::BenNHControl.Properties.Resources.Max;
}
else
{
//防止遮挡任务栏
this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
this.WindowState = FormWindowState.Maximized;
//最大化图标切换
this.btnEXMax.ImageDefault = global::BenNHControl.Properties.Resources.MaxNormal;
}
this.Invalidate();//使重绘
} private void FormEX_Resize(object sender, EventArgs e)
{
this.Invalidate();//使重绘
}