當前位置:編程學習大全網 - 源碼下載 - 窗體中怎麽讓toolstrip漂浮(停靠已經做出來了)(C#)

窗體中怎麽讓toolstrip漂浮(停靠已經做出來了)(C#)

DotNet2.0開發框架中提供的ToolStrip和ToolStripPanel控件可以方便開發具有可停靠工具欄功能的Windows應用程序, ToolStrip對象可以在各個ToolStripPanel間完成拖拽停靠,但是如果想實現類似VS IDE 或Office中可以浮動的工具欄必須借助於DevExpress等壹些第三方的控件或編寫壹定的代碼。 這裏介紹壹種比較簡單的方法,只需繼承ToolStrip類即可實現上述的效果。

放置到ToolStripPanel上的,當工具欄浮動的時候,事實上是改變了其所在的容器對象,從其所在的ToolStripPanel移動到壹個漂浮的容器上,因此要實現工具欄的浮動必須解決以下兩個問題:

必須有壹個浮動的容器來承載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類。增加了相應的屬性和方法。

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 漂浮實現

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

}

}

結論。 該方法實現較簡單, 當不願意使用功能較強大的第三方控件庫時可以采用這種方法,缺點是負責浮動的容器是壹個窗口,不大美觀。

  • 上一篇:2016年有什麽好看電影推薦下
  • 下一篇:qtouch初學者,在看 modbustcp與qtouch工程上位機通訊測試,請問qtouch怎麽新建
  • copyright 2024編程學習大全網