C语言实现鼠标绘图

使用C语言+EGE图形库(Easy Graphics Engine)。思路是通过不断绘制直线来实现鼠标绘图的功能,前一个时刻鼠标的坐标作为直线的起点,现在时刻的坐标作为终点(严格意义是线段而不是直线)。

 1 #include "graphics.h"
2
3 int main() {
4 initgraph(1000, 600, 0);
5 setcolor(GREEN);
6
7 // 开启抗锯齿,使线条更平滑
8 ege_enable_aa(true);
9
10 // 前一个时刻鼠标的坐标,后一个时刻鼠标的坐标
11 // 用这两点坐标绘制直线,实现连续的绘图动作
12 int pre_x, pre_y, now_x, now_y;
13 int toDraw = 0;
14
15 for (; is_run();) {
16 // 时刻获取新的鼠标消息
17 mouse_msg msg = {0};
18 msg = getmouse();
19
20 // 1. 鼠标左键有消息,判断是否绘图操作
21 if(msg.is_left()) {
22 // 若左键按下,则开始绘图
23 if(msg.is_down()) {
24 toDraw = 1;
25 }
26 // 否则不绘图
27 else {
28 toDraw = 0;
29 }
30 }
31
32 // 2. 更换坐标信息
33 // 前一个时刻的坐标为直线起点
34 // 现在时刻的坐标为直线终点
35 // 记录现在时刻的坐标,作为下一个时刻的“前一个时刻的坐标”
36 pre_x = now_x;
37 pre_y = now_y;
38 now_x = msg.x;
39 now_y = msg.y;
40
41 // 实时显示鼠标坐标信息
42 if(msg.x>=0&&msg.x<=1000 && msg.y>=0&&msg.y<=600) {
43 xyprintf(1,1,"[%d,%d]",msg.x, msg.y);
44 }
45 // 3. 用绘制直线的方式实现鼠标连续绘图
46 if(toDraw) {
47 line(pre_x, pre_y, now_x, now_y);
48 }
49 }
50
51 getch();
52 closegraph();
53 return 0;
54 }

效果展示:

C语言实现鼠标绘图

上一篇:MVC加载分布页的三种方式


下一篇:python使用pip安装第三方库(工具包)速度慢、超时、失败的解决方案