zjl880 发表于 2012-7-7 10:13:00

如何在鼠标移动到TEXTFIELD上显示一窗口

问题:当鼠标移动到该区域,我怎么获取鼠标所在位置的TEXTFIELD

iceman 发表于 2012-7-9 10:40:00

回复 1楼zjl880的帖子

获取方法请参考代码:

      private void Form1_Load(object sender, EventArgs e)
      {
            TXTextControl.TextField field = new TXTextControl.TextField();
            field.Text = "位置测试";
            field.ID = 10;
            this.textControl1.TextFields.Add(field);
            this.textControl1.MouseMove += new MouseEventHandler(textControl1_MouseMove);
      }

      void textControl1_MouseMove(object sender, MouseEventArgs e)
      {
            TXTextControl.TextField field = GetTextFieldFromLocation(e.Location);
            if (field!=null)
            {
                MessageBox.Show(field.Text);
            }
      }
      private TXTextControl.TextField GetTextFieldFromLocation(Point Location)
      {
            Graphics g = textControl1.CreateGraphics();
            int iDpi = (int)(1440 / g.DpiX);
            Point pTwipsLocation = new Point(Location.X * iDpi, Location.Y * iDpi);
            foreach (TXTextControl.TextField tfField in textControl1.TextFields)
            {
                Rectangle rFieldBounds = new Rectangle(tfField.Bounds.X - textControl1.ScrollLocation.X,
                  tfField.Bounds.Y - textControl1.ScrollLocation.Y, tfField.Bounds.Width,
                  tfField.Bounds.Height);

                if (pTwipsLocation.X >= rFieldBounds.X &amp;&amp; pTwipsLocation.X <= rFieldBounds.X
                  + rFieldBounds.Width &amp;&amp; pTwipsLocation.Y >=
                  rFieldBounds.Y &amp;&amp; pTwipsLocation.Y <= rFieldBounds.Y + rFieldBounds.Height)
                {
                  return tfField;
                }
            }
            return null;
      }

zjl880 发表于 2012-7-10 15:06:00

谢谢:)

iceman 发表于 2012-7-10 16:28:00

回复 3楼zjl880的帖子

:share:
页: [1]
查看完整版本: 如何在鼠标移动到TEXTFIELD上显示一窗口