使用滚动条在面板上获取光标的位置 [英] Get Position of cursor on panel with scrollbars

查看:23
本文介绍了使用滚动条在面板上获取光标的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个简单的程序(到目前为止),它有一个大面板作为程序的工作区".我在上面画了一个网格,有一些功能可以将我的光标捕捉到网格上的最近点等.我在窗口底部有一个状态栏,显示我在面板上的当前位置.但是,无论我滚动到哪里(假设垂直条相对于顶部为 10%,水平为 25%),它都会显示我的光标相对于实际窗口的位置.

I've build a simple program (so far) that has a large panel as the "WorkArea" of the program. I draw a grid onto it, have some functionality that snaps my cursor to closest point on the grid etc. I have a status bar on the bottom of the window which displays my current position on the panel. However, regardless of where I've scrolled to (let's say vertical bar is at 10% relative to top and horizontal is 25%) it displays my cursor position with regards to the actual window.

我有一个 OnMouseMove 事件来处理这个:

I have a OnMouseMove event that handles this:

private void WorkArea_MouseMove(object sender, MouseEventArgs e)
{
    GridCursor = grid.GetSnapToPosition(new Point(e.X, e.Y));
    toolStripStatusLabel1.Text = grid.GetSnapToPosition(new Point(e.X, e.Y)).ToString();
    Refresh();
}

它像我期望的那样工作,给出光标的点,将其绘制到正确的位置,等等.但是,如果我向外滚动,我仍然会得到相同的读数.我可以在垂直和水平滚动条上滚动一半,将光标放在左上角,然后读取 0,0,此时它应该更像 5000,5000(在 10k x 10k 的面板上).

It works as I'd expect giving the points of the cursor, drawing it to the correct place, and so on. However, if I scroll out, I still get the same readings. I could be scrolled out half way on the vertical and horizontal scrollbars, put my cursor in the upper left-hand corner, and read a 0,0, when it should be something more like 5000,5000 (on a panel 10k by 10k).

如何获得面板内相对于其滚动条的绝对位置?

How can one go about getting the absolute position within a panel with respect to its scrollbars?

推荐答案

需要通过滚动位置偏移位置:

You need to offset the location by the scroll position:

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
      Point scrolledPoint = new Point( e.X - panel1.AutoScrollPosition.X, 
                                       e.Y - panel1.AutoScrollPosition.Y);

      ..
}

请注意 AutoScrollPosition 值为负..:

Note that the AutoScrollPosition values are negative..:

检索到的 X 和 Y 坐标值是负的,如果控制已从其起始位置 (0,0) 滚动.当你设置这个属性,您必须始终分配正 X 和 Y 值以设置相对于起始位置的滚动位置.例如,如果你有一个水平滚动条,将 x 和 y 设置为 200,然后移动向右滚动 200 像素;如果然后将 x 和 y 设置为 100,则滚动似乎向左跳了 100 像素,因为您正在设置它距离起始位置 100 像素.在第一种情况下,AutoScrollPosition 返回 {-200, 0};在第二种情况下,它返回{-100,0}.

The X and Y coordinate values retrieved are negative if the control has scrolled away from its starting position (0,0). When you set this property, you must always assign positive X and Y values to set the scroll position relative to the starting position. For example, if you have a horizontal scroll bar and you set x and y to 200, you move the scroll 200 pixels to the right; if you then set x and y to 100, the scroll appears to jump the left by 100 pixels, because you are setting it 100 pixels away from the starting position. In the first case, AutoScrollPosition returns {-200, 0}; in the second case, it returns {-100,0}.

这篇关于使用滚动条在面板上获取光标的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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