今天的程序主要是UCGUI的视窗管理,这点和QT的视窗管理比较类似,由于这阶段对视窗管理任务的要祈求不是很高,下面程序只是实现基本的视窗管理
#include "MainTask.h"
#define MSG_CHANGE_TEXT WM_USER+0 //用户自定义消息号,从用户可以使用的消息号0开始
#define SPEED 1250 //延时时间
static char _acInfoText[40];
static GUI_COLOR _WindowColor1 = GUI_GREEN;
static GUI_COLOR _FrameColor1 = GUI_BLUE;
static WM_CALLBACK* _cbBkWindowOld;
static WM_HWIN _hWindow1;
static void _ChangeInfoText(char* pStr) {
WM_MESSAGE Message;
Message.MsgId = MSG_CHANGE_TEXT;
Message.Data.p = pStr;
WM_SendMessage(WM_HBKWIN, &Message); //向一个指定的窗口发送消息
WM_InvalidateWindow(WM_HBKWIN); //使一个指定的窗口无效,调用该函数告诉WM该窗口不更新
}
// 固定的屏幕显示
static void _DrawInfoText(char* pStr) {
GUI_SetColor(GUI_WHITE);
GUI_SetFont(&GUI_Font24_ASCII);
GUI_DispStringHCenterAt("WindowManager - Sample", 160, 5);
GUI_SetFont(&GUI_Font8x16);
GUI_DispStringAtCEOL(pStr, 5, 40);
}
// _LiftUp
static void _LiftUp(int dy) {
int i, tm;
for (i = 0; i < (dy/4); i++) {
tm = GUI_GetTime();
WM_MoveWindow(_hWindow1, 0, -4); //窗口指定移动, x移动0,y移动-4
while ((GUI_GetTime() - tm) < 20) {
WM_Exec(); //通过回调函数,重绘无效窗口
}
}
}
// _LiftDown
static void _LiftDown(int dy) {
int i, tm;
for (i = 0; i < (dy/4); i++) {
tm = GUI_GetTime();
WM_MoveWindow(_hWindow1, 0, 4);
while ((GUI_GetTime() - tm) < 20) {
WM_Exec();
}
}
}
static void _cbBkWindow(WM_MESSAGE* pMsg) {
switch (pMsg->MsgId) {
case MSG_CHANGE_TEXT:
strcpy(_acInfoText, pMsg->Data.p);
case WM_PAINT:
GUI_SetBkColor(GUI_BLACK);
GUI_Clear();
GUI_SetColor(GUI_WHITE);
GUI_SetFont(&GUI_Font24_ASCII);
GUI_DispStringHCenterAt("WindowManager - Sample", 160, 5);
GUI_SetFont(&GUI_Font8x16);
GUI_DispStringAt(_acInfoText, 5, 40);
break;
default:
WM_DefaultProc(pMsg);
}
}
// _cbWindow1
static void _cbWindow1(WM_MESSAGE* pMsg) {
GUI_RECT Rect;
int x, y;
switch (pMsg->MsgId) {
case WM_PAINT:
WM_GetInsideRect(&Rect);
GUI_SetBkColor(_WindowColor1);
GUI_SetColor(_FrameColor1);
GUI_ClearRectEx(&Rect);
GUI_DrawRectEx(&Rect);
GUI_SetColor(GUI_WHITE);
GUI_SetFont(&GUI_Font24_ASCII);
x = WM_GetWindowSizeX(pMsg->hWin);
y = WM_GetWindowSizeY(pMsg->hWin);
GUI_DispStringHCenterAt("Window 1", x / 2, (y / 2)-12);
break;
default:
WM_DefaultProc(pMsg);
}
}
static void _SetDesktopColor(void) {
GUI_SetBkColor(GUI_BLUE);
GUI_Clear();
_DrawInfoText("WM_SetDesktopColor()");
GUI_Delay(SPEED*3/2);
WM_SetDesktopColor(GUI_BLACK);
GUI_Delay(SPEED/2);
/* Set background color and invalidate desktop color.
This is needed for the later redrawing demo. */
GUI_SetBkColor(GUI_BLACK);
WM_SetDesktopColor(GUI_INVALID_COLOR);
}
static void _CreateWindow(void) {
/* Set callback for background window */
_cbBkWindowOld = WM_SetCallback(WM_HBKWIN, _cbBkWindow);
/* Create windows */
_ChangeInfoText("WM_CreateWindow()");
GUI_Delay(SPEED);
_hWindow1 = WM_CreateWindow( 50, 70, 165, 100, WM_CF_SHOW | WM_CF_MEMDEV, _cbWindow1, 0); //窗口创建
GUI_Delay(SPEED);
}
static void _InvalidateWindow(void) {
_ChangeInfoText("WM_InvalidateWindow()");
_WindowColor1 = GUI_BLUE;
_FrameColor1 = GUI_GREEN;
GUI_Delay(SPEED);
WM_InvalidateWindow(_hWindow1);
GUI_Delay(SPEED);
}
static void _BringToTop(void) {
_ChangeInfoText("WM_BringToTop()");
GUI_Delay(SPEED);
WM_BringToTop(_hWindow1);
GUI_Delay(SPEED);
}
static void _MoveTo(void) {
int i, tm, tDiff;
_ChangeInfoText("WM_MoveTo()");
GUI_Delay(SPEED);
for (i = 1; i < 56; i++) {
tm = GUI_GetTime();
WM_MoveTo(_hWindow1, 50 + i, 70 + i);
tDiff = 15 - (GUI_GetTime() - tm);
GUI_Delay(tDiff);
}
for (i = 1; i < 56; i++) {
tm = GUI_GetTime();
WM_MoveTo(_hWindow1, 105 - i, 125 - i);
tDiff = 15 - (GUI_GetTime() - tm);
GUI_Delay(tDiff);
}
GUI_Delay(SPEED);
}
static void _BringToBottom(void) {
_ChangeInfoText("WM_BringToBottom()");
GUI_Delay(SPEED);
WM_BringToBottom(_hWindow1);
GUI_Delay(SPEED);
}
static void _MoveWindow(void) {
int i, tm, tDiff;
_ChangeInfoText("WM_MoveWindow()");
GUI_Delay(SPEED);
for (i = 0; i < 55; i++) {
tm = GUI_GetTime();
WM_MoveWindow(_hWindow1, 1, 1);
tDiff = 15 - (GUI_GetTime() - tm);
GUI_Delay(tDiff);
}
for (i = 0; i < 55; i++) {
tm = GUI_GetTime();
WM_MoveWindow(_hWindow1, -1, -1);
tDiff = 15 - (GUI_GetTime() - tm);
GUI_Delay(tDiff);
}
GUI_Delay(SPEED);
}
static void _DeleteWindow(void) {
_ChangeInfoText("WM_DeleteWindow()");
GUI_Delay(SPEED);
WM_DeleteWindow(_hWindow1);
GUI_Delay(SPEED);
_ChangeInfoText("");
GUI_Delay(SPEED);
/* Restore background callback and window colors */
WM_SetCallback(WM_HBKWIN, _cbBkWindowOld);
_WindowColor1 = GUI_GREEN;
}
void gui_wm(void)
{
GUI_SetBkColor(GUI_BLACK);
WM_SetCreateFlags(WM_CF_MEMDEV);
WM_EnableMemdev(WM_HBKWIN);
while (1) {
_SetDesktopColor();
_CreateWindow();
_InvalidateWindow();
_BringToTop(); //置顶 用于有重叠的窗口
_MoveTo();
_BringToBottom(); //置底 用于有重叠的窗口
_MoveWindow();
_LiftUp(20);
_LiftDown(20);
_DeleteWindow();
}
}
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。