using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; using System.Text; using System.Runtime.InteropServices; namespace ControlLib.com { /// /// 主界面标题栏 /// public partial class FormTitleBar : UserControl { //换了背景要重新设置该值 public static int TITLEHEIGHT = 18; private Form m_ParentForm = null; public FormTitleBar(Form ParentForm) { InitializeComponent(); m_ParentForm = ParentForm; this.Width = m_ParentForm.Width+20; m_ParentForm.Resize += new EventHandler(ParentResize); FormTitleBar.TITLEHEIGHT = this.picBg.Height; } private void FormTitleBar_Load(object sender, EventArgs e) { Bitmap bit = (Bitmap)picBg.Image; Color c = bit.GetPixel(100, 10); lblTitle.BackColor = c; lblTitle.Text = m_ParentForm.Text; btnMax.Parent = picBg; btnRestore.Parent = picBg; lblTitle.Parent = picBg; lblTitle.BackColor = Color.Transparent; btnMin.Parent = picBg; btnClose.Parent = picBg; string sPath = System.Environment.CurrentDirectory + "/Images/formTitleBar/"; btnMin.Init(sPath + "min.png", sPath + "min_on.png"); btnMax.Init(sPath + "min_max.png", sPath + "min_max_on.png"); btnRestore.Init(sPath + "min_max.png", sPath + "min_max_on.png"); btnRestore.Visible = false; btnClose.Init(sPath + "colse.png", sPath + "close_on.png"); Reset(); } //设置几个按钮位置 private void Reset() { btnClose.Left = m_ParentForm.Width - 30; btnRestore.Left = m_ParentForm.Width - 60+3; btnMax.Left = m_ParentForm.Width - 60+3; btnMin.Left = m_ParentForm.Width - 90+3; } //主窗体发生尺寸改变 private void ParentResize(object sender, EventArgs e) { Reset(); } //最小化 private void btnMin_Click(object sender, EventArgs e) { m_ParentForm.WindowState = FormWindowState.Minimized; } //最大化 private void btnMax_Click(object sender, EventArgs e) { m_ParentForm.WindowState = FormWindowState.Maximized; btnRestore.Visible = true; btnMax.Visible = false; } //普通 private void btnRestore_Click(object sender, EventArgs e) { m_ParentForm.WindowState = FormWindowState.Normal; btnRestore.Visible = false; btnMax.Visible = true; } //关闭 private void btnClose_Click(object sender, EventArgs e) { m_ParentForm.Close(); } //双击(最大化和还原) private void picBg_DoubleClick(object sender, EventArgs e) { if(btnMax.Visible) btnMax_Click(null, null); else btnRestore_Click(null, null); } [DllImport("user32.dll")] public static extern void ReleaseCapture(); [DllImport("user32.dll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); //private const int WM_NCHITTEST = 0x84; //private const int HTCLIENT = 0x1; private const int HTCAPTION = 0x0002; private const int WM_SYSCOMMAND = 0x0112; private const int SC_MOVE = 0xF010; private long preTick = DateTime.Now.Ticks; private void picBg_MouseDown(object sender, MouseEventArgs e) { long curTick = DateTime.Now.Ticks; //0.5秒内连续两次单机视为双击 if ((curTick - preTick) < 500 * 10000) picBg_DoubleClick(null, null); preTick = curTick; ReleaseCapture(); SendMessage(m_ParentForm.Handle, WM_SYSCOMMAND, (IntPtr)(SC_MOVE + HTCAPTION), IntPtr.Zero); } } }