我目前正试图绘制一个简单的矩形,其大小和位置与表单大小有关。 我想我得到了相对于表单大小的矩形的宽度和高度。不过,我似乎无法得到x和y点来做同样的事情。相对于表单大小绘制一个矩形
代码:
using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
public class DrawFill : Form
{
public DrawFill()
{
Size = new Size(500, 300);
Text = "Draw and Fill";
BackColor = Color.White;
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen blue = new Pen(Color.Blue, 3);
// Width is 100, Height is 50
g.DrawRectangle(blue, 10,20, (Width - 100)/3, (Height - 50)/3));
g.FillRectangle(Brushes.Red, 150, 20, (Width - 100)/3, (Height - 50)/3);
base.OnPaint(e);
}
public static void Main()
{
Application.Run(new DrawFill());
}
}
我每次放大两个矩形相互重叠的形式,这让我觉得在x,y,不按屏幕尺寸发生变化。有人能指引我走向正确的方向吗?
改为使用ClientSize属性。在Resize事件处理程序中调用Invalidate(),以便使用新大小重新绘制它。并在构造函数中将ResizeRedraw和DoubleBuffered属性设置为* true *。 – 2014-10-22 06:23:56