C#工具欄的編程實現淺析
C#工具欄的編程實現是如何的呢?DotNet2.0開發框架中提供的ToolStrip和ToolStripPanel控件可以方便開發具有可停靠C#工具欄功能的Windows應用程序, ToolStrip對象可以在各個ToolStripPanel間完成拖拽使用,但是如果想實現類似VS IDE 或Office中可以浮動的C#工具欄必須借助于DevExpress等一些第三方的控件或編寫一定的代碼。 這里介紹一種C#工具欄的編程實現比較簡單的方法,只需繼承ToolStrip類即可實現上述的效果。
放置到ToolStripPanel上的,當C#工具欄浮動的時候,事實上是改變了其所在的容器對象,從其所在的ToolStripPanel移動到一個漂浮的容器上,因此要實現C#工具欄的浮動必須解決以下兩個問題:
◆必須有一個浮動的容器來承載ToolStrip對象。
◆須知道ToolStrip對象何時改變其所在的容器,即在浮動的容器和主窗口上ToolStripPanel之間停靠。
對于第一個問題,我們的解決方案是動態的創建一個Form類作為浮動的容器,命名為ToolStripFloatWindow,該Form對象具有以下的屬性:
FormBorderStyle = FixedToolWindow 邊框樣式
ShowInTaskbar = false 不在任務欄顯示
ShowIcon = false 不顯示窗口圖標
TopMost = true 在所有窗口之上
為了解決第二個問題,我們查閱MSDN獲知,當用鼠標拖拽ToolStrip對象釋放鼠標時會觸發其EndDrag事件。 我們在這個事件的處理方法中判斷當ToolStrip對象的位置被移動到所在的ToolStripPanel之外的時候,創建ToolStripFloatWindow對象,并將ToolStrip對象移動到ToolStripFloatWindow上;要使ToolStrip對象恢復到原來的窗體上只要判斷ToolStripFloatWindow對象的位置是否移動到了ToolStripPanel上, 當條件滿足時將ToolStrip對象移動回ToolStripPanel中并銷毀ToolStripFloatWindow對象。
此外,還要解決當ToolStrip對象放置到ToolStripFloatWindow對象上時, ToolStripFloatWindow對象必須與ToolStrip對象的尺寸一致。 還有ToolStripFloatWindow對象被點擊了關閉按鈕時不能將自己關閉。我們可以做兩個類來實現上述的思路。
ToolStripFloatWindow類繼承自Form類。
MyToolStrip 繼承自ToolStrip類。增加了相應的屬性和方法。
C#工具欄之MyToolStrip類的源代碼如下:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- namespace FloatingToolStrip
- ...{
- public partial class MyToolStrip : ToolStrip
- ...{
- public MyToolStrip()
- ...{
- InitializeComponent();
- this.EndDrag += new EventHandler(MyToolStrip_EndDrag);
- this.SizeChanged += new EventHandler(MyToolStrip_SizeChanged);
- }
- protected override void OnPaint(PaintEventArgs pe)
- ...{
- // TODO: 在此處添加自定義繪制代碼
- // 調用基類 OnPaint
- base.OnPaint(pe);
- }
- #region 漂浮狀態
- private ToolStripFloatWindow floatWindow;
- public ToolStripFloatWindow FloatWindow
- ...{
- get
- ...{
- return this.floatWindow;
- }
- set
- ...{
- floatWindow = value;
- if (FloatWindow != null)
- ...{
- floatWindow.LocationChanged +=
- new EventHandler(floatWindow_LocationChanged);
- floatWindow.FormClosing +=
- new FormClosingEventHandler(floatWindow_FormClosing);
- }
- }
- }
- public bool isFloating
- ...{
- get
- ...{
- return (floatWindow != null);
- }
- }
- private ToolStripPanel tsPanel;
- public ToolStripPanel ToolStripPanel
- ...{
- get
- ...{
- return this.tsPanel;
- }
- set
- ...{
- tsPanel = value;
- }
- }
- #endregion
- #region C#工具欄漂浮實現
- private void floatWindow_LocationChanged(
- object sender, EventArgs e)
- ...{
- //當floatwindws的位置移動到
- //toolstrippanel中時,將this放置到 toolstripPanel上
- if (this.floatWindow == null)
- ...{
- return;
- }
- Point currentPt = new Point(
- floatWindow.Location.X, floatWindow.Location.Y);
- Point minpt = this.tsPanel.PointToScreen(tsPanel.Location);
- Point maxpt;
- if(this.tsPanel.Height <= 20)...{
- maxpt = new Point(minpt.X +
- this.tsPanel.Width, minpt.Y + 20);
- }else...{
- maxpt = new Point(minpt.X +
- this.tsPanel.Width, minpt.Y + this.tsPanel.Height);
- }
- if ((currentPt.X > minpt.X) &&
- (currentPt.X < maxpt.X) &&
- (currentPt.Y > minpt.Y) &&
- (currentPt.Y < maxpt.Y))
- ...{
- this.floatWindow.Controls.Remove(this);
- this.tsPanel.SuspendLayout();
- this.tsPanel.Controls.Add(this);
- this.Location = this.tsPanel.PointToClient(currentPt);
- this.tsPanel.ResumeLayout();
- this.floatWindow.Dispose();
- this.floatWindow = null;
- }
- }
- private void MyToolStrip_EndDrag(
- object sender, EventArgs e)
- ...{
- //判斷移出時
- if (this.tsPanel == null)
- ...{
- MessageBox.Show("請先設置ToolStripPanel屬性");
- return;
- }
- Point endPoint = Cursor.Position;
- int openX, openY;
- openX = endPoint.X;
- openY = endPoint.Y;
- Point clientPt =
- this.tsPanel.Parent.PointToClient(endPoint);
- if (clientPt.Y > tsPanel.Height)
- ...{
- ToolStripFloatWindow fw = new ToolStripFloatWindow();
- this.tsPanel.Controls.Remove(this);
- fw.Controls.Add(this);
- this.Left = 0;
- this.Top = 0;
- this.FloatWindow = fw;
- Point newLoc = new Point(openX, openY);
- fw.Show();
- fw.Location = newLoc;
- fw.SetBounds(newLoc.X, newLoc.Y,
- this.ClientSize.Width, this.ClientSize.Height);
- }
- }
- private void floatWindow_FormClosing(
- object sender, FormClosingEventArgs e)
- ...{
- e.Cancel = true;
- }
- private void MyToolStrip_SizeChanged(
- object sender, EventArgs e)
- ...{
- if (this.isFloating)
- ...{
- this.floatWindow.Width = this.ClientSize.Width;
- }
- }
- #endregion
- //C#工具欄
- }
- }
C#工具欄編程實現實例結論:該方法實現較簡單, 當不愿意使用功能較強大的第三方控件庫時可以采用這種方法,缺點是負責浮動的容器是一個窗口,不大美觀。
C#工具欄編程實現的基本內容就向你介紹到這里,希望對你了解和學習C#工具欄有所幫助。
【編輯推薦】

















