【问题标题】:Restrict mouse move event inside a row of Grid - wpf c#限制一行网格内的鼠标移动事件 - wpf c#
【发布时间】:2020-08-18 16:54:40
【问题描述】:

我在下面的面板中有一个按钮。我必须将这个面板内的 GLControl 移动到任何地方。但是当像下面这样尝试时,当将按钮移动到面板的边框时,GLControl 会离开面板。如何仅限制面板内按钮的移动?

<WindowsFormsHost  x:Name="windowsFormsHost1"  Grid.Row="5"   Grid.ColumnSpan="3"   Initialized="WindowsFormsHost_Initialized"    >
<wf:Panel x:Name="Panel_glcontrol" Dock="None" BackColor="yellow">
       <wf:Panel.Controls>
          <opentk:GLControl x:Name="glControl" Width="450" Height="299"
     Dock="None" MouseMove="GlControl_MouseMove" MouseDown="GlControl_MouseDown"  Resize="glControl_Resize" Paint="glControl_Paint"  />  
       </wf:Panel.Controls>
</wf:Panel>
</WindowsFormsHost>

  private System.Drawing.Point MouseDownLocation;
  private void GlControl_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
                glControl.Left = e.X + glControl.Left - MouseDownLocation.X;
                glControl.Top = e.Y + glControl.Top - MouseDownLocation.Y;
        }
    }

    private void GlControl_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            MouseDownLocation = e.Location;
        }
    }

【问题讨论】:

    标签: c# wpf onmousedown onmousemove


    【解决方案1】:

    这看起来更像是一个 WinForms 问题。尝试对用于更新控制位置的代码设置限制。例如:

    glControl.Left = Math.Min(panel.Width - glControl.Width, e.X + glControl.Left - MouseDownLocation.X);
    glControl.Top = Math.Min(panel.Height - glControl.Height, e.Y + glControl.Top - MouseDownLocation.Y);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 1970-01-01
      • 2017-07-12
      • 2018-06-29
      相关资源
      最近更新 更多