代码运行效果。
第一次写C#,可能有问题。
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.Windows.Forms;
9
10 using System.Runtime.InteropServices; // 增加
11
12
13 namespace FirstCs
14 {
15 public partial class Form1 : Form
16 {
17 public Form1()
18 {
19 InitializeComponent();
20 this.Paint += new PaintEventHandler(Form1_Paint); // 增加
21 }
22
23 // 增加 响应绘制
24 void Form1_Paint(object sender, PaintEventArgs e)
25 {
26 IntPtr hdc = e.Graphics.GetHdc();
27 TextOut(hdc, 50, 50, "12345", 5);
28 Rectangle(hdc, 70, 70, 200, 200);
29 e.Graphics.ReleaseHdc( hdc );
30 }
31
32 // 增加 API 函数声明 begin
33 [DllImport("gdi32.dll", CharSet = CharSet.Auto)]
34 private static extern bool TextOut(IntPtr hdc, int nXStart, int nYStart, string lpString, int cbString);
35
36 [DllImport("gdi32.dll", CharSet = CharSet.Auto)]
37 private static extern bool Rectangle(IntPtr hdc, int le, int to, int ri, int bo);
38 // end
39
40 private void button1_Click(object sender, EventArgs e)
41 {
42 MessageBox.Show("FirstBtn");
43 }
44 }
45 }
46