📄 form1.cs
字号:
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 FormDraw
{
public partial class DrawForm : Form
{
Color col = Color.Green;
Pen pen = new Pen(Color.Green, 3);
Brush brush = new SolidBrush(Color.Green);
bool fill;
public DrawForm()
{
InitializeComponent();
setColButton.BackColor = col; // col为类变量,初值为Color.Green
fill = false;
}
private void OnFileOpen(object sender, EventArgs e)
{
if (openFileDlg.ShowDialog() == DialogResult.OK)
{
string fn = openFileDlg.FileName;
MessageBox.Show(string.Format("你选择的文件名为:{0}", fn), "文件名");
}
}
private void OnFileNew(object sender, EventArgs e)
{
MessageBox.Show("创建新文档?", "请选择", MessageBoxButtons.YesNo);
}
private void OnExit(object sender, EventArgs e)
{
Application.Exit();
}
private void OnFileOpenMouseHover(object sender, MouseEventArgs e)
{
promptLabel.Text = "打开文档文件";
}
private void OnFileOpenMouseLeave(object sender, EventArgs e)
{
promptLabel.Text = "就绪";
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (mouseDown) // mouseDown为bool型类变量
{
int l, t, r, b; // 定义矩形坐标的变量
Graphics graph = drawPanel.CreateGraphics(); // 创建图形对象
IntPtr hdc = graph.GetHdc(); // 获取DC句柄
Point point = new Point(0, 0); // 定义点对象(用于MoveToEx函数的输出参数)
IntPtr hPen = GDI.CreatePen(2, 0, 0x808080); // 创建灰色单像素点线笔
IntPtr hOldPen = GDI.SelectObject(hdc, hPen); // 选入灰笔
IntPtr hOldBrush = GDI.SelectObject(hdc, GDI.GetStockObject(5)); // 选入空刷
GDI.SetBkMode(hdc, 1); // 设置透明背景模式
GDI.SetROP2(hdc, 7); // 设置异或绘图模式
switch (drawType)
{
case DrawTypes.line: // 画动态直线
if (drawn) // 擦除,下同
{
GDI.MoveToEx(hdc, x0, y0, out point);
GDI.LineTo(hdc, x, y);
}
GDI.MoveToEx(hdc,x0, y0, out point);
GDI.LineTo(hdc, e.X, e.Y);
break;
case DrawTypes.rect: // 画动态矩形
if (drawn)
{
l = Math.Min(x0, x); r = Math.Max(x0, x);
t = Math.Min(y0, y); b = Math.Max(y0, y);
GDI.Rectangle(hdc, l, t, r, b);
}
l = Math.Min(x0, e.X); r = Math.Max(x0, e.X);
t = Math.Min(y0, e.Y); b = Math.Max(y0, e.Y);
GDI.Rectangle(hdc, l, t, r, b);
break;
case DrawTypes.ellipse: // 画动态椭圆
if (drawn)
{
l = Math.Min(x0, x); r = Math.Max(x0, x);
t = Math.Min(y0, y); b = Math.Max(y0, y);
GDI.Ellipse(hdc, l, t, r, b);
}
l = Math.Min(x0, e.X); r = Math.Max(x0, e.X);
t = Math.Min(y0, e.Y); b = Math.Max(y0, e.Y);
GDI.Ellipse(hdc, l, t, r, b);
break;
}
GDI.SelectObject(hdc, hOldPen); // 选入老笔
GDI.DeleteObject(hPen); // 删除创建的灰笔
GDI.SetROP2(hdc, 7); // 设置覆盖绘图模式
graph.ReleaseHdc(hdc); // 释放DC句柄
x = e.X; y = e.Y; // 记录当前终点
drawn = true; // 设置已绘图为真
}
xValueLabel.Text = e.X.ToString();
yValueLabel.Text = e.Y.ToString();
}
private void OnSetCol(object sender, EventArgs e)
{
setColDlg.Color = col;
if (setColDlg.ShowDialog() == DialogResult.OK)
{
col = setColDlg.Color;
pen = new Pen(col, 3);
brush = new SolidBrush(col);
setColButton.BackColor = col;
}
}
enum DrawTypes { line, rect, ellipse }
DrawTypes drawType = DrawTypes.line;
bool mouseDown = false, drawn = false;
int x0, y0, x, y;
public class GDI
{
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
internal static extern IntPtr CreatePen(
int fnPenStyle,
int nWidth,
uint crColor
);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
internal static extern IntPtr GetStockObject(
int fnObject
);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
internal static extern IntPtr SelectObject(
IntPtr hdc,
IntPtr hgdiobj
);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
internal static extern bool DeleteObject(
IntPtr hObject
);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
internal static extern int SetBkMode(
IntPtr hdc,
int nBkMode
);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
internal static extern int SetROP2(
IntPtr hdc,
int nDrawMode
);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
internal static extern bool MoveToEx(
IntPtr hdc,
int X, int Y,
out Point point
);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
internal static extern bool LineTo(
IntPtr hdc,
int nXEnd, int nYEnd
);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
internal static extern bool Rectangle(
IntPtr hdc,
int ulCornerX, int ulCornerY,
int lrCornerX, int lrCornerY
);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
internal static extern bool Ellipse(
IntPtr hdc,
int x1, int y1, int x2, int y2
);
}
private void OnMouseDown(object sender, MouseEventArgs e)
{
mouseDown=true;
x0 = e.X; y0 = e.Y;
x = x0; y = y0;
}
private void OnMouseUp(object sender, MouseEventArgs e)
{
if (mouseDown)
{
int l, t, w, h;
Graphics graph = drawPanel.CreateGraphics();
if (drawn) // 擦除
{
int r, b;
IntPtr hdc = graph.GetHdc();
Point point;
IntPtr hPen = GDI.CreatePen(2, 0, 0x808080);
IntPtr hOldPen = GDI.SelectObject(hdc, hPen);
IntPtr hOldBrush = GDI.SelectObject(hdc, GDI.GetStockObject(5));
GDI.SetBkMode(hdc, 1);
GDI.SetROP2(hdc, 7);
switch (drawType)
{
case DrawTypes.line:
GDI.MoveToEx(hdc, x0, y0, out point);
GDI.LineTo(hdc, x, y);
break;
case DrawTypes.rect:
l = Math.Min(x0, x); r = Math.Max(x0, x);
t = Math.Min(y0, y); b = Math.Max(y0, y);
GDI.Rectangle(hdc, l, t, r, b);
break;
case DrawTypes.ellipse:
l = Math.Min(x0, x); r = Math.Max(x0, x);
t = Math.Min(y0, y); b = Math.Max(y0, y);
GDI.Ellipse(hdc, l, t, r, b);
break;
}
GDI.SelectObject(hdc, hOldPen);
GDI.DeleteObject(hPen);
GDI.SetROP2(hdc, 7);
graph.ReleaseHdc(hdc);
}
switch (drawType) // 绘图
{
case DrawTypes.line:
graph.DrawLine(pen, x0, y0, e.X, e.Y);
break;
case DrawTypes.rect:
l = Math.Min(x0, e.X); t = Math.Min(y0, e.Y);
w = Math.Abs(e.X - x0); h = Math.Abs(e.Y - y0);
if (fill== true) graph.FillRectangle(brush, l, t, w, h);
graph.DrawRectangle(pen, l, t, w, h);
break;
case DrawTypes.ellipse:
l = Math.Min(x0, e.X); t = Math.Min(y0, e.Y);
w = Math.Abs(e.X - x0); h = Math.Abs(e.Y - y0);
if (fill == true) graph.FillEllipse(brush, l, t, w, h);
graph.DrawEllipse(pen, l, t, w, h);
break;
}
mouseDown = false;
}
}
private void OnSetDline(object sender, EventArgs e)
{
drawType = DrawTypes.line;
}
private void OnSetDrect(object sender, EventArgs e)
{
drawType = DrawTypes.rect;
}
private void OnSetDellipse(object sender, EventArgs e)
{
drawType = DrawTypes.ellipse;
}
private void OnSetFill(object sender, EventArgs e)
{
fill = !fill;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -