Your Ads Here

Thursday, 23 February 2012

Draw a triangle on the form using C#.NET

It shows how to draw a triangle on the form using C#.NET
Keywords:, CSharp.NET, triangle, drawing, graphics.

First import a namespace
using System.Drawing;

Place the code in the paint event for the form.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace testc
    public partial class Form1 : Form
        Graphics gr_graphics = default(Graphics);
        //need a pen for drawing and make it black
        Pen pen_draw = new Pen(Color.Black);

        public Form1()
        private void Form1_Load(object sender, EventArgs e)

        private void Form1_Paint(object sender, PaintEventArgs e)
            gr_graphics = e.Graphics;
            //Draw a triangle on the form.
            //first have to define an array of points.
            Point[] pnt = new Point[3];
            pnt[0].X = 150;
            pnt[0].Y = 150;

            pnt[1].X = 150;
            pnt[1].Y = 200;

            pnt[2].X = 50;
            pnt[2].Y = 120;

            gr_graphics.DrawPolygon(pen_draw, pnt);

No comments:

Post a Comment