-1

我需要一些关于从哪里开始修改该程序的想法,以便当我单击picCanvas(图片框)时,该位置会出现一个圆圈。到目前为止,程序绘制了一个同心圆图案。

所有代码如下:

    private Random randClick;
    private Graphics paper;
    private Brush bbrush;
    private Pen pen;
    private int circleSize = 30;
    public Form1()
    {
        InitializeComponent();
        randClick = new Random();

        paper = picCanvas.CreateGraphics();

    }

    private void picCanvas_Click(object sender, EventArgs e)
    {
        int x, y;

        x = picCanvas.Height / 2;
        y = picCanvas.Width / 2;

        Color color = Color.FromArgb(randClick.Next(0, 256), randClick.Next(0, 256), randClick.Next(0, 256));
        Pen pen = new Pen(color);
        pen.Width = 3;

        circleSize += 10;
        paper.DrawEllipse(pen, x - circleSize/2, y - circleSize/2, circleSize, circleSize);

    }
4

3 回答 3

2

您需要使用MouseClickeven 和 useXYproperties ofMouseEventArgs来获取您单击的点。

private void picCanvas_MouseClick(object sender, MouseEventArgs e)
{
    int x, y;

    x = picCanvas.Height / 2;
    y = picCanvas.Width / 2;

    Color color = Color.FromArgb(randClick.Next(0, 256), randClick.Next(0, 256), randClick.Next(0, 256));
    Pen pen = new Pen(color);
    pen.Width = 3;

    circleSize += 10;
    paper.DrawEllipse(pen, e.X - circleSize / 2, e.Y - circleSize / 2, circleSize, circleSize);
}
于 2013-11-07T23:20:46.143 回答
2

第一件事:你应该订阅MouseClick事件而不是Click- 这样你就可以访问提供的鼠标位置和按钮MouseEventArgs

第二件事:您尝试实现的方法可能不会持久 - 在最小化和恢复窗口后DrawEllipse不会再次调用。您必须将绘图方法添加到Paint事件。下面的例子:

Point p = Point.Empty; // stores location of last mouseclick
bool clicked = false;  // is picturebox clicked (if yes - circle should be drawn)

private void pictureBox1_MouseClick( object sender, MouseEventArgs e )
{
    p = e.Location;         // capture mouse click position
    clicked = true;         // notify the circle has to be drawn
    pictureBox1.Refresh();  // force refresh of the control
}
private void pictureBox1_Paint( object sender, PaintEventArgs e )
{
    // if there's a circle to be drawn
    if ( clicked )
    {
        Graphics g = e.Graphics;   // grab graphics object
        g.DrawEllipse( Pens.Yellow, p.X - 4, p.Y - 4, 8, 8 );  // draw ellipse (a small one in this case)
    }
}
于 2013-11-07T23:21:24.897 回答
1

将每个圆圈的位置和颜色信息存储在类级别的 List<> 中。在此示例中,我使用了元组,但您也可以使用自定义类。在 Paint() 事件中,您遍历 List 中的所有条目并呈现它们:

在此处输入图像描述

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
        this.Load += new EventHandler(Form1_Load);
        picCanvas.Paint += new PaintEventHandler(picCanvas_Paint);
        picCanvas.MouseDown += new MouseEventHandler(picCanvas_MouseDown);
    }

    private int circleSize = 30;
    private Random R = new Random();
    private List<Color> NamedColors = new List<Color>();
    private List<Tuple<Point, Color>> Circles = new List<Tuple<Point, Color>>();

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach (Color C in System.ComponentModel.TypeDescriptor.GetConverter(typeof(Color)).GetStandardValues())
        {
            if (C.IsNamedColor)
            {
                NamedColors.Add(C);
            }
        }
    }

    void picCanvas_MouseDown(object sender, MouseEventArgs e)
    {
        Tuple<Point, Color> circle = new Tuple<Point, Color>(
            new Point(e.X, e.Y), 
            NamedColors[R.Next(NamedColors.Count)]);
        Circles.Add(circle);
        picCanvas.Invalidate();
    }

    void picCanvas_Paint(object sender, PaintEventArgs e)
    {
        foreach (Tuple<Point, Color> circle in Circles)
        {
            Rectangle rc = new Rectangle(circle.Item1, new Size(1, 1));
            rc.Inflate(circleSize / 2, circleSize / 2);
            using (Pen pen = new Pen(circle.Item2, 3))
            {
                e.Graphics.DrawEllipse(pen, rc);
            }
        }
    }

}
于 2013-11-07T23:46:35.533 回答