1.所用头文件
#include "stdafx.h"
#include<windows.h>
#include<graphics.h>
#include<iostream>
using namespace std;
2.作画函数
void pen()
{
int x0, y0; //定义初始坐标
MOUSEMSG m; //定义鼠标消息
m = GetMouseMsg(); //获取一次鼠标消息
x0 = m.x; y0 = m.y;//获取位置并对初始位置赋值
while (true)
{
m = GetMouseMsg(); //获取一条鼠标消息
if (m.uMsg == WM_MOUSEMOVE) //如果鼠标消息等于鼠标移动
{
line(x0, y0, m.x, m.y);//画线
x0 = m.x; y0 = m.y; //更新初始坐标
}
}
}
3.主函数
int main()
{
initgraph(600,600);
setcolor(GREEN);
pen();
return 0;
}
4.运行结果