using AllBase; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace TemuKJD.BaseForms { public partial class LRBaseForm : BaseForm { const int WM_NCHITTEST = 0x0084; const int HTLEFT = 10; //左边界 const int HTRIGHT = 11; //右边界 const int HTTOP = 12; //上边界 const int HTTOPLEFT = 13; //左上角 const int HTTOPRIGHT = 14; //右上角 const int HTBOTTOM = 15; //下边界 const int HTBOTTOMLEFT = 0x10; //左下角 const int HTBOTTOMRIGHT = 17; //右下角 public bool Dragable { get; set; } = true; 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 >> 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; default: try { base.WndProc(ref m); } catch { } break; } this.Resize += LRBaseForm_Resize; } private void LRBaseForm_Resize(object sender, EventArgs e) { if (WindowState == FormWindowState.Maximized) { pbMaxBase.Visible = false; pbNormalBase.Visible = true; if (showMinimizedBox) pbMinBase.Visible = true; } else if (WindowState == FormWindowState.Minimized) { } else { if (showMaximizeBox) pbMaxBase.Visible = true; pbNormalBase.Visible = false; if (showMinimizedBox) pbMinBase.Visible = true; } if (!showMinimizedBox) this.pbMinBase.Visible = false; if (!showMaximizeBox) { this.pbMaxBase.Visible = false; this.pbNormalBase.Visible = false; } } public LRBaseForm() { InitializeComponent(); this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height); } private void LcBaseForm_Load(object sender, EventArgs e) { if (BaseClass.Ico != null) Iconx = Icon = BaseClass.Ico; else Iconx = this.Icon = BaseClass.Ico = ImageBuild.ConvertToIcon(global::TemuKJD.Properties.Resources.icon); if (!this.ShowIcon) { this.pbLogo.Visible = false; this.lbTitle.Location = new Point(this.pbLogo.Location.X, this.lbTitle.Location.Y); } if (this.ShowMaximizeBox) { if (this.WindowState == FormWindowState.Maximized) { this.pbMaxBase.Visible = false; this.pbNormalBase.Visible = true; } else { this.pbMaxBase.Visible = true; this.pbNormalBase.Visible = false; } } SetPanelVisible(this.TopLevel || DesignMode); } public void SetPanelVisible(bool visible) { if (visible) { this.Padding = new Padding(1); this.panel1.Visible = true; } else { this.Padding = new Padding(0); this.panel1.Visible = false; } } private void PictureBox2_Click(object sender, EventArgs e) { WindowState = FormWindowState.Minimized; } public virtual void PictureBox1_Click(object sender, EventArgs e) { //pictureBox3.Image?.Dispose(); Close(); } private Icon _iconx; public Icon Iconx { set { _iconx = value; if (_iconx == null) return; pbLogo.Image = _iconx.ToBitmap(); } } private bool showMinimizedBox = true; [Browsable(true), Description("显示最小化按钮"), Category("自定义分组")] public bool ShowMinimizedBox { get { return showMinimizedBox; } set { this.showMinimizedBox = value; } } private bool showMaximizeBox = false; [Browsable(true), Description("显示最大化按钮"), Category("自定义分组")] public bool ShowMaximizeBox { get { return showMaximizeBox; } set { showMaximizeBox = value; if (showMaximizeBox) { if (this.WindowState == FormWindowState.Maximized) { this.pbMaxBase.Visible = false; this.pbNormalBase.Visible = true; } else { this.pbMaxBase.Visible = true; this.pbNormalBase.Visible = false; } } else { this.pbMaxBase.Visible = false; this.pbNormalBase.Visible = false; } } } public string TitleText { get { return Text; } set { lbTitle.Text = Text = value; } } private Point mouseOffset;//记录鼠标指针的坐标 private bool isMouseDown;//记录鼠标左键是否按下 private void Panel1_MouseMove(object sender, MouseEventArgs e) { if (isMouseDown && Dragable) { //获取鼠标的位置 Point mousePos = MousePosition; mousePos.Offset(mouseOffset.X, mouseOffset.Y); Location = mousePos; } } private void Panel1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mouseOffset = new Point(-e.X, -e.Y); isMouseDown = true; } } private void Panel1_MouseUp(object sender, MouseEventArgs e) { isMouseDown = false; } private void PictureBox1_MouseHover(object sender, EventArgs e) { this.pbCloseBase.Image = global::TemuKJD.Properties.Resources.close_hover; this.Cursor = Cursors.Hand; } private void PictureBox1_MouseLeave(object sender, EventArgs e) { this.pbCloseBase.Image = global::TemuKJD.Properties.Resources.close; this.Cursor = Cursors.Default; } private void PictureBox2_MouseHover(object sender, EventArgs e) { this.pbMinBase.Image = global::TemuKJD.Properties.Resources.min_hover; this.Cursor = Cursors.Hand; } private void PictureBox2_MouseLeave(object sender, EventArgs e) { this.pbMinBase.Image = global::TemuKJD.Properties.Resources.min; this.Cursor = Cursors.Default; } public virtual void HideTitle() { this.panel1.Visible = false; } #region 窗体提交数据完成事件 public event Action SaveComplete; protected virtual void OnSaveComplete(object param) { this.SaveComplete?.Invoke(param); } public event Action SaveCompleteWithDescribe; protected virtual void OnSaveComplete(object param, object describe) { this.SaveCompleteWithDescribe?.Invoke(param, describe); } #endregion private void pbNormalBase_Click(object sender, EventArgs e) { WindowState = FormWindowState.Normal; this.pbMaxBase.Visible = true; this.pbNormalBase.Visible = false; } private void pbNormalBase_MouseHover(object sender, EventArgs e) { this.pbNormalBase.Image = global::TemuKJD.Properties.Resources.normal_hover; this.Cursor = Cursors.Hand; } private void pbNormalBase_MouseLeave(object sender, EventArgs e) { this.pbNormalBase.Image = global::TemuKJD.Properties.Resources.normal; this.Cursor = Cursors.Default; } private void pbMaxBase_Click(object sender, EventArgs e) { WindowState = FormWindowState.Maximized; this.pbMaxBase.Visible = false; this.pbNormalBase.Visible = true; } private void pbMaxBase_MouseHover(object sender, EventArgs e) { this.pbMaxBase.Image = global::TemuKJD.Properties.Resources.max_hover; this.Cursor = Cursors.Hand; } private void pbMaxBase_MouseLeave(object sender, EventArgs e) { this.pbMaxBase.Image = global::TemuKJD.Properties.Resources.max; this.Cursor = Cursors.Default; } private void lbTitle_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mouseOffset = new Point(-e.X, -e.Y); isMouseDown = true; } } private void lbTitle_MouseMove(object sender, MouseEventArgs e) { if (isMouseDown && Dragable) { var control = sender as Control; if (control == null) return; //获取鼠标的位置 Point mousePos = MousePosition; var x = mouseOffset.X; var y = mouseOffset.Y; while (control.Parent != null && control.Parent != this) { x -= control.Location.X; y -= control.Location.Y; control = control.Parent; } mousePos.Offset(x, y); Location = mousePos; } } private void lbTitle_MouseUp(object sender, MouseEventArgs e) { isMouseDown = false; } } }