c#创建浮动工具栏功能示例
所谓的浮动工具栏,效果图如下:
也就是说,可以将工具栏拖出其原先的停靠位置,而且可以将拖出来的工具栏再拖放回去。
实现的基本思路如下
1、拖动出来以后,需要创建一个大小合适的窗口,作为工具栏新的停靠容器,这个窗口可以这样设置:
FormBorderStyle=System.Windows.Forms.FormBorderStyle.FixedToolWindow;
ShowIcon=false;
ShowInTaskbar=false;
TopMost=true;
2、浮动工具栏可以扩展自.NetFramework提供的ToolStrip,它被拖动都某个位置,松开鼠标左键时,会触发EndDarg事件,在这个事件中,我们将其从原来的停靠容器中移除,同时根据鼠标左键松开时,在鼠标所在位置上创建一个窗口,作为工具栏的新容器。
这个就是基本的思路了,下面是浮动工具栏FloatToolstrip具体的实现代码:
代码
CodehighlightingproducedbyActiproCodeHighlighter(freeware)http://www.CodeHighlighter.com/-->usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Drawing;
usingSystem.Data;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceFloatToolStripDemo
{
publicpartialclassFloatToolstrip:ToolStrip
{
privateToolStripPaneltsPanel;
publicFloatToolstrip()
{
InitializeComponent();
this.EndDrag+=newEventHandler(MyToolStrip_EndDrag);
this.SizeChanged+=newEventHandler(MyToolStrip_SizeChanged);
}
privateToolStripFloatWindowfloatForm;
publicToolStripFloatWindowFloatForm
{
get{returnfloatForm;}
set
{
floatForm=value;
if(floatForm!=null)
{
floatForm.LocationChanged+=newEventHandler(floatForm_LocationChanged);
floatForm.FormClosing+=newFormClosingEventHandler(floatForm_FormClosing);
}
}
}
voidfloatForm_FormClosing(objectsender,FormClosingEventArgse)
{
e.Cancel=true;
}
privatevoidfloatForm_LocationChanged(objectsender,EventArgse)
{
//当floatwindws的位置移动到toolstrippanel中时,将this放置到toolstripPanel上
if(this.floatForm==null)
{
return;
}
else
{
if(floatForm.HasCreated)
{
PointcurrentPt=newPoint(floatForm.Location.X,floatForm.Location.Y);
PointminPt=this.tsPanel.PointToScreen(tsPanel.Location);
PointmaxPt;
if(this.tsPanel.Height<=20)
{
maxPt=newPoint(minPt.X+this.tsPanel.Width,minPt.Y+20);
}
else
{
maxPt=newPoint(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;
}
}
}
}
publicboolisFloating
{
get
{
return(floatForm!=null);
}
}
publicToolStripPanelToolStripPanel
{
get
{
returnthis.tsPanel;
}
set
{
this.tsPanel=value;
}
}
privatevoidMyToolStrip_EndDrag(objectsender,EventArgse)
{
//判断移除时
if(this.tsPanel==null)
{
MessageBox.Show("请先设置ToolStripPanel属性");
return;
}
PointdockPoint=Cursor.Position;
intopenX,openY;
openX=dockPoint.X;
openY=dockPoint.Y;
PointclientPt=this.tsPanel.Parent.PointToClient(dockPoint);
if(clientPt.Y>tsPanel.Height)
{
ToolStripFloatWindowtsfw=newToolStripFloatWindow();
this.tsPanel.Controls.Remove(this);
tsfw.Controls.Add(this);
this.Left=0;
this.Top=0;
this.FloatForm=tsfw;
PointnewLoc=newPoint(openX,openY);
tsfw.Show();
tsfw.Location=newLoc;
tsfw.SetBounds(newLoc.X,newLoc.Y,this.ClientSize.Width,this.ClientSize.Height+25);
}
}
privatevoidMyToolStrip_SizeChanged(objectsender,EventArgse)
{
if(this.isFloating)
{
this.floatForm.Width=this.ClientSize.Width;
}
}
}
}
相关文章
- 数据透视表上线!如何在纯前端实现这个强大的数据分析功能?
- C# winform美化窗体
- c# mysql executenonquery_C#与数据库访问技术总结(八)之ExecuteNonQuery方法
- C与MySQL结合实现处理图片的功能(c# mysql 图片)
- c#Random快速连续产生相同随机数的解决方案
- C#用Lambda和委托实现模板方法
- C#中如何执行存储过程方法
- C#仿密保卡功能的简单实现代码
- C#鼠标穿透窗体功能的实现方法
- c#打印预览控件中实现用鼠标移动页面功能代码分享
- c#网络唤醒功能实现
- C#Page用于各页面继承功能实例
- C#串口通信实现方法
- 基于C#生成随机数示例
- C#实现将网页保存成图片的网页拍照功能
- C#基础之异步调用实例教程