c#創(chuàng)建浮動(dòng)工具欄功能示例
所謂的浮動(dòng)工具欄,效果圖如下:
也就是說,可以將工具欄拖出其原先的停靠位置,而且可以將拖出來的工具欄再拖放回去。
實(shí)現(xiàn)的基本思路如下
1、拖動(dòng)出來以后,需要?jiǎng)?chuàng)建一個(gè)大小合適的窗口,作為工具欄新的??咳萜?,這個(gè)窗口可以這樣設(shè)置:
FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
ShowIcon = false;
ShowInTaskbar = false;
TopMost = true;
2、浮動(dòng)工具欄可以擴(kuò)展自.Net Framework提供的ToolStrip,它被拖動(dòng)都某個(gè)位置,松開鼠標(biāo)左鍵時(shí),會觸發(fā)EndDarg事件,在這個(gè)事件中,我們將其從原來的停靠容器中移除,同時(shí)根據(jù)鼠標(biāo)左鍵松開時(shí),在鼠標(biāo)所在位置上創(chuàng)建一個(gè)窗口,作為工具欄的新容器。
這個(gè)就是基本的思路了,下面是浮動(dòng)工具欄FloatToolstrip 具體的實(shí)現(xiàn)代碼:
代碼
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace FloatToolStripDemo
{
public partial class FloatToolstrip : ToolStrip
{
private ToolStripPanel tsPanel;
public FloatToolstrip()
{
InitializeComponent();
this.EndDrag += new EventHandler(MyToolStrip_EndDrag);
this.SizeChanged += new EventHandler(MyToolStrip_SizeChanged);
}
private ToolStripFloatWindow floatForm;
public ToolStripFloatWindow FloatForm
{
get { return floatForm; }
set
{
floatForm = value;
if (floatForm != null)
{
floatForm.LocationChanged += new EventHandler(floatForm_LocationChanged);
floatForm.FormClosing += new FormClosingEventHandler(floatForm_FormClosing);
}
}
}
void floatForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
private void floatForm_LocationChanged(object sender, EventArgs e)
{
//當(dāng)floatwindws的位置移動(dòng)到toolstrippanel中時(shí),將this放置到 toolstripPanel上
if (this.floatForm == null)
{
return;
}
else
{
if (floatForm.HasCreated)
{
Point currentPt = new Point(floatForm.Location.X, floatForm.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 - 25) && (currentPt.Y < maxPt.Y - 25))
{
this.floatForm.Controls.Remove(this);
this.tsPanel.SuspendLayout();
this.tsPanel.Controls.Add(this);
this.Location = this.tsPanel.PointToClient(currentPt);
this.tsPanel.ResumeLayout();
this.floatForm.Dispose();
this.floatForm = null;
}
}
}
}
public bool isFloating
{
get
{
return (floatForm != null);
}
}
public ToolStripPanel ToolStripPanel
{
get
{
return this.tsPanel;
}
set
{
this.tsPanel = value;
}
}
private void MyToolStrip_EndDrag(object sender, EventArgs e)
{
//判斷移除時(shí)
if (this.tsPanel == null)
{
MessageBox.Show("請先設(shè)置ToolStripPanel屬性");
return;
}
Point dockPoint = Cursor.Position;
int openX, openY;
openX = dockPoint.X;
openY = dockPoint.Y;
Point clientPt = this.tsPanel.Parent.PointToClient(dockPoint);
if (clientPt.Y > tsPanel.Height)
{
ToolStripFloatWindow tsfw = new ToolStripFloatWindow();
this.tsPanel.Controls.Remove(this);
tsfw.Controls.Add(this);
this.Left = 0;
this.Top = 0;
this.FloatForm = tsfw;
Point newLoc = new Point(openX, openY);
tsfw.Show();
tsfw.Location = newLoc;
tsfw.SetBounds(newLoc.X, newLoc.Y, this.ClientSize.Width, this.ClientSize.Height+25);
}
}
private void MyToolStrip_SizeChanged(object sender, EventArgs e)
{
if (this.isFloating)
{
this.floatForm.Width = this.ClientSize.Width;
}
}
}
}
- C#鍵盤輸入回車鍵實(shí)現(xiàn)點(diǎn)擊按鈕效果的方法
- C# Hook鉤子實(shí)例代碼 截取鍵盤輸入
- C#詞法分析器之輸入緩沖和代碼定位的應(yīng)用分析
- c#判斷輸入的是不是數(shù)字的小例子
- asp.net(c#)限制用戶輸入規(guī)定的字符和數(shù)字的代碼
- c#實(shí)現(xiàn)網(wǎng)頁圖片提取工具代碼分享
- 使用C#開源文件實(shí)時(shí)監(jiān)控工具Tail&TailUI介紹
- C#實(shí)現(xiàn)支持?jǐn)帱c(diǎn)續(xù)傳多線程下載客戶端工具類
- asp.net(c#)做一個(gè)網(wǎng)頁數(shù)據(jù)采集工具
- 常用工具之 vb轉(zhuǎn)化C# 數(shù)據(jù)連接串 正則表達(dá)式查詢 源碼世界 腳本字典
- C#實(shí)現(xiàn)的SN快速輸入工具實(shí)例
相關(guān)文章
Unity代碼實(shí)現(xiàn)序列幀動(dòng)畫播放器
這篇文章主要為大家詳細(xì)介紹了Unity代碼實(shí)現(xiàn)序列幀動(dòng)畫播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02.NET(C#):Emit創(chuàng)建異常處理的方法
.NET(C#):Emit創(chuàng)建異常處理的方法,需要的朋友可以參考一下2013-04-04c#中object、var和dynamic的區(qū)別小結(jié)
這篇文章主要給大家介紹了關(guān)于c#中object、var和dynamic的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09C#使用委托實(shí)現(xiàn)的快速排序算法實(shí)例
這篇文章主要介紹了C#使用委托實(shí)現(xiàn)的快速排序算法,實(shí)例分析了C#委托機(jī)制與快速排序算法的實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-07-07C# 中文簡體轉(zhuǎn)繁體實(shí)現(xiàn)代碼
C# 中文簡體轉(zhuǎn)繁體實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-02-02