绘图STemWin篇5

Posted:   2020-09-21

Status:   Completed

Tags :   STM32 emWin STemWin

Categories :   STM32 emWin STemWin

Previous:   数值显示STemWin篇4

Next:   显示位图STemWin篇6


绘图常用函数

绘制相关功能

  • 调整矩形大小void GUI_AddRect(GUI_RECT * pDest, const GUI_RECT * pRect, int Dist)
  • 返回当前可用的绘图区域void GUI_GetClientRect(GUI_RECT * pRect)
  • 返回当前的绘图模式GUI_DRAWMODE GUI_GetDrawMode(void)
  • 返回当前的画笔大小U8 GUI_GetPenSize(void)
  • 返回指定位置的颜色索引unsigned GUI_GetPixelIndex(int x, int y)
  • 设置绘图模式GUI_DRAWMODE GUI_SetDrawMode(GUI_DRAWMODE dm)
  • 设置画笔大小U8 GUI_SetPenSize(U8 Size)

基本绘图函数

  • 为一个矩形区域填充背景颜色void GUI_ClearRect(int x0, int y0, int x1, int y1)
  • 复制屏幕中的一个矩形区域void GUI_CopyRect(int x0, int y0, int x1, int y1, int dx, int dy)
  • 绘制使用水平渐变颜色填充的矩形void GUI_DrawGradientH(int x0, int y0, int x1, int y1, GUI_COLOR Color0, GUI_COLOR Color1)
  • 绘制使用垂直渐变颜色填充的矩形void GUI_DrawGradientV(int x0, int y0, int x1, int y1, GUI_COLOR Color0, GUI_COLOR Color1)
  • 绘制使用水平渐变颜色填充的圆角矩形void GUI_DrawGradientRoundedH(int x0, int y0, int x1, int y1, int rd, GUI_COLOR Color0, GUI_COLOR Color1)
  • 绘制使用垂直渐变颜色填充的圆角矩形void GUI_DrawGradientRoundedV(int x0, int y0, int x1, int y1, int rd, GUI_COLOR Color0, GUI_COLOR Color1)
  • 绘制像素点void GUI_DrawPixel(int x, int y)
  • 绘制矩形void GUI_DrawRect(int x0, int y0, int x1, int y1)
  • 绘制矩形void GUI_DrawRectEx(const GUI_RECT * pRect)
  • 绘制圆角矩形框void GUI_DrawRoundedFrame(int x0, int y0, int x1, int y1, int r, int w)
  • 绘制圆角矩形void GUI_DrawRoundedRect(int x0, int y0, int x1, int y1, int r)
  • 绘制填充颜色的矩形void GUI_FillRect(int x0, int y0, int x1, int y1)
  • 绘制填充颜色的矩形void GUI_FillRectEx(const GUI_RECT * pRect)
  • 绘制填充颜色的圆角矩形void GUI_FillRoundedRect(int x0, int y0, int x1, int y1, int r)

Alpha 混合

  • 开启/禁止自动Alpha 混合void GUI_EnableAlpha(void)
  • 在绘制完成后保留Alpha 通道unsigned GUI_PreserveTrans(unsigned OnOff)
  • 恢复Alpha 混合到之前的状态U32 GUI_RestoreUserAlpha(GUI_ALPHA_STATE * pAlphaState)
  • 设置当前的Alpha 混合值unsigned GUI_SetAlpha(U8 Alpha)
  • 设置一个附加值,用于计算实际的Alpha 混合值U32 GUI_SetUserAlpha(GUI_ALPHA_STATE * pAlphaState, U32 UserAlpha)

绘制线

  • 绘制水平线void GUI_DrawHLine(int y0, int x0, int x1)
  • 绘制从起始坐标到终点坐标的线(绝对坐标)void GUI_DrawLine(int x0, int y0, int x1, int y1)
  • 绘制从当前位置到由X 和Y 距离指定终点的直线(相对坐标)void GUI_DrawLineRel(int dx, int dy)
  • 绘制从当前位置到指定终点的直线void GUI_DrawLineTo(int x, int y)
  • 绘制折线void GUI_DrawPolyLine(const GUI_POINT * pPoints, int NumPoints, int x0, int y0)
  • 绘制垂直线void GUI_DrawVLine(int x0, int y0, int y1)
  • 返回当前直线绘制样式U8 GUI_GetLineStyle(void)
  • 以当前位置为参考移动直线void GUI_MoveRel(int dx, int dy)
  • 将直线指针移动到指定位置void GUI_MoveTo(int x, int y)
  • 设置直线绘制样式U8 GUI_SetLineStyle(U8 Style)

绘制多边形

  • 绘制多边形的轮廓void GUI_DrawPolygon(const GUI_POINT * pPoints, int NumPoints, int x0, int y0)
  • 放大多边形void GUI_EnlargePolygon(GUI_POINT * pDest, const GUI_POINT * pSrc, int NumPoints, int Len)
  • 绘制具有颜色填充的多边形void GUI_FillPolygon(const GUI_POINT * pPoints, int NumPoints, int x0, int y0)
  • 放大多边形void GUI_MagnifyPolygon(GUI_POINT * pDest, const GUI_POINT * pSrc, int NumPoints, int Mag)
  • 按照指定角度旋转多边形void GUI_RotatePolygon(GUI_POINT * pDest, const GUI_POINT * pSrc, int NumPoints, float Angle)

绘制圆

  • 绘制圆的轮廓void GUI_DrawCircle(int x0, int y0, int r)
  • 绘制具有颜色填充的圆void GUI_FillCircle(int x0, int y0, int r)

绘制椭圆

  • 绘制椭圆的轮廓void GUI_DrawEllipse(int x0, int y0, int rx, int ry)
  • 绘制具有颜色填充的椭圆void GUI_FillEllipse(int x0, int y0, int rx, int ry)

绘制圆弧

  • 绘制指定角度的圆弧void GUI_DrawArc(int x0, int y0, int rx, int ry, int a0, int a1)

绘制线图

  • 绘制线图void GUI_DrawGraph(I16 * pay, int NumPoints, int x0, int y0)

绘制二维码

  • 创建二维码位图GUI_HMEM GUI_QR_Create(const char * pText, int PixelSize, int EccLevel, int Version)
  • 删除二维码位图void GUI_QR_Delete(GUI_HMEM hQR)
  • 绘制二维码位图void GUI_QR_Draw(GUI_HMEM hQR, int xPos, int yPos)
  • 返回包含有关二维码信息的结构体void GUI_QR_GetInfo(GUI_HMEM hQR, GUI_QR_INFO * pInfo)

绘制饼图

  • 绘制一个扇形void GUI_DrawPie(int x0, int y0, int r, int a0, int a1, int Type)

绘图

  • 修改MainTask.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "GUI.h"

#include <stdlib.h>
/*******************************************************************************
 * 全局变量
 ******************************************************************************/
GUI_RECT BasicRect = {10, 10, 50, 50};
static const unsigned aValues[] = {100, 135, 190, 240, 340, 360};
static const GUI_COLOR aColor[] = {GUI_BLUE, GUI_GREEN, GUI_RED,
                                   GUI_CYAN, GUI_MAGENTA, GUI_YELLOW};
static const char QR_TEXT[] = "http://www.st.com";
static const GUI_POINT _aPointArrow[] = {
  {  0,   0 },
  {-20, -15 },
  {-5 , -10 },
  {-5 , -35 },
  { 5 , -35 },
  { 5 , -10 },
  { 20, -15 },
};
static const GUI_POINT DashCube_BackPoint[] = {
		{ 76 /2, 104/2 },
		{ 176/2, 104/2 },
		{ 176/2,   4  /2},
		{  76/2,   4  /2}
};
static const GUI_POINT DashCube_LeftPoint[] = {
		{ 40/2, 140 /2},
		{ 76/2, 104 /2},
		{ 76/2,   4 /2},
		{ 40/2,  40 /2}
};
static const GUI_POINT DashCube_BottonPoint[] = {
		{  40/2, 140 /2},
		{ 140/2, 140 /2},
		{ 176/2, 104 /2},
		{  76/2, 104 /2}
};
static const GUI_POINT DashCube_TopPoint[] = {
		{  40/2, 40 /2},
		{ 140/2, 40 /2},
		{ 176/2,  4 /2},
		{  76/2,  4 /2},
};
static const GUI_POINT DashCube_RightPoint[] = {
		{ 140/2, 140/2 },
		{ 176/2, 104/2 },
		{ 176/2,   4/2 },
		{ 140/2,  40/2 },
};
static const 	GUI_POINT DashCube_FrontPoint[] = {
		{  40/2, 140/2},
		{ 140/2, 140/2},
		{ 140/2,  40/2},
		{  40/2,  40/2},
};
/*******************************************************************************
 * 函数
 ******************************************************************************/
/**
  * @brief 饼图绘图函数
  * @note 无
  * @param x0:饼图圆心的x坐标
  *        y0:饼图圆心的y坐标
  *        r:饼图半径
  * @retval 无
  */
static void Pie_Chart_Drawing(int x0, int y0, int r)
{
	int i, a0 = 0, a1 = 0;
	
	for(i = 0; i < GUI_COUNTOF(aValues); i++)
	{
		if(i == 0) a0 = 0;
		else a0 = aValues[i - 1];
		a1 = aValues[i];	
		GUI_SetColor(aColor[i]);
		GUI_DrawPie(x0, y0, r, a0, a1, 0);
	}
}

/**
  * @brief 二维码生成
  * @note 无
  * @param pText:二维码内容
  *        PixelSize:二维码数据色块的大小,单位:像素
  *        EccLevel:纠错编码级别
  *        x0:二维码图像在LCD的坐标x
  *        y0:二维码图像在LCD的坐标y
  * @retval 无
  */
static void QR_Code_Drawing(const char *pText, int PixelSize, int EccLevel, int x0, int y0)
{
	GUI_HMEM hQR;
	
	/* 创建二维码对象 */
	hQR = GUI_QR_Create(pText, PixelSize, EccLevel, 0);
	/* 绘制二维码到LCD */
	GUI_QR_Draw(hQR, x0, y0);
	/* 删除二维码对象 */
	GUI_QR_Delete(hQR);
}

/**
  * @brief 2D绘图函数
  * @note 无
  * @param 无
  * @retval 无
  */
/* 用于存放多边形旋转后的点列表 */ 
GUI_POINT aArrowRotatedPoints[GUI_COUNTOF(_aPointArrow)];
static void _2D_Graph_Drawing(void)
{
	I16 aY[90] = {0};
	int i;
  float pi = 3.1415926L;
  float angle = 0.0f;
	
	/* 绘制各种矩形 */
	GUI_SetColor(GUI_GREEN);
	GUI_DrawRectEx(&BasicRect);
	BasicRect.x0 += 45;
	BasicRect.x1 += 45;
	GUI_FillRectEx(&BasicRect);
  GUI_SetColor(GUI_RED);
	GUI_DrawRoundedRect(100, 10, 140, 50, 5);
	GUI_DrawRoundedFrame(145, 10, 185, 50, 5, 5);
	GUI_FillRoundedRect(190, 10, 230, 50, 5);
	GUI_DrawGradientRoundedH(10, 55, 50, 95, 5, GUI_LIGHTMAGENTA, GUI_LIGHTCYAN);
	GUI_DrawGradientRoundedV(55, 55, 95, 95, 5, GUI_LIGHTMAGENTA, GUI_LIGHTCYAN);
	
	/* 绘制线条 */
	GUI_SetPenSize(5);
  GUI_SetColor(GUI_YELLOW);
	GUI_DrawLine(100, 55, 180, 95);
	
	/* 绘制多边形 */
	GUI_SetColor(GUI_RED);
	GUI_FillPolygon(_aPointArrow, 7, 210, 100);
  /* 旋转多边形 */
	angle = pi / 2;
	GUI_RotatePolygon(aArrowRotatedPoints,
	                  _aPointArrow, 
                    (sizeof(_aPointArrow) / sizeof(_aPointArrow[0])),
										angle);
	GUI_FillPolygon(&aArrowRotatedPoints[0], 7, 230, 120);
  
  /* 绘制线框正方体 */
  GUI_SetPenSize(1);
	GUI_SetColor(0x4a51cc);
	GUI_SetLineStyle(GUI_LS_DOT);
	GUI_DrawPolygon(DashCube_BackPoint, 4, 0, 105);
  GUI_DrawPolygon(DashCube_LeftPoint, 4, 0, 105);
  GUI_DrawPolygon(DashCube_BottonPoint, 4, 0, 105);
  GUI_SetPenSize(2);
  GUI_SetLineStyle(GUI_LS_SOLID);
  GUI_DrawPolygon(DashCube_TopPoint, 4, 0, 105);
  GUI_DrawPolygon(DashCube_RightPoint, 4, 0, 105);
  GUI_DrawPolygon(DashCube_FrontPoint, 4, 0, 105);
                    
	/* 绘制圆 */
	GUI_SetColor(GUI_LIGHTMAGENTA);
	for(i = 10; i <= 40; i += 10)
	{
		GUI_DrawCircle(140, 135, i);
	}
	GUI_SetColor(GUI_LIGHTCYAN);
	GUI_FillCircle(210, 155, 20);
	
	/* 绘制椭圆 */
	GUI_SetColor(GUI_BLUE);
	GUI_FillEllipse(30, 210, 20, 30);
	GUI_SetPenSize(2);
	GUI_SetColor(GUI_WHITE);
	GUI_DrawEllipse(30, 210, 20, 10);
	
	/* 绘制圆弧 */
	GUI_SetPenSize(4);
	GUI_SetColor(GUI_GRAY_3F);
	GUI_DrawArc(100, 215, 30, 30, -30, 210);
	
	/* 绘制折线图 */
	for(i = 0; i< GUI_COUNTOF(aY); i++)
	{
		aY[i] = rand() % 50;
	}
	GUI_SetColor(GUI_BLACK);
	GUI_DrawGraph(aY, GUI_COUNTOF(aY), 140, 180);
	
	/* 绘制饼图 */
	Pie_Chart_Drawing(60, 280, 35);
	
	/* 绘制二维码 */
	QR_Code_Drawing(QR_TEXT, 3, GUI_QR_ECLEVEL_L, 140, 240);
}

/**
  * @brief Alpha混合
  * @note 无
  * @param 无
  * @retval 无
  */
static void Alpha_Blending(void)
{
  /* 显示字符 */
	GUI_SetColor(GUI_BLACK);
	GUI_SetTextMode(GUI_TM_TRANS);
	GUI_SetFont(GUI_FONT_13B_ASCII);
	GUI_DispStringHCenterAt("Alpha blending", 55, 275);
	GUI_DispStringHCenterAt("Not Used", 185, 275);
  /* 开启自动Alpha混合 */
  GUI_EnableAlpha(1);
	/* 将Alpha数值添加到颜色中并显示 */
	GUI_SetColor((0xC0uL << 24) | 0xFF0000);
	GUI_FillRect(10, 10, 100, 90);
	GUI_SetColor((0x80uL << 24) | 0x00FF00);
	GUI_FillRect(10, 100, 100, 180);
	GUI_SetColor((0x40uL << 24) | 0x0000FF);
	GUI_FillRect(10, 190, 100, 270);
  /* 关闭自动Alpha混合 */
  GUI_EnableAlpha(0);
	GUI_SetColor((0xC0uL << 24) | 0xFF0000);
	GUI_FillRect(140, 10, 230, 90);
	GUI_SetColor((0x80uL << 24) | 0x00FF00);
	GUI_FillRect(140, 100, 230, 180);
	GUI_SetColor((0x40uL << 24) | 0x0000FF);
	GUI_FillRect(140, 190, 230, 270);
}

/**
  * @brief GUI主任务
  * @param 无
  * @retval 无
  */
void MainTask(void)
{
  /* 设置背景色 */
	GUI_SetBkColor(GUI_BLUE);
	GUI_Clear();
	
/* 2D绘图 */
	_2D_Graph_Drawing();
	
	GUI_Delay(5000);
	GUI_Clear();

	/* Alpha混合 */
	Alpha_Blending();
  while(1)
  {
    GUI_Delay(100);
  }
}

编译调试

  • 编译并下载到开发板
  • 可以看到LCD显示