#region
|
|
using System;
|
using System.ComponentModel;
|
using System.Drawing;
|
using System.Runtime.InteropServices;
|
using System.Windows.Forms;
|
|
#endregion
|
|
namespace CSFrameworkV5.Library.UIForm
|
{
|
public enum FormSize
|
{
|
NORMAL = 0, //正常大小
|
MAX = 1 //最大化
|
}
|
|
public partial class frmBaseUI : Form
|
{
|
private const int WM_NCHITTEST = 0x0084;
|
|
private const int HTLEFT = 10; //左边界
|
private const int HTRIGHT = 11; //右边界
|
private const int HTTOP = 12; //上边界
|
private const int HTTOPLEFT = 13; //左上角
|
private const int HTTOPRIGHT = 14; //右上角
|
private const int HTBOTTOM = 15; //下边界
|
private const int HTBOTTOMLEFT = 0x10; //左下角
|
private const int HTBOTTOMRIGHT = 17; //右下角
|
|
/// <summary>
|
/// 窗口默认大小
|
/// FormSize.NORMAL OR FormSize.MAX
|
/// </summary>
|
private FormSize defaultFormSize = FormSize.NORMAL;
|
|
/// <summary>
|
/// 是否允许最大化
|
/// </summary>
|
private bool maxVisible = true;
|
|
private Point mPoint;
|
|
/// <summary>
|
/// 窗体标题
|
/// </summary>
|
private string titleText;
|
|
/// <summary>
|
/// 窗体标题是否显示
|
/// </summary>
|
private bool titleVisible = true;
|
|
public frmBaseUI()
|
{
|
InitializeComponent();
|
}
|
|
[Description("窗口默认大小")]
|
public FormSize DefaultFormSize
|
{
|
get => defaultFormSize;
|
set
|
{
|
defaultFormSize = value;
|
if (defaultFormSize == FormSize.MAX)
|
{
|
//防止遮挡任务栏
|
MaximumSize = new Size(
|
Screen.PrimaryScreen.WorkingArea.Width,
|
Screen.PrimaryScreen.WorkingArea.Height);
|
WindowState = FormWindowState.Maximized;
|
//最大化图标切换
|
}
|
}
|
}
|
|
[Description("是否允许最大化")]
|
public bool MaxVisible
|
{
|
get => maxVisible;
|
set
|
{
|
maxVisible = value;
|
if (!maxVisible)
|
{
|
btnEXMin.Location = new Point(btnEXMax.Location.X, 12);
|
btnEXMax.Visible = false;
|
}
|
else
|
{
|
btnEXMin.Location = new Point(btnEXMax.Location.X - 20, 12);
|
btnEXMax.Visible = true;
|
}
|
}
|
}
|
|
[Description("窗体标题")]
|
public string TitleText
|
{
|
get => titleText;
|
set
|
{
|
titleText = value;
|
title.Text = titleText;
|
}
|
}
|
|
[Description("窗体标题是否显示")]
|
public bool TitleVisible
|
{
|
get => titleVisible;
|
set
|
{
|
titleVisible = value;
|
title.Visible = titleVisible;
|
}
|
}
|
|
/// <summary>
|
/// 关闭按钮事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void btnEXClose_ButtonClick(object sender, EventArgs e)
|
{
|
Close();
|
}
|
|
/// <summary>
|
/// 最大化按钮事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void btnEXMax_ButtonClick(object sender, EventArgs e)
|
{
|
MaxNormalSwitch();
|
}
|
|
/// <summary>
|
/// 最小化按钮事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void btnEXMin_ButtonClick(object sender, EventArgs e)
|
{
|
WindowState = FormWindowState.Minimized;
|
}
|
|
private void FormEX_Resize(object sender, EventArgs e)
|
{
|
Invalidate(); //使重绘
|
}
|
|
/// <summary>
|
/// 最大化和正常状态切换
|
/// </summary>
|
private void MaxNormalSwitch()
|
{
|
if (WindowState ==
|
FormWindowState.Maximized) //如果当前状态是最大化状态 则窗体需要恢复默认大小
|
{
|
WindowState = FormWindowState.Normal;
|
//
|
}
|
else
|
{
|
//防止遮挡任务栏
|
MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width,
|
Screen.PrimaryScreen.WorkingArea.Height);
|
WindowState = FormWindowState.Maximized;
|
//最大化图标切换
|
}
|
|
Invalidate(); //使重绘
|
}
|
|
private void titleBar_DoubleClick(object sender, EventArgs e)
|
{
|
MaxNormalSwitch();
|
}
|
|
/// <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)
|
Location = new Point(Location.X + e.X - mPoint.X,
|
Location.Y + e.Y - mPoint.Y);
|
}
|
|
#region 无边框窗体移动、放大、缩小
|
|
protected override void WndProc(ref Message m)
|
{
|
switch (m.Msg)
|
{
|
case WM_NCHITTEST:
|
base.WndProc(ref m);
|
var vPoint = new Point((int)m.LParam & 0xFFFF,
|
((int)m.LParam >> 16) & 0xFFFF);
|
vPoint = PointToClient(vPoint);
|
if (vPoint.X <= 5)
|
{
|
if (vPoint.Y <= 5)
|
m.Result = (IntPtr)HTTOPLEFT;
|
else if (vPoint.Y >= ClientSize.Height - 5)
|
m.Result = (IntPtr)HTBOTTOMLEFT;
|
else
|
m.Result = (IntPtr)HTLEFT;
|
}
|
else if (vPoint.X >= ClientSize.Width - 5)
|
{
|
if (vPoint.Y <= 5)
|
m.Result = (IntPtr)HTTOPRIGHT;
|
else if (vPoint.Y >= ClientSize.Height - 5)
|
m.Result = (IntPtr)HTBOTTOMRIGHT;
|
else
|
m.Result = (IntPtr)HTRIGHT;
|
}
|
else if (vPoint.Y <= 5)
|
{
|
m.Result = (IntPtr)HTTOP;
|
}
|
else if (vPoint.Y >= ClientSize.Height - 5)
|
{
|
m.Result = (IntPtr)HTBOTTOM;
|
}
|
|
break;
|
case WM_NCPAINT: //四周阴影 box shadow
|
if (m_aeroEnabled)
|
{
|
var v = 2;
|
DwmSetWindowAttribute(Handle, 2, ref v, 4);
|
var margins = new MARGINS
|
{
|
bottomHeight = 1,
|
leftWidth = 1,
|
rightWidth = 1,
|
topHeight = 1
|
};
|
DwmExtendFrameIntoClientArea(Handle, ref margins);
|
}
|
|
break;
|
}
|
|
base.WndProc(ref m);
|
|
if (m.Msg == WM_NCHITTEST &&
|
(int)m.Result == HTCLIENT) // drag the form
|
m.Result = (IntPtr)HTCAPTION;
|
}
|
|
#endregion
|
|
#region 窗体四周阴影
|
|
[DllImport("dwmapi.dll")]
|
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd,
|
ref MARGINS pMarInset);
|
|
[DllImport("dwmapi.dll")]
|
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr,
|
ref int attrValue, int attrSize);
|
|
[DllImport("dwmapi.dll")]
|
public static extern int DwmIsCompositionEnabled(ref int pfEnabled);
|
|
private bool m_aeroEnabled; // variables for box shadow
|
private const int CS_DROPSHADOW = 0x00020000;
|
private const int WM_NCPAINT = 0x0085;
|
private const int WM_ACTIVATEAPP = 0x001C;
|
|
public struct MARGINS // struct for box shadow
|
{
|
public int leftWidth;
|
public int rightWidth;
|
public int topHeight;
|
public int bottomHeight;
|
}
|
|
//private const int WM_NCHITTEST = 0x84; // variables for dragging the form
|
private const int HTCLIENT = 0x1;
|
private const int HTCAPTION = 0x2;
|
|
protected override CreateParams CreateParams
|
{
|
get
|
{
|
m_aeroEnabled = CheckAeroEnabled();
|
|
var cp = base.CreateParams;
|
if (!m_aeroEnabled) cp.ClassStyle |= CS_DROPSHADOW;
|
|
return cp;
|
}
|
}
|
|
private bool CheckAeroEnabled()
|
{
|
if (Environment.OSVersion.Version.Major >= 6)
|
{
|
var enabled = 0;
|
DwmIsCompositionEnabled(ref enabled);
|
return enabled == 1 ? true : false;
|
}
|
|
return false;
|
}
|
|
#endregion
|
}
|
}
|