C# ToolStrip制作四邊??扛?dòng)工具欄
關(guān)于浮動(dòng)工具條的制作,阿捷寫了一篇很不錯(cuò)的文章,見:http://www.dbjr.com.cn/article/44272.htm
阿捷這個(gè)工具條浮動(dòng)后只能在頂部停靠,基于此,我在這邊增加在左/右/底部???,??織l件是浮動(dòng)窗體緊貼或越過主窗體邊緣。
其實(shí)阿捷給出的代碼已經(jīng)相當(dāng)詳細(xì)了:) 我這里主要給出重寫的ToolStrip代碼段,增加了三個(gè)ToolStripPanel
public partial class MyToolStrip : ToolStrip
{
public MyToolStrip()
{
InitializeComponent();
this.EndDrag += new EventHandler(MyToolStrip_EndDrag);
this.SizeChanged += new EventHandler(MyToolStrip_SizeChanged);
}
#region 漂浮狀態(tài)
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 漂浮實(shí)現(xiàn)
private void FloatWindow_LocationChanged(object sender, EventArgs e)
{
//當(dāng)floatwindws的位置移動(dòng)到 toolstrippanel中時(shí),將this放置到 toolstripPanel上
if (this.FloatWindow == null)
{
return;
}
if (FloatWindow.HasCreated)
{
//主窗體位置
Point frmLoc = this.TopToolStripPanel.Parent.Location;
//浮動(dòng)工具條位置
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);
//浮動(dòng)區(qū)域
Rectangle floatArea = new Rectangle(32, 32, //我這里圖標(biāo)大小調(diào)整為32*32
this.TopToolStripPanel.Parent.Width - 2 * 32,
this.TopToolStripPanel.Parent.Height - 2 * 32);
if (floatArea.Contains(clientPt)) //判斷移出時(shí)
{
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為窗體標(biāo)題欄高度
fw.Show();
}
}
private void MyToolStrip_SizeChanged(object sender, EventArgs e)
{
if (this.isFloating)
{
this.FloatWindow.Width = this.ClientSize.Width;
}
}
#endregion
}
相關(guān)文章
C#實(shí)現(xiàn)把圖片轉(zhuǎn)換成二進(jìn)制以及把二進(jìn)制轉(zhuǎn)換成圖片的方法示例
這篇文章主要介紹了C#實(shí)現(xiàn)把圖片轉(zhuǎn)換成二進(jìn)制以及把二進(jìn)制轉(zhuǎn)換成圖片的方法,結(jié)合具體實(shí)例形式分析了基于C#的圖片與二進(jìn)制相互轉(zhuǎn)換以及圖片保存到數(shù)據(jù)庫(kù)的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06C#?Math中常用數(shù)學(xué)運(yùn)算的示例詳解
Math?為通用數(shù)學(xué)函數(shù)、對(duì)數(shù)函數(shù)、三角函數(shù)等提供常數(shù)和靜態(tài)方法,使用起來(lái)非常方便。這篇文章主要為大家介紹幾個(gè)常用的數(shù)學(xué)運(yùn)算的使用,需要的可以參考一下2022-11-11DevExpress SplitContainerControl用法總結(jié)
這篇文章主要介紹了DevExpress SplitContainerControl用法,對(duì)初學(xué)者有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-08-08