C#中用Form实现浮动工具栏(转)_

来源:互联网 发布:ubuntu开机运行脚本 编辑:程序博客网 时间:2024/05/07 09:48
今天才发觉微软XX的在.net 2003里边没给提供浮动工具栏控件,没办法google了一下只找到几个第三方的控件,要付费不过功能倒是挺强悍。考虑一下偶只是需要一个简单的浮动条,不至于用到那些太多的牛x功能,自己动手丰衣足食吧!
    基本思路,用一个无text,ControlBox=false的窗体,设置其top属性为true作为该工具条的载体
    在窗体MouseDown,MouseMove,MouseUp事件中处理工具条的移动
 
代码:
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace SmartBoard
{
 /// <summary>
 /// Form1 
 /// </summary>
 public class NPButton : System.Windows.Forms.Form
 {
  protected SmartBoard ParentFrom = null;
  private int mouseX,mouseY,currX,currY;
  public System.Windows.Forms.Button button3;
  public System.Windows.Forms.Button button2;
  /// <summary>
  ///
  /// </summary>
  private System.ComponentModel.Container components = null;
  public NPButton(SmartBoard parent)
  {
   //
   // Windows
   //
   InitializeComponent();
   this.ParentFrom = parent;
   //
   // TODO: InitializeComponent 
   //
  }
  /// <summary>
  /// 
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
  #region Windows 
  /// <summary>
  /// 
  /// 
  /// </summary>
  private void InitializeComponent()
  {
   this.button3 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // button3
   //
   this.button3.BackColor = System.Drawing.Color.LightGray;
   this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
   this.button3.ForeColor = System.Drawing.Color.Black;
   this.button3.Location = new System.Drawing.Point(55, 5);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(50, 18);
   this.button3.TabIndex = 1;
   this.button3.Text = ">";
     //
   // button2
   //
   this.button2.BackColor = System.Drawing.Color.LightGray;
   this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
   this.button2.ForeColor = System.Drawing.Color.Black;
   this.button2.Location = new System.Drawing.Point(5, 5);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(50, 18);
   this.button2.TabIndex = 0;
   this.button2.Text = "<";
    //
   // NPButton
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
   this.BackColor = System.Drawing.SystemColors.Control;
   this.ClientSize = new System.Drawing.Size(110, 30);
   this.ControlBox = false;
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button3);
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
   this.Name = "NPButton";
   this.ShowInTaskbar = false;
   this.TopMost = true;
   this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.NPButton_MouseDown);
   this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.NPButton_MouseUp);
   this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.NPButton_MouseMove);
   this.ResumeLayout(false);
  }
  #endregion
  private void NPButton_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   mouseX=e.X;
   mouseY=e.Y;
  }
  private void NPButton_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   if (e.Button==MouseButtons.Left)
   {
    currX = this.Left - mouseX + e.X;
    currY = this.Top - mouseY + e.Y;
    this.SetDesktopLocation(currX,currY);  
   }
  }
  private void NPButton_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   this.Capture=true;
  }
 }
 
原创粉丝点击