c#畫直線


c#畫直線

本文實例講述了C#使用GDI繪制矩形的方法。分享給大家供大家參考。具體實現方法如下:

1

2

3

Pen p = new Pen(Color.Black,2);

Graphics g = CreateGraphics();

g.DrawRectangle(p,200,200,100,100);

 

代碼

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

namespace WindowsFormsApp11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //創建Graphics對象
            Graphics GPS = this.CreateGraphics();
            //創建黑色pen對象
            Pen MyPen = new Pen(Color.Black, 2f);
            //確定起點和終點
            Point pt1 = new Point(70, 20);
            Point pt2 = new Point(200, 320);
            //使用DrawLine方法繪制直線
            GPS.DrawLine(MyPen, pt1, pt2);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Graphics GPS = this.CreateGraphics();
            Pen MyPen = new Pen(Color.Red, 2f);
            GPS.DrawLine(MyPen, 50,20,300,200);
        }
    }
}

運行結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM