|
用过VS的都知道,顶部有很多工具条,可以显示隐藏,也可以拖来拖去。这个在winform里面就是用ToolStripContainer实现的。如何使用,网上帖子多得是,这里记录一下我被坑的地方。 1、ToolStripContainer的高度随着toolstrip的拖动发生变化,也就是说,当toolstrip工具条之前是两行排列,现在拖为一行,这时如果不做任何处理,ToolStripContainer的高度仍然保持原样,有一片空白,看着很是别扭。网上找了很久,无果。后来自己调试发现,在TopToolStripPanel_SizeChanged事件里面添加一行代码就搞定啦。
- private void toolStripContainer1_TopToolStripPanel_SizeChanged(object sender, EventArgs e)
- {
- toolStripContainer1.Height = toolStripContainer1.TopToolStripPanel.Height;
- }
复制代码 我只要TopToolStripPanel,其他的Panel都隐藏了,没用到。
2、如何让程序的时候,ToolStripContainer只有一行toolstrip。这个网上也没说,博客园人提问,后来说解决了,但又没晒出解决办法,我表示醉了!
分两步:
a、添加工具条进ToolStripContainer,TopToolStripPanel.SizeChanged事件里面设置Container高度。
b、分别设置每个toolstrip的location。注意这步要在窗体Shown事件里面做。(如果是用usercontrol,在构造函数)。
务必a在前,b在后。思路就是手动调整Location,稍加调试即可。
工具条我是放到UserControl里面的:
- <p>ZtoolStripContainerMain.TopToolStripPanel.Controls.Add(toolStrip2);
- ZtoolStripContainerMain.TopToolStripPanel.Controls.Add(toolStrip1);
- ZtoolStripContainerMain.ContentPanel.Visible = false;
- toolItemMapGeneric.Checked = toolStrip1.Visible;
- toolItemBusiness1.Checked = toolStrip2.Visible;
- ZtoolStripContainerMain.TopToolStripPanel.ContextMenuStrip = menuToolStrip;
- ZtoolStripContainerMain.TopToolStripPanel.SizeChanged += delegate//(object o,EventArgs e)
- {
- if (ZtoolStripContainerMain.TopToolStripPanel.Height > 0)
- {
- ZtoolStripContainerMain.Height = ZtoolStripContainerMain.TopToolStripPanel.Height;
- }
- };</p><p>
- </p><p>menuToolStrip.ItemClicked += (o, e) =>
- {
- bool ischecked = (e.ClickedItem as ToolStripMenuItem).Checked;
- int visibleCnt = 0;
- foreach (Control item in ZtoolStripContainerMain.TopToolStripPanel.Controls)
- {
- if (item.Visible)
- {
- visibleCnt++;
- }
- }
- //至少要有一个工具条存在,因为工具条为0的时候,toolStripContainer右键点击事件失效。
- if (toolStrip1.DisplayRectangle.Height == ZtoolStripContainerMain.Height &&
- ischecked && visibleCnt == 1)
- {
- return;
- }
- if (e.ClickedItem.Text == "地图基本工具")
- {
- ischecked = toolStrip1.Visible = !toolStrip1.Visible;
- }
- else if (e.ClickedItem.Text == "业务工具")
- {
- ischecked = toolStrip2.Visible = !toolStrip2.Visible;
- }
- (e.ClickedItem as ToolStripMenuItem).Checked = ischecked;
- };
- //为了使刚加载的时候,两根工具条在一行(且toolStrip1排在前面),这里的设置顺序不能换。
-
- toolStrip1.Location = new System.Drawing.Point(0, 0);</p><p>
- </p><p>toolStrip2.Location = new System.Drawing.Point(toolStrip1.Width, 0);</p></p>
复制代码
|
|