gpt4 book ai didi

c# - 变换鼠标坐标

转载 作者:行者123 更新时间:2023-11-30 16:49:01 26 4
gpt4 key购买 nike

我正在制作一个图形程序,但我卡在了需要让鼠标坐标等于图形比例的地方。对于 picturebox,我使用 transform 来缩放我的图形:

RectangleF world = new RectangleF(wxmin, wymin, wwid, whgt);
PointF[] device_points =
{
new PointF(0, PictureBox1.ClientSize.Height),
new PointF(PictureBox1.ClientSize.Width, PictureBox1.ClientSize.Height),
new PointF(0, 0),
};
Matrix transform = new Matrix(world, device_points);
gr.Transform = transform;

enter image description here我正在使用 MouseMove 功能。有没有办法转换鼠标坐标?当我将鼠标放在 x=9 上时,我需要鼠标坐标为 9。

private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Console.WriteLine(e.X);
}

最佳答案

正如 Hans 的评论所暗示的,您可以使用第二个 Matrix 来完成此操作。您可以通过复制原始 Matrix 并调用副本的 Invert() 方法来获得它,或者您可以从头开始创建新的 Matrix将您的输入矩形与原始矩形反转。

恕我直言,求逆更容易,但这确实意味着您需要创建逆矩阵并将其存储在某个地方。例如:

    Matrix transform = new Matrix(world, device_points);
gr.Transform = transform;
inverseTransform = transform.Clone();
inverseTransform.Invert();

其中 inverseTransform 是您类中的一个字段而不是局部变量,以便您的鼠标处理代码稍后可以使用它。

如果以后必须构造Matrix,可以这样做:

RectangleF device = new RectangleF(new Point(), PictureBox1.ClientSize);
PointF[] world_points =
{
new PointF(wxmin, wymin + whgt),
new PointF(wxmin + wwid, wymin + whgt),
new PointF(wxmin, wymin),
};
Matrix inverseTransform = new Matrix(device, world_points);

无论哪种情况,您只需使用 Matrix.TransformPoints()鼠标处理代码中的方法将逆变换应用于鼠标坐标以返回到您的世界坐标。

关于c# - 变换鼠标坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37334305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com