C#图形绘制和类实例化

时间:2014-10-27 03:10:12

标签: c# winforms class drawing

在教科书中练习,但卡住了。我需要画一个简单的棒图。但是我需要创建Face,Body类并在main中调用它。已经完成了谷歌搜索,但似乎无法找到答案。

以下是尝试这样做:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

public class stickFace : Form
{
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics draw = e.Graphics;
        Pen black = new Pen(Color.Black, 3);

        draw.DrawEllipse(black, 20, 20, 100, 100);

        base.OnPaint(e);
    }
}

public class stickBody : Form
{
    protected override void OnPaint(PaintEventArgs e)
    {
        // Draw Line codes goes here
        base.OnPaint(e);
    }
}

public class StickFigure : Form
{
    public StickFigure()
    {
        Size = new Size(500, 300);
        Text = "Stick Figure";
        BackColor = Color.White;
    }

    public static void Main()
    {
        // Call the stickface and stickBody Class
    }
}

如何在Main方法中实例化StickFace和StickBody?或者我的代码完全错误。

1 个答案:

答案 0 :(得分:1)

Program.cs的

 static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new stickFace());
    }
}

StickFace.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

public class stickFace : Form
{
     public stickFace()
    {
        new stickBody(this);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics draw = e.Graphics;
        Pen black = new Pen(Color.Black, 3);

        draw.DrawEllipse(black, 20, 20, 100, 100);

        base.OnPaint(e);
    }
}

StickBody.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

 public class stickBody : Form
{
    public stickBody(stickFace obj)
    {

    }

    protected override void OnPaint(PaintEventArgs e)
    {//lines of code.....

        base.OnPaint(e);
    }
}