image.png

    image.png

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. namespace _126_使用GDI绘制简单图形
    11. {
    12. public partial class Form1 : Form
    13. {
    14. public Form1()
    15. {
    16. InitializeComponent();
    17. }
    18. private void btnLine_Click(object sender, EventArgs e)
    19. {
    20. //创建GDI对象
    21. Graphics g = this.CreateGraphics(); //new Graphics();没有构造函数
    22. //创建画笔对象
    23. Pen pen = new Pen(Brushes.Black);
    24. //创建两个点
    25. Point p1 = new Point(30, 50);
    26. Point p2 = new Point(250, 250);
    27. //开始画
    28. g.DrawLine(pen, p1, p2);
    29. }
    30. int i = 0;
    31. private void Form1_Paint(object sender, PaintEventArgs e)
    32. {
    33. i++;
    34. lblRefresh.Text = i.ToString();
    35. //创建GDI对象
    36. Graphics g = this.CreateGraphics();
    37. //创建画笔对象
    38. Pen pen = new Pen(Brushes.Black);
    39. //创建两个点
    40. Point p1 = new Point(30, 50);
    41. Point p2 = new Point(250, 250);
    42. //开始画
    43. g.DrawLine(pen, p1, p2);
    44. }
    45. private void btbRectangle_Click(object sender, EventArgs e)
    46. {
    47. Graphics g = this.CreateGraphics();
    48. //DrawRectangle()需要两个对象,画笔和矩形
    49. Pen pen = new Pen(Brushes.Red);
    50. Size size = new Size(80, 80);
    51. Rectangle rec = new Rectangle(new Point(50, 50), size); //Rectangle rec = new Rectangle(50,50,80,80 );
    52. g.DrawRectangle(pen, rec);
    53. }
    54. private void btnSector_Click(object sender, EventArgs e)
    55. {
    56. Graphics g = this.CreateGraphics();
    57. //DrawPie()方法需要的四个参数:
    58. //1、画笔
    59. Pen pen = new Pen(Brushes.Blue);
    60. //2、矩形
    61. Size size = new Size(180, 480);
    62. Rectangle rec = new Rectangle(new Point(150, 150), size);
    63. //3、4、两个角度 startAngle,sweepAngle
    64. //画扇形
    65. g.DrawPie(pen, rec, 270, 60);
    66. }
    67. private void btnText_Click(object sender, EventArgs e)
    68. {
    69. Graphics g = this.CreateGraphics();
    70. //画文本
    71. g.DrawString("韦晓龙牛逼", new Font("宋体", 20, FontStyle.Bold), Brushes.DarkViolet, new Point(300, 300));
    72. }
    73. }
    74. }