C#工具欄的編程實(shí)現(xiàn)淺析
C#工具欄的編程實(shí)現(xiàn)是如何的呢?DotNet2.0開發(fā)框架中提供的ToolStrip和ToolStripPanel控件可以方便開發(fā)具有可停靠C#工具欄功能的Windows應(yīng)用程序, ToolStrip對象可以在各個(gè)ToolStripPanel間完成拖拽使用,但是如果想實(shí)現(xiàn)類似VS IDE 或Office中可以浮動的C#工具欄必須借助于DevExpress等一些第三方的控件或編寫一定的代碼。 這里介紹一種C#工具欄的編程實(shí)現(xiàn)比較簡單的方法,只需繼承ToolStrip類即可實(shí)現(xiàn)上述的效果。
放置到ToolStripPanel上的,當(dāng)C#工具欄浮動的時(shí)候,事實(shí)上是改變了其所在的容器對象,從其所在的ToolStripPanel移動到一個(gè)漂浮的容器上,因此要實(shí)現(xiàn)C#工具欄的浮動必須解決以下兩個(gè)問題:
◆必須有一個(gè)浮動的容器來承載ToolStrip對象。
◆須知道ToolStrip對象何時(shí)改變其所在的容器,即在浮動的容器和主窗口上ToolStripPanel之間???。
對于第一個(gè)問題,我們的解決方案是動態(tài)的創(chuàng)建一個(gè)Form類作為浮動的容器,命名為ToolStripFloatWindow,該Form對象具有以下的屬性:
FormBorderStyle = FixedToolWindow 邊框樣式
ShowInTaskbar = false 不在任務(wù)欄顯示
ShowIcon = false 不顯示窗口圖標(biāo)
TopMost = true 在所有窗口之上
為了解決第二個(gè)問題,我們查閱MSDN獲知,當(dāng)用鼠標(biāo)拖拽ToolStrip對象釋放鼠標(biāo)時(shí)會觸發(fā)其EndDrag事件。 我們在這個(gè)事件的處理方法中判斷當(dāng)ToolStrip對象的位置被移動到所在的ToolStripPanel之外的時(shí)候,創(chuàng)建ToolStripFloatWindow對象,并將ToolStrip對象移動到ToolStripFloatWindow上;要使ToolStrip對象恢復(fù)到原來的窗體上只要判斷ToolStripFloatWindow對象的位置是否移動到了ToolStripPanel上, 當(dāng)條件滿足時(shí)將ToolStrip對象移動回ToolStripPanel中并銷毀ToolStripFloatWindow對象。
此外,還要解決當(dāng)ToolStrip對象放置到ToolStripFloatWindow對象上時(shí), ToolStripFloatWindow對象必須與ToolStrip對象的尺寸一致。 還有ToolStripFloatWindow對象被點(diǎn)擊了關(guān)閉按鈕時(shí)不能將自己關(guān)閉。我們可以做兩個(gè)類來實(shí)現(xiàn)上述的思路。
ToolStripFloatWindow類繼承自Form類。
MyToolStrip 繼承自ToolStrip類。增加了相應(yīng)的屬性和方法。
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: 在此處添加自定義繪制代碼
- // 調(diào)用基類 OnPaint
- base.OnPaint(pe);
- }
- #region 漂浮狀態(tài)
- 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#工具欄漂浮實(shí)現(xiàn)
- private void floatWindow_LocationChanged(
- object sender, EventArgs e)
- ...{
- //當(dāng)floatwindws的位置移動到
- //toolstrippanel中時(shí),將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)
- ...{
- //判斷移出時(shí)
- if (this.tsPanel == null)
- ...{
- MessageBox.Show("請先設(shè)置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#工具欄編程實(shí)現(xiàn)實(shí)例結(jié)論:該方法實(shí)現(xiàn)較簡單, 當(dāng)不愿意使用功能較強(qiáng)大的第三方控件庫時(shí)可以采用這種方法,缺點(diǎn)是負(fù)責(zé)浮動的容器是一個(gè)窗口,不大美觀。
C#工具欄編程實(shí)現(xiàn)的基本內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)C#工具欄有所幫助。
【編輯推薦】