// ***********************************************************************
// Assembly : HZH_Controls
// Created : 08-08-2019
//
// ***********************************************************************
//
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
//
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace HZH_Controls.Forms
{
///
/// Class FrmBase.
/// Implements the
///
///
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))]
public partial class FrmBase : Form
{
///
/// Gets or sets the hot keys.
///
/// The hot keys.
[Description("定义的热键列表"), Category("自定义")]
public Dictionary HotKeys { get; set; }
///
/// Delegate HotKeyEventHandler
///
/// The string hot key.
/// true if XXXX, false otherwise.
public delegate bool HotKeyEventHandler(string strHotKey);
///
/// 热键事件
///
[Description("热键事件"), Category("自定义")]
public event HotKeyEventHandler HotKeyDown;
#region 字段属性
///
/// 失去焦点关闭
///
bool _isLoseFocusClose = false;
///
/// 是否重绘边框样式
///
private bool _redraw = false;
///
/// 是否显示圆角
///
private bool _isShowRegion = false;
///
/// 边圆角大小
///
private int _regionRadius = 10;
///
/// 边框颜色
///
private Color _borderStyleColor;
///
/// 边框宽度
///
private int _borderStyleSize;
///
/// 边框样式
///
private ButtonBorderStyle _borderStyleType;
///
/// 是否显示模态
///
private bool _isShowMaskDialog = false;
///
/// 蒙版窗体
///
/// true if this instance is show mask dialog; otherwise, false.
[Description("是否显示蒙版窗体")]
public bool IsShowMaskDialog
{
get
{
return this._isShowMaskDialog;
}
set
{
this._isShowMaskDialog = value;
}
}
///
/// 边框宽度
///
/// The size of the border style.
[Description("边框宽度")]
public int BorderStyleSize
{
get
{
return this._borderStyleSize;
}
set
{
this._borderStyleSize = value;
}
}
///
/// 边框颜色
///
/// The color of the border style.
[Description("边框颜色")]
public Color BorderStyleColor
{
get
{
return this._borderStyleColor;
}
set
{
this._borderStyleColor = value;
}
}
///
/// 边框样式
///
/// The type of the border style.
[Description("边框样式")]
public ButtonBorderStyle BorderStyleType
{
get
{
return this._borderStyleType;
}
set
{
this._borderStyleType = value;
}
}
///
/// 边框圆角
///
/// The region radius.
[Description("边框圆角")]
public int RegionRadius
{
get
{
return this._regionRadius;
}
set
{
this._regionRadius = Math.Max(value, 1);
}
}
///
/// 是否显示自定义绘制内容
///
/// true if this instance is show region; otherwise, false.
[Description("是否显示自定义绘制内容")]
public bool IsShowRegion
{
get
{
return this._isShowRegion;
}
set
{
this._isShowRegion = value;
}
}
///
/// 是否显示重绘边框
///
/// true if redraw; otherwise, false.
[Description("是否显示重绘边框")]
public bool Redraw
{
get
{
return this._redraw;
}
set
{
this._redraw = value;
}
}
///
/// The is full size
///
private bool _isFullSize = true;
///
/// 是否全屏
///
/// true if this instance is full size; otherwise, false.
[Description("是否全屏")]
public bool IsFullSize
{
get { return _isFullSize; }
set { _isFullSize = value; }
}
///
/// 失去焦点自动关闭
///
/// true if this instance is lose focus close; otherwise, false.
[Description("失去焦点自动关闭")]
public bool IsLoseFocusClose
{
get
{
return this._isLoseFocusClose;
}
set
{
this._isLoseFocusClose = value;
}
}
#endregion
///
/// Gets a value indicating whether this instance is desing mode.
///
/// true if this instance is desing mode; otherwise, false.
private bool IsDesingMode
{
get
{
bool ReturnFlag = false;
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
ReturnFlag = true;
else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
ReturnFlag = true;
return ReturnFlag;
}
}
#region 初始化
///
/// Initializes a new instance of the class.
///
public FrmBase()
{
InitializeComponent();
base.SetStyle(ControlStyles.UserPaint, true);
base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
base.SetStyle(ControlStyles.DoubleBuffer, true);
//base.HandleCreated += new EventHandler(this.FrmBase_HandleCreated);
//base.HandleDestroyed += new EventHandler(this.FrmBase_HandleDestroyed);
this.KeyDown += FrmBase_KeyDown;
this.FormClosing += FrmBase_FormClosing;
}
///
/// Handles the FormClosing event of the FrmBase control.
///
/// The source of the event.
/// The instance containing the event data.
void FrmBase_FormClosing(object sender, FormClosingEventArgs e)
{
if (_isLoseFocusClose)
{
MouseHook.OnMouseActivity -= hook_OnMouseActivity;
}
}
///
/// Handles the Load event of the FrmBase control.
///
/// The source of the event.
/// The instance containing the event data.
private void FrmBase_Load(object sender, EventArgs e)
{
if (!IsDesingMode)
{
if (_isFullSize)
SetFullSize();
}
if (_isLoseFocusClose)
{
MouseHook.OnMouseActivity += hook_OnMouseActivity;
}
}
#endregion
#region 方法区
///
/// Handles the OnMouseActivity event of the hook control.
///
/// The source of the event.
/// The instance containing the event data.
void hook_OnMouseActivity(object sender, MouseEventArgs e)
{
try
{
if (this._isLoseFocusClose && e.Clicks > 0)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left || e.Button == System.Windows.Forms.MouseButtons.Right)
{
if (!this.IsDisposed)
{
if (!this.ClientRectangle.Contains(this.PointToClient(e.Location)))
{
base.Close();
}
}
}
}
}
catch { }
}
///
/// 全屏
///
public void SetFullSize()
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
///
/// Does the escape.
///
protected virtual void DoEsc()
{
base.Close();
}
///
/// Does the enter.
///
protected virtual void DoEnter()
{
}
///
/// 设置重绘区域
///
public void SetWindowRegion()
{
GraphicsPath path = new GraphicsPath();
Rectangle rect = new Rectangle(-1, -1, base.Width + 1, base.Height);
path = this.GetRoundedRectPath(rect, this._regionRadius);
base.Region = new Region(path);
}
///
/// 获取重绘区域
///
/// The rect.
/// The radius.
/// GraphicsPath.
private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
{
Rectangle rect2 = new Rectangle(rect.Location, new Size(radius, radius));
GraphicsPath graphicsPath = new GraphicsPath();
graphicsPath.AddArc(rect2, 180f, 90f);
rect2.X = rect.Right - radius;
graphicsPath.AddArc(rect2, 270f, 90f);
rect2.Y = rect.Bottom - radius;
rect2.Width += 1;
rect2.Height += 1;
graphicsPath.AddArc(rect2, 360f, 90f);
rect2.X = rect.Left;
graphicsPath.AddArc(rect2, 90f, 90f);
graphicsPath.CloseFigure();
return graphicsPath;
}
///
/// 将窗体显示为具有指定所有者的模式对话框。
///
/// 任何实现 (表示将拥有模式对话框的顶级窗口)的对象。
/// 值之一。
///
///
///
///
///
///
///
public new DialogResult ShowDialog(IWin32Window owner)
{
try
{
if (this._isShowMaskDialog && owner != null)
{
var frmOwner = (Control)owner;
FrmTransparent _frmTransparent = new FrmTransparent();
_frmTransparent.Width = frmOwner.Width;
_frmTransparent.Height = frmOwner.Height;
Point location = frmOwner.PointToScreen(new Point(0, 0));
_frmTransparent.Location = location;
_frmTransparent.frmchild = this;
_frmTransparent.IsShowMaskDialog = false;
return _frmTransparent.ShowDialog(owner);
}
else
{
return base.ShowDialog(owner);
}
}
catch (NullReferenceException)
{
return System.Windows.Forms.DialogResult.None;
}
}
///
/// 将窗体显示为模式对话框,并将当前活动窗口设置为它的所有者。
///
/// 值之一。
///
///
///
///
///
///
///
public new DialogResult ShowDialog()
{
return base.ShowDialog();
}
#endregion
#region 事件区
///
/// 关闭时发生
///
/// 一个包含事件数据的 。
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
if (base.Owner != null && base.Owner is FrmTransparent)
{
(base.Owner as FrmTransparent).Close();
}
}
///
/// 快捷键
///
/// 通过引用传递的 ,它表示要处理的 Win32 消息。
/// 值之一,它表示要处理的键。
/// 如果控件处理并使用击键,则为 true;否则为 false,以允许进一步处理。
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
int num = 256;
int num2 = 260;
bool result;
if (msg.Msg == num | msg.Msg == num2)
{
if (keyData == (Keys)262259)
{
result = true;
return result;
}
if (keyData != Keys.Enter)
{
if (keyData == Keys.Escape)
{
this.DoEsc();
}
}
else
{
this.DoEnter();
}
}
result = false;
if (result)
return result;
else
return base.ProcessCmdKey(ref msg, keyData);
}
///
/// Handles the KeyDown event of the FrmBase control.
///
/// The source of the event.
/// The instance containing the event data.
protected void FrmBase_KeyDown(object sender, KeyEventArgs e)
{
if (HotKeyDown != null && HotKeys != null)
{
bool blnCtrl = false;
bool blnAlt = false;
bool blnShift = false;
if (e.Control)
blnCtrl = true;
if (e.Alt)
blnAlt = true;
if (e.Shift)
blnShift = true;
if (HotKeys.ContainsKey(e.KeyValue))
{
string strKey = string.Empty;
if (blnCtrl)
{
strKey += "Ctrl+";
}
if (blnAlt)
{
strKey += "Alt+";
}
if (blnShift)
{
strKey += "Shift+";
}
strKey += HotKeys[e.KeyValue];
if (HotKeyDown(strKey))
{
e.Handled = true;
e.SuppressKeyPress = true;
}
}
}
}
///
/// 重绘事件
///
/// 包含事件数据的 。
protected override void OnPaint(PaintEventArgs e)
{
if (this._isShowRegion)
{
this.SetWindowRegion();
}
base.OnPaint(e);
if (this._redraw)
{
ControlPaint.DrawBorder(e.Graphics, base.ClientRectangle, this._borderStyleColor, this._borderStyleSize, this._borderStyleType, this._borderStyleColor, this._borderStyleSize, this._borderStyleType, this._borderStyleColor, this._borderStyleSize, this._borderStyleType, this._borderStyleColor, this._borderStyleSize, this._borderStyleType);
}
}
#endregion
#region 窗体拖动 English:Form drag
///
/// Releases the capture.
///
/// true if XXXX, false otherwise.
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
///
/// Sends the message.
///
/// The HWND.
/// The w MSG.
/// The w parameter.
/// The l parameter.
/// true if XXXX, false otherwise.
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
///
/// The wm syscommand
///
public const int WM_SYSCOMMAND = 0x0112;
///
/// The sc move
///
public const int SC_MOVE = 0xF010;
///
/// The htcaption
///
public const int HTCAPTION = 0x0002;
///
/// 通过Windows的API控制窗体的拖动
///
/// The HWND.
public static void MouseDown(IntPtr hwnd)
{
ReleaseCapture();
SendMessage(hwnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
#endregion
///
/// 在构造函数中调用设置窗体移动
///
/// The cs.
protected void InitFormMove(params Control[] cs)
{
foreach (Control c in cs)
{
if (c != null && !c.IsDisposed)
c.MouseDown += c_MouseDown;
}
}
///
/// Handles the MouseDown event of the c control.
///
/// The source of the event.
/// The instance containing the event data.
void c_MouseDown(object sender, MouseEventArgs e)
{
MouseDown(this.Handle);
}
}
}