在WinForms图片框中绘制圆圈

时间:2014-10-13 17:39:31

标签: c# picturebox

This is what I have done so far。我需要做的是使用按钮绘制圆圈 局部变量。但我不知道该怎么做。

public partial class Form1 : Form
{
    Graphics paper;

    public Form1()
    {
        InitializeComponent();
        paper

    }
    //Part One 
    private void btnCircumfance_Click(object sender, EventArgs e)
    {
        double radius;
        if (double.TryParse(txtRadius.Text, out radius))
        {
            radius = 2 * Math.PI * radius;
            radius = Math.Round(radius, 2);
            lblDisplay.Text = "Circumfance is : ";
            lblAnwser.Text = Convert.ToString(radius);
        }
        else
        {
            MessageBox.Show("Please put in a number", "Stupid Human", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
    }
    //Part One
    private void btnArea_Click(object sender, EventArgs e)
    {

        double radius;
        if (double.TryParse(txtRadius.Text, out radius))
        {
            radius = Math.PI * Math.Pow(radius, 2);
            radius = Math.Round(radius, 2);
            lblDisplay.Text = "Area is : ";
            lblAnwser.Text = Convert.ToString(radius);
        }
        else
        {
            MessageBox.Show("Please put in a number", "Stupid Human", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
    }
    //Part One
    private void btnVolume_Click(object sender, EventArgs e)
    {
        double radius;
        if (double.TryParse(txtRadius.Text, out radius))
        {
            radius = (4.0/3.0) * Math.PI * Math.Pow(radius, 3);
            radius = Math.Round(radius, 2);
            lblDisplay.Text = "Volume is : ";
            lblAnwser.Text = Convert.ToString(radius);
        }
        else
        {
            MessageBox.Show("Please put in a number", "Stupid Human", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
    }
    //Part Two
    private void btnRadius_Click(object sender, EventArgs e)
    {
        double Circumfance;
        if (double.TryParse(txtCircumfance.Text, out Circumfance))
        {
            double anwser = 2 * Math.PI;
            Circumfance = Circumfance / anwser;
            Circumfance = Math.Round(Circumfance, 2);
            lblDisplayRadius.Text = "Radius is : ";
            lblAnwserRadius.Text = Convert.ToString(Circumfance);
        }
        else
        {
            MessageBox.Show("Please put in a number", "Stupid Human", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
    }
    private void btnDraw_Click(object sender, EventArgs e)
    {
        int xCord, yCord;
        int radius;
        xCord = Convert.ToInt16(txtxCord.Text);
        yCord = Convert.ToInt16(txtyCord.Text);
        radius = Convert.ToInt16(txtRadius.Text);

        //Graphics paper = e.Graphics;
        Pen blackPen = new Pen(Color.Black);
        paper.DrawEllipse(blackPen, xCord, yCord, radius, radius); 

        Refresh();
    }
}

2 个答案:

答案 0 :(得分:3)

在Paint例程中进行所有绘图。

首先,从btnDraw_Click中删除所有代码。相反,在那里,只需把:
    me.invalidate(true);或类似的

然后为图片框的'Paint'事件创建一个事件处理程序。

在那里,你将通过SEnder作为对象,e作为PaintArgs。 PaintArgs持有密钥。

Pen blackpen ... 
_e.Graphics_.DrawEllipse(....

您可能必须使用IF语句来保护它,直到所有参数都被回答为止。当窗口需要时,绘画将发生,而不仅仅是“绘制”按钮无效时。如果您不希望它们在点击“绘图”之前更新椭圆,则可以让Draw例程将“输入的参数”传输到DrawEllipse中使用的一组新参数。

有关细致入微的解释,请访问GDI +常见问题解答:
http://bobpowell.net/faqmain.aspx
并点击顶部问题'为什么我的图形会不断消失'?

答案 1 :(得分:0)

我猜你在e.Graphics遇到了问题。您可以创建对象:

Graphics a=pictureBox1.CreateGraphics();

然后你知道方法,