初级会员
- 积分
- 115
- 金钱
- 115
- 注册时间
- 2016-6-3
- 在线时间
- 25 小时
|
前段时间有客户问起emWin有没有表盘控件?一时语塞,才发现emWin还真没有表盘控件,SEGGER怎么了?
这几天突发奇想,为什么不用Progbar进度条控件改成表盘控件来用,因为它们性质和功能完全一样,只要把背景改一改,画个指针就完事,APP函数完全通用!自己还给它起了个好听的名字:ProgbarMeter控件。步骤如下:
(1)准备个表盘背景图片
(2)把绘制Progbar控件的函数改成自定义绘制函数,这样我们想把Progbar控件画成什么样都行,emWin已经不参与这个控件的绘制工作了
[mw_shl_code=c,true]hItem = WM_GetDialogItem(pMsg->hWin, ID_PROGBAR_0);//获取句柄
PROGBAR_SetSkin(hItem, SKIN_progbarmeter3C);//将这个PROGBAR控件改成自定义绘制函数[/mw_shl_code]
(3)编写自定义绘制函数
[mw_shl_code=c,true]//ProgbarMeter控件的自定义绘制函数
static int SKIN_progbarmeter(const WIDGET_ITEM_DRAW_INFO * pDrawItemInfo, unsigned char C)
{
WM_HWIN hWin;
int x0, y0;
WINDOW_BMP *winBMP;
switch (pDrawItemInfo->Cmd)
{
case WIDGET_ITEM_CREATE:
//case WIDGET_ITEM_DRAW_BACKGROUND:
case WIDGET_ITEM_DRAW_FRAME:
//case WIDGET_ITEM_DRAW_TEXT:
break;
default: return PROGBAR_DrawSkinFlex(pDrawItemInfo);//emWin默认控件绘制函数
case WIDGET_ITEM_DRAW_BACKGROUND:
hWin = (WM_GET_PARENT[C-2])(pDrawItemInfo->hWin);//位于第几层修改这里(WM_GetParent数=层数-2)
WM_GetUserData(hWin, &winBMP, sizeof(winBMP));//从用户数据区读出"页面图片结构体"指针
//获取此控件相对于主页面(比如:背景WM_HBKWIN->WINDOW)位置偏移坐标
x0 = WM_GetWindowOrgX(hWin) - WM_GetWindowOrgX(pDrawItemInfo->hWin);
y0 = WM_GetWindowOrgY(hWin) -WM_GetWindowOrgY(pDrawItemInfo->hWin);
PROGBARMETER_SetUserClip(pDrawItemInfo);//设置1个用户剪切区
if(((PROGBAR_SKINFLEX_INFO *)pDrawItemInfo->p)->Index == PROGBAR_SKINFLEX_L)
GUI_DrawBitmap(winBMP->normal, x0, y0);//从页面坐标显示图片,emWin会自己剪切出这个控件范围的图片
else if(((PROGBAR_SKINFLEX_INFO *)pDrawItemInfo->p)->Index == PROGBAR_SKINFLEX_R)
GUI_DrawBitmap(winBMP->normal, x0, y0);
else BUTTON_DrawSkinFlex(pDrawItemInfo);
WM_SetUserClipRect(0);
break;
case WIDGET_ITEM_DRAW_TEXT:
PROGBARMETER_DispNeedle(pDrawItemInfo, 150, -150, 0, 100);
return PROGBAR_DrawSkinFlex(pDrawItemInfo);
}
return 0;
}[/mw_shl_code]
(4)编写表盘指针绘制函数
[mw_shl_code=c,true]static const GUI_POINT _aNeedle[] = {{-3, 0}, {-2, -70}, {0, -80}, {2, -70}, {3, 0}};
//ProgbarMeter控件指针绘制函数
static void PROGBARMETER_DispNeedle(const WIDGET_ITEM_DRAW_INFO * pDrawItemInfo, short angleBEG, short angleEND, short valMIN, short valMAX)
{
GUI_POINT aPoints[GUI_COUNTOF(_aNeedle)];
GUI_RECT r;
float Angle;
short Value; char i, Text[8], *p = (char *)((PROGBAR_SKINFLEX_INFO *)pDrawItemInfo->p)->pText;
for(i=0; i<7; i++){Text = *p++; Text[i+1] = 0; if(*p == '%') break;}
Value = atoi(Text); if(Value<valMIN) Value = valMIN; if(Value>valMAX) Value = valMAX;
Angle = angleBEG - (abs(angleEND-angleBEG)/(valMAX-valMIN))*Value;
//PROGBAR_GetMinMax(pDrawItemInfo->hWin, &valMIN, &valMAX);//emwin v5.30
//Angle = angleBEG - (abs(angleEND-angleBEG)/(valMAX-valMIN))*PROGBAR_GetValue(pDrawItemInfo->hWin);//emwin v5.30
Angle *= 3.1415926f / 180;
GUI_SetColor(0x35bf5f);
GUI_RotatePolygon(aPoints, _aNeedle, GUI_COUNTOF(_aNeedle), Angle);
WM_GetWindowRectEx(pDrawItemInfo->hWin, &r);
GUI_FillPolygon(aPoints, GUI_COUNTOF(aPoints), (r.x1-r.x0)/2, (r.y1-r.y0)/2);
}[/mw_shl_code]
emWin表盘控件效果演示.rar
(251.58 KB, 下载次数: 1423)
|
|