本例实现了鼠标点击并拖动时绘制鼠标轨迹, 并将鼠标当前点与按下点进行连线。
示例一: 最简单的绘制到屏幕, 很多初学者喜欢直接这样写c代码, 其实有一些弊端, 请看下方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#define SHOW_CONSOLE //显示控制台窗口 #include <graphics.h> #include <cstdio> int main() { initgraph(640, 480); bool isDown = false; int startX, startY; int lastX, lastY; mouse_msg msg; int colorIndex = 0; for(; is_run(); ) { msg = getmouse(); switch (msg.msg) { case mouse_msg_down: startX = msg.x; startY = msg.y; lastX = msg.x; lastY = msg.y; printf("mouse down: %d, %d\n", msg.x, msg.y); isDown = true; break; case mouse_msg_up: //这里并没有什么卵用 printf("mouse up: %d, %d\n", msg.x, msg.y); isDown = false; break; case mouse_msg_move: if(isDown) { do { color_t color = hsv2rgb(colorIndex++ % 360, 1, 1); //使用hsv颜色轮换 setcolor(color); line(startX, startY, msg.x, msg.y); //绘制当前点和按下的点 line(lastX, lastY, msg.x, msg.y); //绘制当前点和上一个点 lastX = msg.x; lastY = msg.y; printf("mouse moved to: %d, %d\n", msg.x, msg.y); msg = getmouse(); }while(mousemsg() && msg.is_move()); if(msg.is_up()) isDown = false; } break; case mouse_msg_wheel: printf("mousewheel: %d\n", msg.wheel); break; default: printf("呵呵\n"); break; } } closegraph(); return 0; } |
示例二: 将示例1 里面的代码进行简单的重构, 加入一些OOP的思想, 即可得到如下的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
#define SHOW_CONSOLE //显示控制台窗口 #include <graphics.h> #include <cstdio> #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 class TestClass { public: TestClass() : m_isDwon(false) {} void mouseDown(int x, int y) { m_startX = x; m_startY = y; m_lastX = x; m_lastY = y; printf("mouse down: %d, %d\n", x, y); m_isDwon = true; } void mouseUp(int x, int y) { //这里并没有什么卵用 printf("mouse up: %d, %d\n", x, y); m_isDwon = false; } void mouseMove(int x, int y) { if(m_isDwon) { color_t color = hsv2rgb(m_hsvColorIndex++ % 360, 1, 1); //使用hsv颜色轮换 setcolor(color); line(m_startX, m_startY, x, y); //绘制当前点和按下的点 line(m_lastX, m_lastY, x, y); //绘制当前点和上一个点 m_lastX = x; m_lastY = y; } printf("mouse moved to: %d, %d\n", x, y); } //这里没啥用 void mouseWheel(int wheel) { printf("mousewheel: %d\n", wheel); } void drawLineFromMouseDown(int x, int y) { line(m_startX, m_startY, x, y); } private: bool m_isDwon; int m_startX, m_startY; int m_lastX, m_lastY; int m_hsvColorIndex; }; int main() { initgraph(WINDOW_WIDTH, WINDOW_HEIGHT); TestClass testObj; //创建一个对象, 简单的OOP 思维 int colorIndex = 0; for(; is_run(); delay_fps(60)) { while (mousemsg()) { mouse_msg msg = getmouse(); switch (msg.msg) { case mouse_msg_down: testObj.mouseDown(msg.x, msg.y); break; case mouse_msg_up: testObj.mouseUp(msg.x, msg.y); break; case mouse_msg_move: testObj.mouseMove(msg.x, msg.y); break; case mouse_msg_wheel: testObj.mouseWheel(msg.wheel); break; default: //执行不到的 break; } } } closegraph(); return 0; } |
示例三: 本例加入一个PIMAGE作为缓存图像, 显示出绘图的轨迹, 但是仅在鼠标松开时存储松开时的那一条线, 与前面两个demo不同, 请看示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
#define SHOW_CONSOLE //显示控制台窗口 #include <graphics.h> #include <cstdio> #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 class TestClass { public: TestClass() : m_isDwon(false) {} void mouseDown(int x, int y) { m_startX = x; m_startY = y; m_lastX = x; m_lastY = y; printf("mouse down: %d, %d\n", x, y); m_isDwon = true; } void mouseUp(int x, int y) { //这里并没有什么卵用 printf("mouse up: %d, %d\n", x, y); m_isDwon = false; } void mouseMove(int x, int y) { if(m_isDwon) { m_color = hsv2rgb(m_hsvColorIndex++ % 360, 1, 1); //使用hsv颜色轮换 setcolor(m_color); line(m_startX, m_startY, x, y); //绘制当前点和按下的点 line(m_lastX, m_lastY, x, y); //绘制当前点和上一个点 m_lastX = x; m_lastY = y; } printf("mouse moved to: %d, %d\n", x, y); } //这里没啥用 void mouseWheel(int wheel) { printf("mousewheel: %d\n", wheel); } void drawLineFromMouseDown(int x, int y, PIMAGE target = NULL) { setcolor(m_color, target); line(m_startX, m_startY, x, y, target); } bool shouldDraw() { return m_isDwon; } private: bool m_isDwon; int m_startX, m_startY; int m_lastX, m_lastY; color_t m_color; int m_hsvColorIndex; }; int main() { initgraph(WINDOW_WIDTH, WINDOW_HEIGHT); setrendermode(RENDER_MANUAL); //需要每一帧高速重绘时启用. //由于每一帧重绘, 需要图像缓存。 PIMAGE pBufferImage = newimage(640, 480); cleardevice(pBufferImage); //作为缓存使用 TestClass testObj; //创建一个对象, 简单的OOP 思维 int colorIndex = 0; mouse_msg msg; for(; is_run(); delay_fps(60)) { putimage(0, 0, pBufferImage); while (mousemsg()) { msg = getmouse(); switch (msg.msg) { case mouse_msg_down: testObj.mouseDown(msg.x, msg.y); break; case mouse_msg_up: testObj.mouseUp(msg.x, msg.y); testObj.drawLineFromMouseDown(msg.x, msg.y, pBufferImage); break; case mouse_msg_move: testObj.mouseMove(msg.x, msg.y); break; case mouse_msg_wheel: testObj.mouseWheel(msg.wheel); break; default: //执行不到的 break; } } if(testObj.shouldDraw()) { testObj.drawLineFromMouseDown(msg.x, msg.y); } } closegraph(); return 0; } |
近期评论