C# ToolStrip浮动及上/下/左/右 停靠
2013-12-16 09:55
351 查看
关于浮动工具条的制作,阿捷写了一篇很不错的文章,见:http://www.cnblogs.com/ajiefj/archive/2010/04/27/1722256.html
阿捷这个工具条浮动后只能在顶部停靠,基于此,我在这边增加在左/右/底部停靠,停靠条件是浮动窗体紧贴或越过主窗体边缘。
其实阿捷给出的代码已经相当详细了:) 我这里主要给出重写的ToolStrip代码段,增加了三个ToolStripPanel
public partial class MyToolStrip : ToolStrip
{
public MyToolStrip()
{
InitializeComponent();
this.EndDrag += new EventHandler(MyToolStrip_EndDrag);
this.SizeChanged += new EventHandler(MyToolStrip_SizeChanged);
}
#region 漂浮状态
public ToolStripFloatWindow FloatWindow { get; set; }
private bool isFloating
{
get
{
return (FloatWindow != null);
}
}
public ToolStripPanel TopToolStripPanel { get; set; }
public ToolStripPanel BottomToolStripPanel { get; set; }
public ToolStripPanel LeftToolStripPanel { get; set; }
public ToolStripPanel RightToolStripPanel { get; set; }
#endregion
#region 漂浮实现
private void FloatWindow_LocationChanged(object sender, EventArgs e)
{
//当floatwindws的位置移动到 toolstrippanel中时,将this放置到 toolstripPanel上
if (this.FloatWindow == null)
{
return;
}
if (FloatWindow.HasCreated)
{
//主窗体位置
Point frmLoc = this.TopToolStripPanel.Parent.Location;
//浮动工具条位置
Point toolBarLoc = FloatWindow.Location;
if (toolBarLoc.Y - frmLoc.Y <= 0) //置于顶部StripPanel
{
this.FloatWindow.Controls.Remove(this);
this.TopToolStripPanel.SuspendLayout();
this.TopToolStripPanel.Controls.Add(this);
this.Location = this.TopToolStripPanel.PointToClient(toolBarLoc);
this.TopToolStripPanel.ResumeLayout();
this.FloatWindow.Dispose();
this.FloatWindow = null;
return;
}
if (toolBarLoc.X - frmLoc.X <= 0) //置于左边StripPanel
{
this.FloatWindow.Controls.Remove(this);
this.LeftToolStripPanel.SuspendLayout();
this.LeftToolStripPanel.Controls.Add(this);
this.Location = this.LeftToolStripPanel.PointToClient(toolBarLoc);
this.LeftToolStripPanel.ResumeLayout();
this.FloatWindow.Dispose();
this.FloatWindow = null;
return;
}
if (toolBarLoc.X + FloatWindow.Width >= this.TopToolStripPanel.Parent.Width) //置于右边StripPanel
{
this.FloatWindow.Controls.Remove(this);
this.RightToolStripPanel.SuspendLayout();
this.RightToolStripPanel.Controls.Add(this);
this.Location = this.RightToolStripPanel.PointToClient(toolBarLoc);
this.RightToolStripPanel.ResumeLayout();
this.FloatWindow.Dispose();
this.FloatWindow = null;
return;
}
if (toolBarLoc.Y + FloatWindow.Height >= this.TopToolStripPanel.Parent.Height) //置于底部StripPanel
{
this.FloatWindow.Controls.Remove(this);
this.BottomToolStripPanel.SuspendLayout();
this.BottomToolStripPanel.Controls.Add(this);
this.Location = this.BottomToolStripPanel.PointToClient(toolBarLoc);
this.BottomToolStripPanel.ResumeLayout();
this.FloatWindow.Dispose();
this.FloatWindow = null;
return;
}
}
}
private void MyToolStrip_EndDrag(object sender, EventArgs e)
{
Point screenPt = Cursor.Position;
Point clientPt = this.TopToolStripPanel.Parent.PointToClient(screenPt);
//浮动区域
Rectangle floatArea = new Rectangle(32, 32, //我这里图标大小调整为32*32
this.TopToolStripPanel.Parent.Width - 2 * 32,
this.TopToolStripPanel.Parent.Height - 2 * 32);
if (floatArea.Contains(clientPt)) //判断移出时
{
ToolStripFloatWindow fw = new ToolStripFloatWindow();
fw.Controls.Add(this);
this.Left = 0;
this.Top = 0;
this.FloatWindow = fw;
FloatWindow.LocationChanged += new EventHandler(FloatWindow_LocationChanged);
fw.SetBounds(screenPt.X, screenPt.Y, this.ClientSize.Width, this.ClientSize.Height + 22); //22为窗体标题栏高度
fw.Show();
}
}
private void MyToolStrip_SizeChanged(object sender, EventArgs e)
{
if (this.isFloating)
{
this.FloatWindow.Width = this.ClientSize.Width;
}
}
#endregion
}
主窗体自然是放四个ToolStripPanel和一个MyToolStrip,注意主窗体的IsMdiContainer属性 置为 True
阿捷这个工具条浮动后只能在顶部停靠,基于此,我在这边增加在左/右/底部停靠,停靠条件是浮动窗体紧贴或越过主窗体边缘。
其实阿捷给出的代码已经相当详细了:) 我这里主要给出重写的ToolStrip代码段,增加了三个ToolStripPanel
public partial class MyToolStrip : ToolStrip
{
public MyToolStrip()
{
InitializeComponent();
this.EndDrag += new EventHandler(MyToolStrip_EndDrag);
this.SizeChanged += new EventHandler(MyToolStrip_SizeChanged);
}
#region 漂浮状态
public ToolStripFloatWindow FloatWindow { get; set; }
private bool isFloating
{
get
{
return (FloatWindow != null);
}
}
public ToolStripPanel TopToolStripPanel { get; set; }
public ToolStripPanel BottomToolStripPanel { get; set; }
public ToolStripPanel LeftToolStripPanel { get; set; }
public ToolStripPanel RightToolStripPanel { get; set; }
#endregion
#region 漂浮实现
private void FloatWindow_LocationChanged(object sender, EventArgs e)
{
//当floatwindws的位置移动到 toolstrippanel中时,将this放置到 toolstripPanel上
if (this.FloatWindow == null)
{
return;
}
if (FloatWindow.HasCreated)
{
//主窗体位置
Point frmLoc = this.TopToolStripPanel.Parent.Location;
//浮动工具条位置
Point toolBarLoc = FloatWindow.Location;
if (toolBarLoc.Y - frmLoc.Y <= 0) //置于顶部StripPanel
{
this.FloatWindow.Controls.Remove(this);
this.TopToolStripPanel.SuspendLayout();
this.TopToolStripPanel.Controls.Add(this);
this.Location = this.TopToolStripPanel.PointToClient(toolBarLoc);
this.TopToolStripPanel.ResumeLayout();
this.FloatWindow.Dispose();
this.FloatWindow = null;
return;
}
if (toolBarLoc.X - frmLoc.X <= 0) //置于左边StripPanel
{
this.FloatWindow.Controls.Remove(this);
this.LeftToolStripPanel.SuspendLayout();
this.LeftToolStripPanel.Controls.Add(this);
this.Location = this.LeftToolStripPanel.PointToClient(toolBarLoc);
this.LeftToolStripPanel.ResumeLayout();
this.FloatWindow.Dispose();
this.FloatWindow = null;
return;
}
if (toolBarLoc.X + FloatWindow.Width >= this.TopToolStripPanel.Parent.Width) //置于右边StripPanel
{
this.FloatWindow.Controls.Remove(this);
this.RightToolStripPanel.SuspendLayout();
this.RightToolStripPanel.Controls.Add(this);
this.Location = this.RightToolStripPanel.PointToClient(toolBarLoc);
this.RightToolStripPanel.ResumeLayout();
this.FloatWindow.Dispose();
this.FloatWindow = null;
return;
}
if (toolBarLoc.Y + FloatWindow.Height >= this.TopToolStripPanel.Parent.Height) //置于底部StripPanel
{
this.FloatWindow.Controls.Remove(this);
this.BottomToolStripPanel.SuspendLayout();
this.BottomToolStripPanel.Controls.Add(this);
this.Location = this.BottomToolStripPanel.PointToClient(toolBarLoc);
this.BottomToolStripPanel.ResumeLayout();
this.FloatWindow.Dispose();
this.FloatWindow = null;
return;
}
}
}
private void MyToolStrip_EndDrag(object sender, EventArgs e)
{
Point screenPt = Cursor.Position;
Point clientPt = this.TopToolStripPanel.Parent.PointToClient(screenPt);
//浮动区域
Rectangle floatArea = new Rectangle(32, 32, //我这里图标大小调整为32*32
this.TopToolStripPanel.Parent.Width - 2 * 32,
this.TopToolStripPanel.Parent.Height - 2 * 32);
if (floatArea.Contains(clientPt)) //判断移出时
{
ToolStripFloatWindow fw = new ToolStripFloatWindow();
fw.Controls.Add(this);
this.Left = 0;
this.Top = 0;
this.FloatWindow = fw;
FloatWindow.LocationChanged += new EventHandler(FloatWindow_LocationChanged);
fw.SetBounds(screenPt.X, screenPt.Y, this.ClientSize.Width, this.ClientSize.Height + 22); //22为窗体标题栏高度
fw.Show();
}
}
private void MyToolStrip_SizeChanged(object sender, EventArgs e)
{
if (this.isFloating)
{
this.FloatWindow.Width = this.ClientSize.Width;
}
}
#endregion
}
主窗体自然是放四个ToolStripPanel和一个MyToolStrip,注意主窗体的IsMdiContainer属性 置为 True
相关文章推荐
- C# ToolStrip浮动及上/下/左/右 停靠
- 利用DockPanel与C#制作窗体浮动和停靠(vs2010)
- 利用DockPanel与C#制作窗体浮动和停靠(vs2010)
- 纯CSS定位的固定垂 浮动层 ,支持所以浏览器,静态停靠(广告用)
- 用C#代码实现类似QQ窗体的“上、左、右”停靠功能【转】
- C#浮动的窗口如何实现
- C# 实现类似QQ的窗体在桌面边缘停靠和隐藏
- C# Visual Studio 2005中修改toolStrip的内容
- C#实现类似QQ的隐藏浮动窗体、消息闪动
- C#实现浮动和多标签窗体解决方案---使用Dockpanel
- C#停靠栏组件 DockPanel Suite
- 利用DockPanel实现可停靠、可浮动、多文档窗体
- C#中使用WeiFenLuo.WinFormsUI.Docking.dll实现窗口停靠效果
- 在mfc的sdi/mdi程序任意视图view内创建可停靠及浮动的工具栏方法
- MFC创建可停靠与浮动窗口
- C# 高仿腾讯QQ (主面板左边功能导航基于Toolstrip绘制)(附源码)
- 利用DockPanel实现可停靠、可浮动、多文档窗体
- C# 窗口停靠隐藏类 最近又修改了一下.使用更方便了
- c#浮动文字框Tooltip控件的使用总结随笔
- C#中toolStrip或statusStrip遮挡了SplitContainer怎么办?