【问题标题】:How to allow user to move a control on the form如何允许用户在表单上移动控件
【发布时间】:2014-12-09 21:14:08
【问题描述】:

我有一个允许用户移动控件的 winform。

该控件(目前)是一条垂直线:带有边框和宽度为 1 的标签。

上下文不是很重要,但无论如何我都会给你。我有一些图形的背景,我希望用户能够在图形上方滑动指南。图形是用 Nplots 库制作的。它看起来像这样: http://www.ibme.de/pictures/xtm-window-graphic-ramp-signals.png

如果我能找出用户如何在屏幕上单击并拖动标签/线条控件,我就能解决我的指南问题。请帮忙。

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

    此代码可能会有点复杂,但本质上您需要在表单上捕获 MouseDown、MouseMove 和 MouseUp 事件。像这样的:

    public void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        if(e.Button != MouseButton.Left)
            return;
    
        // Might want to pad these values a bit if the line is only 1px,
        // might be hard for the user to hit directly
        if(e.Y == myControl.Top)
        {
            if(e.X >= myControl.Left && e.X <= myControl.Left + myControl.Width)
            {
                _capturingMoves = true;
                return;
            }
        }
    
        _capturingMoves = false;
    }
    
    public void Form1_MouseMove(object sender, MouseEventArgs e) 
    {
        if(!_capturingMoves)
            return;
    
        // Calculate the delta's and move the line here
    }
    
    public void Form1_MouseUp(object sender, MouseEventArgs e) 
    {
        if(_capturingMoves)
        {
            _capturingMoves = false;
            // Do any final placement
        }
    }
    

    【讨论】:

      【解决方案2】:

      在 WinForms 中,您可以处理控件的 MouseDown、MouseMove 和 MouseUp 事件。在 MouseDown 上,设置一些位或引用来告诉您的表单单击鼠标的控件,并从 MouseEventArgs 捕获鼠标的 X 和 Y。在 MouseMove 上,如果设置了控件,则通过上次捕获的 X 和 Y 与当前坐标之间的差异来调整其 X 和 Y。在 MouseUp 上,释放控件。

      我会为此设置一个“编辑模式”;当用户进入此模式时,表单控件的当前事件处理程序应该被分离,并附加移动处理程序。如果您想保留或恢复这些更改(例如您正在制作一个自定义表单设计器,您的客户可以使用它来自定义窗口布局),您还需要能够拍摄一些前后布局的快照控件。

      【讨论】:

        【解决方案3】:

        我为此编写了一个组件:在表单上移动控件(或在屏幕上移动无边框表单)。您甚至可以从设计器中使用它,而无需编写任何代码。

        http://www.thomaslevesque.com/2009/05/06/windows-forms-automatically-drag-and-drop-controls-dragmove/

        【讨论】:

          【解决方案4】:

          这是一种可用于任何控件的扩展方法。它使用 Rx 并基于 Wes Dyer 的 A Brief Introduction to the Reactive Extensions for .NET, Rx 帖子和示例。

          public static class FormExtensions
          {
              public static void EnableDragging(this Control c)
              {
                  // Long way, but strongly typed.        
                  var downs =
                      from down in Observable.FromEvent<MouseEventHandler, MouseEventArgs>(
                          eh => new MouseEventHandler(eh),
                          eh => c.MouseDown += eh,
                          eh => c.MouseDown -= eh)
                      select new { down.EventArgs.X, down.EventArgs.Y };
          
                  // Short way.        
                  var moves = from move in Observable.FromEvent<MouseEventArgs>(c, "MouseMove")
                              select new { move.EventArgs.X, move.EventArgs.Y };
          
                  var ups = Observable.FromEvent<MouseEventArgs>(c, "MouseUp");
          
                  var drags = from down in downs
                              from move in moves.TakeUntil(ups)
                              select new Point { X = move.X - down.X, Y = move.Y - down.Y };
          
                  drags.Subscribe(drag => c.SetBounds(c.Location.X + drag.X, c.Location.Y + drag.Y, 0, 0, BoundsSpecified.Location));
              }
          }
          

          用法:

          按钮 button1 = new Button();

          button1.EnableDragging();

          【讨论】:

            【解决方案5】:

            老实说,有一种更简单的方法,您可以初始化一个全局布尔变量,命名为您喜欢的任何名称,在本例中为isMouseClicked。在您希望允许拖动的控件上转到其鼠标按下事件,

            确保这些事件是您的控件事件而不是表单事件。

            if (e.button == MouseButtons.left)
                //this is where you set the boolean to true
            

            然后转到它的鼠标移动事件

            if (isMouseClicked == true)
                //You then set your location of your control. See below:
                Button1.Location = new Point(MousePosition.X, MousePosition.Y);
            

            在您的鼠标上确保将您的isMouseClicked 设置为false

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2021-06-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-07-27
              相关资源
              最近更新 更多