如果另一个工具条位于同一工具条面板上,则将工具条向左移太近将创建新行 [英] Moving toolstrip just a little too far left will create a new row if another toolstrip is on the same toolstrip panel

查看:139
本文介绍了如果另一个工具条位于同一工具条面板上,则将工具条向左移太近将创建新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在同一工具栏面板中将另一个工具栏拖动到左侧的工具栏(也许只是将其放在角落)时,我拖动的工具栏会跳到新"行,就像我已经移动了一样它下来.解释起来非常棘手,所以这里有几个图表.

When I drag a toolstrip to the left (perhaps just to get it in the corner) with another toolstrip in that same toolstrip panel, the one I'm dragging jumps down to a 'new' row, as if I had moved it down. It's quite tricky to explain, so here's a couple of diagrams.

图A:我将工具条向左移动,并且不小心"向左移得太远(只有几十个像素,用户可以轻松地做到这一点).

Diagram A: I move the toolstrip to the left, and 'accidentally' go too far left (well only by a couple of dozen pixels, which users can easily do).

图B:发生这种情况,拖动的工具条会下降一行.

Diagram B: This happens, the dragged toolstrip drops down a row.

如何防止此新行被创建"?作为最后的选择,我很乐意防止在任何情况下都创建新行(例如:如果用户打算将其向下拖动以创建新行).

How can I prevent this new row from being 'created'? As a last resort, I would be happy to prevent new rows being created under any circumstances (e.g: If the user intended to drag it down to create a new row).

我已经尝试过LayoutStyle了.

I've experimented with LayoutStyle to no avail.

推荐答案

可能有几种方法可以做到这一点.我尝试了一些,但没有一个是完美的.我想说的是,找到的最简单的方法是像Hans提到的那样设置ToolStripPanel的最大大小,还对ToolStrip进行子类化并覆盖OnLocationChanged(或将事件处理程序分配给LocationChanged而不是子类化,但是您d必须为每个ToolStrip分配一个处理程序.)

There's probably a couple ways to do this. I've experimented with a few, but none are perfect. I would say the least complicated way I found is to set the maximum size of the ToolStripPanel like Hans mentioned, and also to subclass the ToolStrip and override OnLocationChanged (or assign an event handler to LocationChanged rather than subclassing, but you'd have to assign a handler to each ToolStrip).

public class ToolStripEx : ToolStrip
{
    protected override void OnLocationChanged(EventArgs e)
    {
        if (this.Location.Y >= this.Parent.MaximumSize.Height)
        {
            this.Location = new Point(this.Location.X, 0);
        }
        else
        {
            base.OnLocationChanged(e);
        }           
    }
}


注意:这会导致鼠标在尝试向下拖动ToolStrip时来回跳动,原因是在 位置已更改后,位置被重置,因此它实际上是向下移动,然后立即向上跳回.

Note: This causes the mouse to jump back and forth when trying to drag the ToolStrip down, due to the fact that the Location is reset after it already changed, so it is essentially moving down, then immediately jumping back up.

值得一提的是,这可能会让用户感到烦恼,尤其是如果他们有意将ToolStrip放置在新的一行中,那么我不建议您这样做.但是,既然您问了,就在那里.

It's also worth mentioning that this may be an annoyance to the user, especially if they are purposely trying to put the ToolStrip in a new row, so I wouldn't really recommend doing this. But since you asked, there it is.

使用完整的步骤和代码进行更新:

我创建了一个新的空白Windows Forms项目.我在解决方案中添加了一个新文件ToolStripEx.cs,这就是里面的内容:

I created a new blank Windows Forms project. I added a new file ToolStripEx.cs to the solution, and this is what is inside:

已更新其他面板及其Orientation

Updated for the other panels and their Orientation

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public class ToolStripEx : ToolStrip
    {
        protected override void OnLocationChanged(EventArgs e)
        {
            if (this.Parent is ToolStripPanel)
            {
                ToolStripPanel parent = this.Parent as ToolStripPanel;

                if (parent.Orientation == Orientation.Horizontal)
                {
                    if (this.Location.Y != 0)
                    {
                        this.Location = new Point(this.Location.X, 0);
                        return;
                    }
                }
                else if (parent.Orientation == Orientation.Vertical)
                {
                    if (this.Location.X != 0)
                    {
                        this.Location = new Point(0, this.Location.Y);
                        return;
                    }
                }
            }
            base.OnLocationChanged(e);
        }
    }
}


然后,我构建了解决方案,以使ToolStripEx类将显示在工具箱中.


Then I built the solution so the ToolStripEx class will show up in the Toolbox.

然后我将常规的ToolStripContainer放在工具箱的表单上,将Dock设置为Fill,设置颜色,等等.

Then I put a regular ToolStripContainer onto the form from the Toolbox, set Dock to Fill, set the colors, etc.

然后,我从工具箱中将两个ToolStripEx(带有您提到的齿轮图标)拖到TopToolStripPanel.我设置了它们的颜色,渲染器等等.

Then I dragged two ToolStripExs (with the cog icon you mentioned) from the Toolbox to the TopToolStripPanel. I set their colors and renderers and whatnot.

这是Form1.cs的样子:

已更新以设置其他最大尺寸

using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.toolStripContainer1.TopToolStripPanel.MaximumSize = new Size(0, toolStripEx1.Height);
            this.toolStripContainer1.LeftToolStripPanel.MaximumSize = new Size(toolStripEx1.Height, 0);
            this.toolStripContainer1.BottomToolStripPanel.MaximumSize = new Size(0, toolStripEx1.Height);
            this.toolStripContainer1.RightToolStripPanel.MaximumSize = new Size(toolStripEx1.Height, 0);
        }
    }
}


注意:此代码可防止任何面板扩展其行(或列,如果OrientationOrientation.Vertical则为列)如果您希望侧面板能够扩展,不要设置其最大大小并摆脱else if parent.Orientation == Orientation.Vertical部分.

Note: This code prevents any of the panels from expanding their rows (or columns if they have an Orientation of Orientation.Vertical) If you want the side panels to be able to expand, don't set their maximum size and get rid of the else if parent.Orientation == Orientation.Vertical section.

这应该就是它的全部了.我执行了此操作,并且移动它们时,两个ToolStripEx都没有消失.

That should be all there is to it. I ran this, and both of the ToolStripExs did not disappear when moving them around.

就像汉斯所说的那样,ToolStrip类非常古怪,几乎所有解决问题的方法都不是完美的,除非您从头开始开发自己的控件,并牢记自己的需求.

Like Hans said, the ToolStrip class is pretty quirky and pretty much any solution to your problem is not going to be perfect unless you develop your own controls from the ground up with your needs in mind.

如果出于某些原因需要扩展ToolStripContainer类,请将其与新的ToolStripEx类分开.我怀疑嵌套类会导致您仍然使用常规的ToolStrip而不是ToolStripEx类.

If for some reason you need to extend the ToolStripContainer class, keep it separate from the new ToolStripEx class. I suspect that having the nested classes was causing you to still be using the regular ToolStrip rather than the ToolStripEx class.

另一个更新-解决了鼠标跳动问题: 我在尝试摆脱鼠标光标问题时偶然发现了这一点.将此添加到ToolStripEx类:

Another Update - fixing the mouse jumping: I found this by accident when experimenting to get rid of the mouse cursor problem. Add this to the ToolStripEx class:

protected override void OnBeginDrag(EventArgs e)
{
    //base.OnBeginDrag(e);
}

奇怪的是,这似乎大大降低了工具条将其拖到面板外的阻力.我还没有研究为什么它起作用,但是似乎ToolStrip无需使用基本的拖放功能即可实现其自身的拖动行为,而通过覆盖OnBeginDragDrop,ToolStrip会排他地使用其自定义行为,这会使鼠标的行为更好拖动时.

Strangely enough, that seems to cut down significantly on the toolstrip's resistance to being dragged outside of a panel. I haven"t dug into why this works, but it seems like ToolStrip implements its own dragging behavior without using basic drag/drop functionality and by overriding OnBeginDragDrop the ToolStrip is then using its custom behavior exclusively which makes the mouse act a lot nicer when dragging it.

这篇关于如果另一个工具条位于同一工具条面板上,则将工具条向左移太近将创建新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆