用Emwin制作一个0-9的数字键盘,并通过EDIT工具实时显示输入的数值
时间: 2024-05-08 20:22:11 浏览: 301
好的,首先我们需要使用Emwin中的窗口控件来创建数字键盘和EDIT工具来显示输入的数值。
首先创建一个窗口控件来容纳数字键盘。在窗口中绘制0-9的数字键盘。可以使用BUTTON控件或者TEXT控件来实现每个数字键。
接下来,创建一个EDIT控件来实时显示输入的数值。可以使用WM_HEDIT_Create函数创建一个水平方向的EDIT控件。
接着,为每个数字键添加按键响应函数,将数字键的值添加到EDIT控件中。可以使用WM_HEDIT_AddChar函数将字符添加到EDIT控件中。
最后,将数字键盘和EDIT控件添加到窗口中并显示窗口即可。
以下是示例代码:
```
#include "DIALOG.h"
#define ID_WINDOW_0 (GUI_ID_USER + 0x00)
#define ID_BUTTON_0 (GUI_ID_USER + 0x01)
#define ID_BUTTON_1 (GUI_ID_USER + 0x02)
#define ID_BUTTON_2 (GUI_ID_USER + 0x03)
#define ID_BUTTON_3 (GUI_ID_USER + 0x04)
#define ID_BUTTON_4 (GUI_ID_USER + 0x05)
#define ID_BUTTON_5 (GUI_ID_USER + 0x06)
#define ID_BUTTON_6 (GUI_ID_USER + 0x07)
#define ID_BUTTON_7 (GUI_ID_USER + 0x08)
#define ID_BUTTON_8 (GUI_ID_USER + 0x09)
#define ID_BUTTON_9 (GUI_ID_USER + 0x0A)
#define ID_EDIT_0 (GUI_ID_USER + 0x0B)
static WM_HWIN hEdit;
static void _cbWindow(WM_MESSAGE *pMsg) {
switch (pMsg->MsgId) {
case WM_PAINT:
GUI_SetBkColor(GUI_BLACK);
GUI_Clear();
break;
default:
WM_DefaultProc(pMsg);
break;
}
}
static void _cbButton(WM_MESSAGE *pMsg) {
switch (pMsg->MsgId) {
case WM_PAINT:
GUI_DrawRect(&pMsg->hWin->Rect);
break;
case WM_NOTIFY_PARENT:
switch (pMsg->Data.v) {
case WM_NOTIFICATION_CLICKED:
if (pMsg->hWin == WM_GetDialogItem(pMsg->hWin, ID_BUTTON_0)) {
WM_HEDIT_AddChar(hEdit, '0');
} else if (pMsg->hWin == WM_GetDialogItem(pMsg->hWin, ID_BUTTON_1)) {
WM_HEDIT_AddChar(hEdit, '1');
} else if (pMsg->hWin == WM_GetDialogItem(pMsg->hWin, ID_BUTTON_2)) {
WM_HEDIT_AddChar(hEdit, '2');
} else if (pMsg->hWin == WM_GetDialogItem(pMsg->hWin, ID_BUTTON_3)) {
WM_HEDIT_AddChar(hEdit, '3');
} else if (pMsg->hWin == WM_GetDialogItem(pMsg->hWin, ID_BUTTON_4)) {
WM_HEDIT_AddChar(hEdit, '4');
} else if (pMsg->hWin == WM_GetDialogItem(pMsg->hWin, ID_BUTTON_5)) {
WM_HEDIT_AddChar(hEdit, '5');
} else if (pMsg->hWin == WM_GetDialogItem(pMsg->hWin, ID_BUTTON_6)) {
WM_HEDIT_AddChar(hEdit, '6');
} else if (pMsg->hWin == WM_GetDialogItem(pMsg->hWin, ID_BUTTON_7)) {
WM_HEDIT_AddChar(hEdit, '7');
} else if (pMsg->hWin == WM_GetDialogItem(pMsg->hWin, ID_BUTTON_8)) {
WM_HEDIT_AddChar(hEdit, '8');
} else if (pMsg->hWin == WM_GetDialogItem(pMsg->hWin, ID_BUTTON_9)) {
WM_HEDIT_AddChar(hEdit, '9');
}
break;
default:
break;
}
break;
default:
BUTTON_Callback(pMsg);
break;
}
}
void MainTask(void) {
GUI_Init();
WM_SetCreateFlags(WM_CF_MEMDEV);
WM_SetDesktopColor(GUI_BLACK);
WM_SetCallback(WM_HBKWIN, _cbWindow);
WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbWindow, 0);
WM_CreateWindow(10, 10, 220, 280, WM_CF_SHOW, _cbWindow, 0);
hEdit = WM_HEDIT_CreateEx(10, 10, 200, 40, 0, WM_CF_SHOW, 0, ID_EDIT_0, 10);
WM_CreateWindowAsChild(10, 60, 60, 60, WM_GetDesktopWindowEx(0), WM_CF_SHOW, _cbButton, 0);
WM_CreateWindowAsChild(80, 60, 60, 60, WM_GetDesktopWindowEx(0), WM_CF_SHOW, _cbButton, 0);
WM_CreateWindowAsChild(150, 60, 60, 60, WM_GetDesktopWindowEx(0), WM_CF_SHOW, _cbButton, 0);
WM_CreateWindowAsChild(10, 130, 60, 60, WM_GetDesktopWindowEx(0), WM_CF_SHOW, _cbButton, 0);
WM_CreateWindowAsChild(80, 130, 60, 60, WM_GetDesktopWindowEx(0), WM_CF_SHOW, _cbButton, 0);
WM_CreateWindowAsChild(150, 130, 60, 60, WM_GetDesktopWindowEx(0), WM_CF_SHOW, _cbButton, 0);
WM_CreateWindowAsChild(10, 200, 60, 60, WM_GetDesktopWindowEx(0), WM_CF_SHOW, _cbButton, 0);
WM_CreateWindowAsChild(80, 200, 60, 60, WM_GetDesktopWindowEx(0), WM_CF_SHOW, _cbButton, 0);
WM_CreateWindowAsChild(150, 200, 60, 60, WM_GetDesktopWindowEx(0), WM_CF_SHOW, _cbButton, 0);
BUTTON_SetText(WM_GetDialogItem(WM_GetDesktopWindowEx(0), ID_BUTTON_0), "0");
BUTTON_SetText(WM_GetDialogItem(WM_GetDesktopWindowEx(0), ID_BUTTON_1), "1");
BUTTON_SetText(WM_GetDialogItem(WM_GetDesktopWindowEx(0), ID_BUTTON_2), "2");
BUTTON_SetText(WM_GetDialogItem(WM_GetDesktopWindowEx(0), ID_BUTTON_3), "3");
BUTTON_SetText(WM_GetDialogItem(WM_GetDesktopWindowEx(0), ID_BUTTON_4), "4");
BUTTON_SetText(WM_GetDialogItem(WM_GetDesktopWindowEx(0), ID_BUTTON_5), "5");
BUTTON_SetText(WM_GetDialogItem(WM_GetDesktopWindowEx(0), ID_BUTTON_6), "6");
BUTTON_SetText(WM_GetDialogItem(WM_GetDesktopWindowEx(0), ID_BUTTON_7), "7");
BUTTON_SetText(WM_GetDialogItem(WM_GetDesktopWindowEx(0), ID_BUTTON_8), "8");
BUTTON_SetText(WM_GetDialogItem(WM_GetDesktopWindowEx(0), ID_BUTTON_9), "9");
while (1) {
GUI_Delay(100);
}
}
```
这个示例程序中,我们使用了BUTTON控件来实现数字键盘,使用了WM_HEDIT控件来实时显示输入的数值。在_cbButton回调函数中,我们为每个数字键添加了按键响应函数,将数字键的值添加到EDIT控件中。
阅读全文