📄 mainform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace UsingPen
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Paint(object sender, PaintEventArgs e)
{
// 四个矩形的颜色
Color[] colors = new Color[]{
Color.Red,
Color.Green,
Color.Blue,
Color.Yellow
};
// 用于计算矩形坐标的值
// initX、initY:最外面的矩形的左上角位置
// initW、initH:最外面的矩形的大小
// incr:矩形之间的距离
int initX = 10, initY = 10;
int initW = 200, initH = 150;
int incr = 20;
// 创建画笔
Pen p = new Pen(Color.Empty);
for(int i = 0; i < 4; i++)
{
// 改变画笔的颜色和宽度
p.Color = colors[i];
p.Width = (i + 1) * 3.0f;
// 计算当前矩形的位置和大小
int x = initX + i * incr;
int y = initY + i * incr;
int w = initW - i * incr * 2;
int h = initH - i * incr * 2;
// 绘制矩形
e.Graphics.DrawRectangle(p, x, y, w, h);
}
// 释放画笔
p.Dispose();
#if false
// 绘制虚线
using(Pen p2 = new Pen(Color.Black))
{
p2.DashStyle = DashStyle.Dash;
e.Graphics.DrawLine(p2, 0, 0, 200, 200);
}
#endif
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -