1. 放置1个【画布】和1个【功能键】
此处画布大小为400*400

2. 设置功能键属性


3.代码

3.1包含头文件(用于取随机数,如有另外数据来源,则不需要)
#include <iostream>// 核心头文件:C++11 随机数库#include <random>// 用于确保随机数引擎只初始化一次(可选但推荐)#include <mutex>
3.2获取随机数函数(用于取随机数,如有另外数据来源,则不需要)
// 获取随机数函数int getRandomNumber(int min, int max) {// 1. 静态变量:确保随机数引擎和分布只初始化一次(避免重复播种导致随机数重复)static std::random_device rd; // 生成真随机种子(部分平台为伪随机,但优于time)static std::mt19937 gen(rd()); // 梅森旋转算法引擎,速度快、分布均匀static std::uniform_int_distribution<int> dist(min, max); // 均匀分布// 2. 线程安全保护(如果多线程调用需加,单线程可省略)static std::mutex mtx;std::lock_guard<std::mutex> lock(mtx);// 3. 生成并返回随机数return dist(gen);}
3.3定义上一点坐标(使两线首尾相连)
// 保存上一点坐标int lastx = getRandomNumber(0, 400);int lasty = getRandomNumber(0, 400);
3.4直线绘制代码
void Frm04::wMButton1_clk_cb(uint16_t code, LvEvent e) {/*wMButton1(画直线)的点击事件*/int xx = getRandomNumber(0, 400);//画布的x范围内int yy = getRandomNumber(0, 400);//画布的y范围内int xy[4] = {lastx, lasty, xx, yy};//drawLine的第1个参数int cnt = 2;//drawLine的第2个参数LvDrawLineDsc dsc; //drawLine的第3个参数dsc.color.full = 0xff0000;//颜色dsc.width = 10;//线宽dsc.dash_width = 10;//如果是虚线,实长部分dsc.dash_gap = 5;//如果是虚线,空白部分dsc.opa = 128;//线的不透明度0~255dsc.round_start = 1;//起点是否加圆帽 0~否 1~是dsc.round_end = 0;//终点是否加圆帽 0~否 1~是wMCanvas1->drawLine(xy, cnt, &dsc);//更新上一点坐标lastx = xx;lasty = yy;}
4. 运行
