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

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

问题描述

到目前为止,我已经构建了一个简单的程序,该程序的大面板为该程序的"WorkArea".我在上面绘制了一个网格,并具有一些将光标捕捉到网格上最近点的功能,等等.我在窗口底部有一个状态栏,用于在面板上显示当前位置.但是,无论我滚动到哪个位置(假设垂直条相对于顶部的比例为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);

      ..
}

请注意,

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天全站免登陆