c#鼠标滚轮事件的代码看不懂

open422000 2012-09-25 12:38:59
该代码是 实现当前鼠标的位置,放大缩小图形的。

我不懂他的思路






PointF m_panOffset = new PointF(25, -25); //为什么取这个店呢。。
PointF m_dragOffset = new PointF(0, 0);
float m_screenResolution = 96;
protected override void OnMouseWheel(MouseEventArgs e)
{
UnitPoint p = GetMousePoint();
float wheeldeltatick = 120;
float zoomdelta = (1.25f * (Math.Abs(e.Delta) / wheeldeltatick)); //为什么要这样倍数
if (e.Delta < 0)
m_model.Zoom = m_model.Zoom / zoomdelta;
else
m_model.Zoom = m_model.Zoom * zoomdelta;
SetCenterScreen(ToScreen(p), true);
DoInvalidate(true);
base.OnMouseWheel(e);
}

PointF Translate(UnitPoint point)
{
return point.Point;
}
float ScreenHeight()
{
return (float)(ToUnit(this.ClientRectangle.Height) / m_model.Zoom);//这里为什么要除缩放比例呢
}
public double ToUnit(float screenvalue)
{
return (double)screenvalue / (double)(m_screenResolution * m_model.Zoom);//上面除了比例 ,这里为什么还除呢。 还有为什么要除分辨率呢。。
}


public UnitPoint GetMousePoint()
{
Point point = this.PointToClient(Control.MousePosition);
return ToUnit(point);
}
//UnitPoint只是个类

protected void SetCenterScreen(PointF screenPoint, bool setCursor)
{
float centerX = ClientRectangle.Width / 2;
m_panOffset.X += centerX - screenPoint.X; //这里又是什么意思。。

float centerY = ClientRectangle.Height / 2;
m_panOffset.Y += centerY - screenPoint.Y;

if (setCursor)
Cursor.Position = this.PointToScreen(new Point((int)centerX, (int)centerY));
DoInvalidate(true);
}


public UnitPoint ToUnit(PointF screenpoint)
{
float panoffsetX = m_panOffset.X + m_dragOffset.X;
float panoffsetY = m_panOffset.Y + m_dragOffset.Y;
float xpos = (screenpoint.X - panoffsetX) / (m_screenResolution * m_model.Zoom);
float ypos = ScreenHeight() - ((screenpoint.Y - panoffsetY)) / (m_screenResolution * m_model.Zoom);///为什么要用height - 呢
return new UnitPoint(xpos, ypos);
}



public UnitPoint(double x, double y)
{
m_x = x;
m_y = y;
}








我把主要代码 贴了出来,请高手指点逻辑、、、
...全文
3827 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2012-09-25
  • 打赏
  • 举报
回复
最好的办法是你尝试修改这些变量,看程序的反应有什么不同。

有时候有些东西是有用但是不绝对的,明白我的意思么?比如下面条,烧3分钟还是3分钟零1秒都可以,甚至3分半钟也没问题,也许面条会有点发软,但是明显烧1秒钟和10小时都会出大问题。那么为什么我告诉你要烧3分钟呢?因为它是一个经验值。
bdmh 2012-09-25
  • 打赏
  • 举报
回复
zoomdelta 就是个计算比例,跟后面都有关联的,这个不是死的,你可以根据你具体的情况去设置

8,834

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 组件/控件开发
社区管理员
  • 组件/控件开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧