• <tfoot id='ebAnO'></tfoot>
    • <bdo id='ebAnO'></bdo><ul id='ebAnO'></ul>

  • <small id='ebAnO'></small><noframes id='ebAnO'>

    <legend id='ebAnO'><style id='ebAnO'><dir id='ebAnO'><q id='ebAnO'></q></dir></style></legend>

        <i id='ebAnO'><tr id='ebAnO'><dt id='ebAnO'><q id='ebAnO'><span id='ebAnO'><b id='ebAnO'><form id='ebAnO'><ins id='ebAnO'></ins><ul id='ebAnO'></ul><sub id='ebAnO'></sub></form><legend id='ebAnO'></legend><bdo id='ebAnO'><pre id='ebAnO'><center id='ebAnO'></center></pre></bdo></b><th id='ebAnO'></th></span></q></dt></tr></i><div id='ebAnO'><tfoot id='ebAnO'></tfoot><dl id='ebAnO'><fieldset id='ebAnO'></fieldset></dl></div>
      1. 不要将标签移到 PictureBox 之外

        时间:2023-08-25

            <legend id='omc8C'><style id='omc8C'><dir id='omc8C'><q id='omc8C'></q></dir></style></legend>

                  <bdo id='omc8C'></bdo><ul id='omc8C'></ul>
                  <tfoot id='omc8C'></tfoot>
                • <small id='omc8C'></small><noframes id='omc8C'>

                • <i id='omc8C'><tr id='omc8C'><dt id='omc8C'><q id='omc8C'><span id='omc8C'><b id='omc8C'><form id='omc8C'><ins id='omc8C'></ins><ul id='omc8C'></ul><sub id='omc8C'></sub></form><legend id='omc8C'></legend><bdo id='omc8C'><pre id='omc8C'><center id='omc8C'></center></pre></bdo></b><th id='omc8C'></th></span></q></dt></tr></i><div id='omc8C'><tfoot id='omc8C'></tfoot><dl id='omc8C'><fieldset id='omc8C'></fieldset></dl></div>

                    <tbody id='omc8C'></tbody>
                • 本文介绍了不要将标签移到 PictureBox 之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在创建一个应用程序,我可以在其中移动 PictureBox 上的 Labels.
                  问题是我希望这些标签只移动 inside PictureBox.

                  这是我的代码:

                  protected void lbl_MouseMove(object sender, MouseEventArgs e){标签 lbl = 作为标签的发件人;尝试{if (lbl != null && e.Button == MouseButtons.Left){if (m_lblLocation != new Point(0, 0)){点 newLocation = lbl.Location;新位置.X = 新位置.X + e.X - m_lblLocation.X;新位置.Y = 新位置.Y + e.Y - m_lblLocation.Y;lbl.Location = 新位置;this.Refresh();}}}捕捉(异常前){}}protected void lbl_MouseUp(对象发送者,MouseEventArgs e){标签 lbl = 作为标签的发件人;尝试{if (lbl != null && e.Button == MouseButtons.Left){m_lblLocation = Point.Empty;}}捕捉(异常前){}}protected void lbl_MouseDown(对象发送者,MouseEventArgs e){标签 lbl = 作为标签的发件人;尝试{if (lbl != null && e.Button == MouseButtons.Left){m_lblLocation = e.Location;}}捕捉(异常前){}}

                  在上面的代码中,我为标签创建了一些鼠标事件.

                  解决方案

                  PictureBox控件不是容器,不能直接在里面放另一个控件,就像使用 PanelGroupBox 或其他实现

                  I am creating an application in which I can move the Labels that are on a PictureBox.
                  The problem is that I want these to only Labels move inside the PictureBox.

                  Here is my code:

                  protected void lbl_MouseMove(object sender, MouseEventArgs e)
                  {
                      Label lbl = sender as Label;
                  
                      try
                      {
                          if (lbl != null && e.Button == MouseButtons.Left)
                          {
                              if (m_lblLocation != new Point(0, 0))
                              {
                                  Point newLocation = lbl.Location;
                                  newLocation.X = newLocation.X + e.X - m_lblLocation.X;
                                  newLocation.Y = newLocation.Y + e.Y - m_lblLocation.Y;
                                  lbl.Location = newLocation;
                                  this.Refresh();
                              }
                          }
                      }
                      catch(Exception ex) { }
                  }
                  
                  protected void lbl_MouseUp(object sender, MouseEventArgs e)
                  {
                      Label lbl = sender as Label;
                  
                      try
                      {
                          if (lbl != null && e.Button == MouseButtons.Left)
                          {
                              m_lblLocation = Point.Empty;
                          }
                      }
                      catch(Exception ex) { }
                  }
                  
                  protected void lbl_MouseDown(object sender, MouseEventArgs e)
                  {
                      Label lbl = sender as Label;
                  
                      try
                      {
                          if (lbl != null && e.Button == MouseButtons.Left)
                          {
                              m_lblLocation = e.Location;
                          }
                      }
                      catch(Exception ex) { }
                  }
                  

                  In above code I have created some mouse events for the Labels.

                  解决方案

                  The PictureBox control is not a container, you can't directly put another control inside it, as you would do with a Panel, a GroupBox or other controls that implement IContainerControl.
                  You could parent the Label (in this case), setting the Label Parent to a PictureBox handle. The Label.Bounds will then reflect the parent Bounds.
                  However it's not necessary: you can just calculate the position of the Label in relation to the control that contains both (Label(s) and PictureBox):

                  You can restrict the movements of other Label controls subscribing to the MovableLabel_MouseDown/MouseUp/MouseMove events.

                  An example:

                  bool ThisLabelCanMove;
                  Point LabelMousePosition = Point.Empty;
                  
                  private void MovableLabel_MouseDown(object sender, MouseEventArgs e)
                  {
                      if (e.Button == MouseButtons.Left)
                      {
                          LabelMousePosition = e.Location;
                          ThisLabelCanMove = true;
                      }
                  }
                  
                  private void MovableLabel_MouseUp(object sender, MouseEventArgs e)
                  {
                      ThisLabelCanMove = false;
                  }
                  
                  private void MovableLabel_MouseMove(object sender, MouseEventArgs e)
                  {
                      if (ThisLabelCanMove)
                      {
                          Label label = sender as Label;
                  
                          Point LabelNewLocation = new Point(label.Left + (e.Location.X - LabelMousePosition.X),
                                                             label.Top + (e.Location.Y - LabelMousePosition.Y));
                          LabelNewLocation.X = (LabelNewLocation.X < pictureBox1.Left) ? pictureBox1.Left : LabelNewLocation.X;
                          LabelNewLocation.Y = (LabelNewLocation.Y < pictureBox1.Top) ? pictureBox1.Top : LabelNewLocation.Y;
                          LabelNewLocation.X = (LabelNewLocation.X + label.Width > pictureBox1.Right) ? label.Left : LabelNewLocation.X;
                          LabelNewLocation.Y = (LabelNewLocation.Y + label.Height > pictureBox1.Bottom) ? label.Top : LabelNewLocation.Y;
                          label.Location = LabelNewLocation;
                      }
                  }
                  

                  这篇关于不要将标签移到 PictureBox 之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何将 DataGridView 行拖放到彼此下方? 下一篇:为什么这个 Selenium 的拖放 C# 代码在 Chrome 上不起

                  相关文章

                  最新文章

                      <tfoot id='kp2yB'></tfoot><legend id='kp2yB'><style id='kp2yB'><dir id='kp2yB'><q id='kp2yB'></q></dir></style></legend>

                    1. <small id='kp2yB'></small><noframes id='kp2yB'>

                      • <bdo id='kp2yB'></bdo><ul id='kp2yB'></ul>
                    2. <i id='kp2yB'><tr id='kp2yB'><dt id='kp2yB'><q id='kp2yB'><span id='kp2yB'><b id='kp2yB'><form id='kp2yB'><ins id='kp2yB'></ins><ul id='kp2yB'></ul><sub id='kp2yB'></sub></form><legend id='kp2yB'></legend><bdo id='kp2yB'><pre id='kp2yB'><center id='kp2yB'></center></pre></bdo></b><th id='kp2yB'></th></span></q></dt></tr></i><div id='kp2yB'><tfoot id='kp2yB'></tfoot><dl id='kp2yB'><fieldset id='kp2yB'></fieldset></dl></div>