基本上我要做的就是让我的绘图工作更轻松。
回到VB6的时代,有一种名为Scalewidth和Scaleheight的东西,我可以将它们设置为自定义值。防爆。 100.
然后,当我需要在可用空间的中心绘制一个点时,我会将其绘制为50,50。
.Net中有什么方法可以获得类似的功能吗?
因此,无论画布大小如何,我都可以使用绝对坐标绘制它。
答案 0 :(得分:1)
我不知道是否有办法在.NET中实现这一点,但您可以自己轻松实现:
// Unscaled coordinates = x, y; canvas size = w, h;
// Scaled coordinates = sx, sy; Scalewidth, Scaleheight = sw, sh;
x = (sx / sw) * w;
y = (sy / sh) * h;
// Or the other way round
sx = (x / w) * sw;
sy = (y / h) * sh;
答案 1 :(得分:1)
首先,为什么不使用Graphics.ScaleTransform而不是自己处理所有缩放?类似的东西:
e.Graphics.ScaleTransform(
100.0 / this.ClientSize.Width,
100.0 / this.ClientSize.Height );
你的代码会更加清晰,我敢打赌啤酒会更快一点。
其次,如果您坚持使用cnvX / rcnvX函数,请确保使用this.ClientSize.Width(高度相同)而不是“this.Width”。
答案 2 :(得分:0)
Schnaader有正确的想法......最终我实现了四个功能来实现这一目标。功能在
之下private float cnvX(double x)
{
return (float)((Width / 100.00) * x);
}
private float rcnvX(double x)
{
return (float)(x / Width) * 100;
}
private float rcnvY(double y)
{
return (float)((y / Height) * 100);
}
private float cnvY(double y)
{
return (float)((Height / 100.00) * y);
}