增加椭圆的宽度

本文关键字:增加 | 更新日期: 2023-09-27 18:13:01

我想编写一个程序,允许用户调整在窗体上绘制的椭圆的宽度和高度。下面是我当前的代码:

using (Graphics graphics = CreateGraphics())
{
    graphics.FillEllipse(new SolidBrush(colorDlg.Color), e.X, e.Y, x, y);
}

此代码是在MouseMove事件处理程序中编写的。问题是,我希望用户能够通过单击按钮或菜单条来增加宽度x和高度y。复杂的是,这些控件的事件处理程序不接受MouseEventArgs作为参数,因此编译器会给出e.X和e.Y的错误。

欢迎提出任何有用的意见。谢谢。

增加椭圆的宽度

试试这样:

private int x;
private int y;
private int width;
private int height;
private SomeForm()
{
    // Initialize ellipse position and size with some values
    ...
}
private void btnIncreaseWidth_Click(object sender, EventArgs e)
{
    width += 5; // Increase width by 5 pixels
    Invalidate();
}
private void btnDecreaseWidth_Click(object sender, EventArgs e)
{
    width -= 5; // Decrease width by 5 pixels
    Invalidate();
}
private void btnIncreaseHeight_Click(object sender, EventArgs e)
{
    height += 5; // Increase height by 5 pixels
    Invalidate();
}
private void btnDecreaseHeight_Click(object sender, EventArgs e)
{
    height -= 5; // Decrease height by 5 pixels
    Invalidate();
}
private void SomeForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
    Graphics g = e.Graphics;
    g.FillEllipse(new SolidBrush(colorDlg.Color), x, y, width, height);
}

假设你有4个按钮:两个增加/减少宽度,两个增加/减少高度。按下按钮后,相应的维度被更改,表单的内容无效(=>重新绘制)。