自己定义的模仿窗体标题栏的控件

最近因为项目需要,软件客户端界面需要有换肤功能,但.net没有提供对窗体标题栏的修改。没有办法于是自己动手写了个模仿标题栏功能的控件,其中ICON和Title属性分别是窗体的标题栏文字和ICO图片的内容。(其中还用到了一个重载的button控件)
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SkinLib
{
 /// <summary>
 /// TitleBar 的摘要说明。
 /// </summary>
 public class TitleBar : System.Windows.Forms.UserControl
 {
  private System.Windows.Forms.ToolTip tipClose;
  private System.Windows.Forms.PictureBox vICON; 
  private System.Windows.Forms.Label lblTitle;
  private SkinLib.CKButton cBtnClose;
  private SkinLib.CKButton cBtnMax;
  private SkinLib.CKButton cBtnMin;
  private System.Windows.Forms.ToolTip tipMin;
  private System.Windows.Forms.ToolTip tipMax;

  public delegate void OnClosing(object sender, System.EventArgs args);  // 事件将用到的委托

  private System.ComponentModel.IContainer components;

  public TitleBar()
  {
   // 该调用是 Windows.Forms 窗体设计器所必需的。
   InitializeComponent();
   // TODO: 在 InitializeComponent 调用后添加任何初始化  
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  [Category("TitleBar")]
  public Image ICON
  {
   get
   {
    return vICON.BackgroundImage;
   }
   set
   {
    vICON.BackgroundImage=value;
   }
  }

  [Category("TitleBar")]
  public string Title
  {
   get
   {
    return lblTitle.Text;
   }
   set
   {
    lblTitle.Text=value;
   }
  } 
  
  [Category("TitleBar")]
  public event  OnClosing Closing; 

  #region 修改控件布局
  private void SetLayout()
  {
   // 检测控件是否超过最大高度
   if (this.Height>35)     
   {
    this.Height=35;
   }

   // 定义元素位置
   Point cBtnCtrl=new Point();

   cBtnCtrl.X=8;
   cBtnCtrl.Y=4;
   this.vICON.Location=cBtnCtrl;  // ICO位置

   cBtnCtrl.X=this.vICON.Location.X+this.vICON.Width;
   cBtnCtrl.Y=4;
   this.lblTitle.Location=cBtnCtrl; // 标题文字位置
   this.lblTitle.Width=this.Width-8-5-this.cBtnClose.Width; // 标题文字栏的宽度

   //cBtnCtrl.X=this.Width-8-5-this.cBtnClose.Width;
   //this.cBtnClose.Location=cBtnCtrl; // 关闭按钮位置
   //tipClose.SetToolTip(cBtnClose,"关闭");


   
   //Point cBtnCtrl=new Point();
   
   cBtnCtrl.X=this.Width-this.cBtnClose.Width-5-2;
   cBtnCtrl.Y=2;
   this.cBtnClose.Location=cBtnCtrl;

   cBtnCtrl.X=this.Width-(2*cBtnClose.Width)-5;
   this.cBtnMax.Location=cBtnCtrl;
   
   cBtnCtrl.X=this.Width-(3*cBtnClose.Width)-5;
   this.cBtnMin.Location=cBtnCtrl;

   tipClose.SetToolTip(cBtnClose,"关闭");   
   tipMin.SetToolTip(cBtnMin,"最小化");
   if (this.ParentForm.WindowState == FormWindowState.Maximized)
   {
    tipMax.SetToolTip(cBtnMax,"还原窗口");
   }
   else
   {
    tipMax.SetToolTip(cBtnMax,"最大化");
   }
   
  }
  #endregion

  #region win32API实现按住控件移动窗体
  // 引用windowsAPI
  [DllImport("user32.dll")]
  public static extern bool ReleaseCapture();
  [DllImport("user32.dll")]
  public static extern bool SendMessage(IntPtr hwnd, int wMsg,int wParam, int lParam);
  
  // 置windows消息常量
  public const int WM_SYSCOMMAND=0x0112;
  public const int SC_MOVE=0xF010;
  public const int HTCAPTION=0x0002;
  
  // 移动窗体
  private void SetWindowMove()
  {
   ReleaseCapture();
   SendMessage(this.ParentForm.Handle,WM_SYSCOMMAND,SC_MOVE+HTCAPTION,0);
  }
  #endregion

  #region 重载重绘事件
  protected override void OnPaint(PaintEventArgs e)
  {
   base.OnPaint (e);  

   if (this.BackgroundImage != null)
   {
    e.Graphics.Clear(Color.Transparent);
    e.Graphics.DrawImage(this.BackgroundImage,0,0,this.Width,this.Height);
   }
   
   SetLayout();
  }

  #endregion

  #region 组件设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器
  /// 修改此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.components = new System.ComponentModel.Container();
   this.tipClose = new System.Windows.Forms.ToolTip(this.components);
   this.vICON = new System.Windows.Forms.PictureBox();
   this.lblTitle = new System.Windows.Forms.Label();
   this.cBtnClose = new ICCE.SkinLib.CKButton();
   this.cBtnMax = new ICCE.SkinLib.CKButton();
   this.cBtnMin = new ICCE.SkinLib.CKButton();
   this.tipMin = new System.Windows.Forms.ToolTip(this.components);
   this.tipMax = new System.Windows.Forms.ToolTip(this.components);
   this.SuspendLayout();
   //
   // vICON
   //
   this.vICON.BackColor = System.Drawing.Color.Transparent;
   this.vICON.Location = new System.Drawing.Point(8, 4);
   this.vICON.Name = "vICON";
   this.vICON.Size = new System.Drawing.Size(16, 16);
   this.vICON.TabIndex = 0;
   this.vICON.TabStop = false;
   //
   // lblTitle
   //
   this.lblTitle.BackColor = System.Drawing.Color.Transparent;
   this.lblTitle.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
   this.lblTitle.Location = new System.Drawing.Point(24, 5);
   this.lblTitle.Name = "lblTitle";
   this.lblTitle.Size = new System.Drawing.Size(216, 24);
   this.lblTitle.TabIndex = 1;
   this.lblTitle.Text = "上海诚库信息科技有限公司---研发部";
   this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
   this.lblTitle.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lblTitle_MouseDown);
   //
   // cBtnClose
   //
   this.cBtnClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
   this.cBtnClose.BackColor = System.Drawing.Color.Transparent;
   this.cBtnClose.Cursor = System.Windows.Forms.Cursors.Hand;
   this.cBtnClose.ImgDown = null;
   this.cBtnClose.ImgNormal = null;
   this.cBtnClose.ImgOver = null;
   this.cBtnClose.IsTransparent = false;
   this.cBtnClose.Location = new System.Drawing.Point(352, 0);
   this.cBtnClose.Name = "cBtnClose";
   this.cBtnClose.Size = new System.Drawing.Size(24, 24);
   this.cBtnClose.TabIndex = 2;
   this.cBtnClose.Text = "X";
   this.cBtnClose.Click += new System.EventHandler(this.cBtnClose_Click);
   //
   // cBtnMax
   //
   this.cBtnMax.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
   this.cBtnMax.BackColor = System.Drawing.Color.Transparent;
   this.cBtnMax.Cursor = System.Windows.Forms.Cursors.Hand;
   this.cBtnMax.ImgDown = null;
   this.cBtnMax.ImgNormal = null;
   this.cBtnMax.ImgOver = null;
   this.cBtnMax.IsTransparent = false;
   this.cBtnMax.Location = new System.Drawing.Point(328, 0);
   this.cBtnMax.Name = "cBtnMax";
   this.cBtnMax.Size = new System.Drawing.Size(24, 24);
   this.cBtnMax.TabIndex = 3;
   this.cBtnMax.Text = "X";
   this.cBtnMax.Click += new System.EventHandler(this.cBtnMax_Click);
   //
   // cBtnMin
   //
   this.cBtnMin.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
   this.cBtnMin.BackColor = System.Drawing.Color.Transparent;
   this.cBtnMin.Cursor = System.Windows.Forms.Cursors.Hand;
   this.cBtnMin.ImgDown = null;
   this.cBtnMin.ImgNormal = null;
   this.cBtnMin.ImgOver = null;
   this.cBtnMin.IsTransparent = false;
   this.cBtnMin.Location = new System.Drawing.Point(304, 0);
   this.cBtnMin.Name = "cBtnMin";
   this.cBtnMin.Size = new System.Drawing.Size(24, 24);
   this.cBtnMin.TabIndex = 4;
   this.cBtnMin.Text = "X";
   this.cBtnMin.Click += new System.EventHandler(this.cBtnMin_Click);
   //
   // TitleBar
   //
   this.BackColor = System.Drawing.SystemColors.Control;
   this.Controls.Add(this.cBtnMin);
   this.Controls.Add(this.cBtnMax);
   this.Controls.Add(this.cBtnClose);
   this.Controls.Add(this.lblTitle);
   this.Controls.Add(this.vICON);
   this.Name = "TitleBar";
   this.Size = new System.Drawing.Size(384, 40);
   this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TitleBar_MouseDown);
   this.ResumeLayout(false);

  }
  #endregion

  #region 窗体拖运
  private void TitleBar_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   SetWindowMove();
  }

  private void lblTitle_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   SetWindowMove();
  }
  #endregion

  // 引发自定义事件
  private void cBtnClose_Click(object sender, System.EventArgs e)
  {
   if (Closing != null)
   {
    Closing(sender,e);
   }  
   
   Application.Exit();
  }

  private void cBtnMax_Click(object sender, System.EventArgs e)
  {
   if (this.ParentForm.WindowState != FormWindowState.Maximized)
   {
    this.ParentForm.WindowState=FormWindowState.Maximized;
   }
   else
   {
    this.ParentForm.WindowState=FormWindowState.Normal;
   }
  }

  private void cBtnMin_Click(object sender, System.EventArgs e)
  {
   this.ParentForm.WindowState=FormWindowState.Minimized;
  } 
  
 } 

}

posted @ 2006-02-13 14:23  小小点  阅读(541)  评论(0编辑  收藏  举报