Emwin点击Edit弹出数字键盘,输入到Edit并按下确认更新到Text显示
1. 第一步,先打开emwin的GUIBuilder,分别创建两个页面,第一个是数字键盘页面。
第二个页面包含一个Window窗口、4个Text和两个Edit 一个Button
2. 生成代码后添加到PC的工程上去
我的自己创建了一个Text文件夹,里面有这两个文件
这两个文件名字如下
我在工程里面创建了对应的组名来区分
添加进去
3. 添加代码
这俩个名字是一样的
void MainTask(void)
{
GUI_Init(); //GUI初始化
GUI_UC_SetEncodeUTF8();
Createmoni();//自定义的名称,可以修改
while (1)
{
GUI_Delay(200);
}
}
然后运行模拟,第一个页面就做好了
4. 合并两个页面
一共有四个东西 1. 定义控件 2 创建控件函数 3.回调函数 4.包涵这个三个东西的函数,在最末尾,我也不知道叫啥函数
1.定义控件
/第一个界面控件定义
#define ID_WINDOW_0 (GUI_ID_USER + 0x20)
#define ID_BUTTON_0 (GUI_ID_USER + 0x21)
#define ID_EDIT_0 (GUI_ID_USER + 0x22)
#define ID_EDIT_1 (GUI_ID_USER + 0x23)
#define ID_TEXT_0 (GUI_ID_USER + 0x24)
#define ID_TEXT_1 (GUI_ID_USER + 0x25)
#define ID_TEXT_2 (GUI_ID_USER + 0x26)
#define ID_TEXT_3 (GUI_ID_USER + 0x27)
/数字键盘控件定义
#define ID_FRAMEWIN_50 (GUI_ID_USER + 0x01)
#define ID_EDIT_50 (GUI_ID_USER + 0x02)
#define ID_BUTTON_50 (GUI_ID_USER + 0x03)
#define ID_BUTTON_51 (GUI_ID_USER + 0x04)
#define ID_BUTTON_52 (GUI_ID_USER + 0x05)
#define ID_BUTTON_53 (GUI_ID_USER + 0x06)
#define ID_BUTTON_54 (GUI_ID_USER + 0x07)
#define ID_BUTTON_55 (GUI_ID_USER + 0x08)
#define ID_BUTTON_56 (GUI_ID_USER + 0x09)
#define ID_BUTTON_57 (GUI_ID_USER + 0x0A)
#define ID_BUTTON_58 (GUI_ID_USER + 0x0B)
#define ID_BUTTON_59 (GUI_ID_USER + 0x0C)
#define ID_BUTTON_5A (GUI_ID_USER + 0x0D)
#define ID_BUTTON_5B (GUI_ID_USER + 0x0E)
#define ID_BUTTON_5C (GUI_ID_USER + 0x0F)
2.创建控件函数
static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
{ WINDOW_CreateIndirect, "moni", ID_WINDOW_0, 0, 0, 800, 480, 0, 0x0, 0 },
{ BUTTON_CreateIndirect, "确认", ID_BUTTON_0, 38, 359, 129, 85, 0, 0x0, 0 },
{ EDIT_CreateIndirect, "Edit1", ID_EDIT_0, 315, 62, 115, 50, 0, 0x64, 0 },
{ EDIT_CreateIndirect, "Edit2", ID_EDIT_1, 315, 141, 120, 50, 0, 0x64, 0 },
{ TEXT_CreateIndirect, "窗口1", ID_TEXT_0, 140, 60, 110, 40, 0, 0x64, 0 },
{ TEXT_CreateIndirect, "窗口2", ID_TEXT_1, 140, 140, 80, 40, 0, 0x64, 0 },
{ TEXT_CreateIndirect, "已灌装", ID_TEXT_2, 4, 60, 100, 40, 0, 0x64, 0 },
{ TEXT_CreateIndirect, "总瓶数", ID_TEXT_3, 5, 140, 100, 40, 0, 0x64, 0 },
// USER START (Optionally insert additional widgets)
// USER END
};
static const GUI_WIDGET_CREATE_INFO _aDialogCreate5[] = {
{ FRAMEWIN_CreateIndirect, "number", ID_FRAMEWIN_50, 400, 50, 350, 405, 0, 0x64, 0 },
{ EDIT_CreateIndirect, "Edit", ID_EDIT_50, 5, 5, 141, 40, 0, 0x64, 0 },
{ BUTTON_CreateIndirect, "0", ID_BUTTON_50, 134, 298, 80, 65, 0, 0x0, 0 },
{ BUTTON_CreateIndirect, "1", ID_BUTTON_51, 20, 60, 80, 65, 0, 0x0, 0 },
{ BUTTON_CreateIndirect, "2", ID_BUTTON_52, 130, 60, 80, 65, 0, 0x0, 0 },
{ BUTTON_CreateIndirect, "3", ID_BUTTON_53, 240, 60, 80, 65, 0, 0x0, 0 },
{ BUTTON_CreateIndirect, "4", ID_BUTTON_54, 20, 140, 80, 65, 0, 0x0, 0 },
{ BUTTON_CreateIndirect, "5", ID_BUTTON_55, 130, 140, 80, 65, 0, 0x0, 0 },
{ BUTTON_CreateIndirect, "6", ID_BUTTON_56, 240, 140, 78, 65, 0, 0x0, 0 },
{ BUTTON_CreateIndirect, "7", ID_BUTTON_57, 20, 220, 80, 65, 0, 0x0, 0 },
{ BUTTON_CreateIndirect, "8", ID_BUTTON_58, 130, 220, 80, 65, 0, 0x0, 0 },
{ BUTTON_CreateIndirect, "9", ID_BUTTON_59, 240, 220, 80, 65, 0, 0x0, 0 },
{ BUTTON_CreateIndirect, "ESC", ID_BUTTON_5A, 20, 300, 80, 65, 0, 0x0, 0 },
{ BUTTON_CreateIndirect, "Delete", ID_BUTTON_5B, 240, 300, 80, 65, 0, 0x0, 0 },
{ BUTTON_CreateIndirect, "OK", ID_BUTTON_5C, 160, 5, 160, 43, 0, 0x0, 0 },
// USER START (Optionally insert additional widgets)
// USER END
};
3…回调函数 太多了就不贴了,截图也没截完整4.
到这里就把两个界面的东西都丢到一起了,当然不丢到一起也是可以的
5. 点击对应控件有对应动作
接下来想让他点击Edit后弹出数字键盘
1.先定义一些东西
WM_HWIN layer[2];//两个界面
WM_HWIN hItem_slave;//点击总瓶数Edit和灌装量Edit的句柄
WM_HWIN EDIT_botttle;//总瓶数Edit控件输入框句柄
WM_HWIN EDIT_capacity;//灌装量Edit控件输入框句柄
WM_HWIN hItem_bottle;//总瓶数Text控件句柄
WM_HWIN hItem_capacity;//灌装量Text控件句柄
char Edit_buff[10];//数字键盘储存字符串
char bottle_buff[10];//储存瓶子总数的字符串
char capacity_buff[10];//储存设定流量的字符串
extern void Write_edit(char * buff);//写入瓶数Edit和流量Edit
extern void Write_Bottletext(char * buff);//更新显示界面的设定瓶子数量
extern void Write_Capacitytext(char * buff);//更新显示界面的设定灌装流量
extern void change(char* to);
static void _cbDialog5(WM_MESSAGE * pMsg);//数字键盘
static void _cbDialog(WM_MESSAGE * pMsg);//另一个页面
#define Edit_SetText(Edit_buff) EDIT_SetText(WM_GetDialogItem(pMsg->hWin, ID_EDIT_50), Edit_buff) ;//这个Edit是数字键盘上的Edit关键作用,后面把数值写入两个Edit就靠它了
/在Main Task做这些处理
void MainTask(void)
{
GUI_Init(); //GUI初始化
GUI_UC_SetEncodeUTF8();
//Createmoni();//自定义的名称,可以修改
layer[1] = Createnumber();
layer[0] = Createmoni();
WM_HideWindow(layer[1]);//隐藏数字键盘
while (1)
{
GUI_Delay(200);
}
}
然后去 WM_NOTIFY_PARENT:这里对EDit处理,当Edit被点击时,弹出数字键盘,同时弹出数字键盘后,点击数字键盘上的ESC和OK可以隐藏(结束)数字键盘
2.点击Edti弹出数字键盘处理
case ID_EDIT_0: // Notifications sent by 'Edit1'
switch(NCode) {
case WM_NOTIFICATION_CLICKED:
hItem_slave = WM_GetDialogItem(pMsg->hWin, ID_EDIT_0);//先点击的EDit1 返回它的句柄
GUI_CreateDialogBox(_aDialogCreate5, GUI_COUNTOF(_aDialogCreate5), _cbDialog5, WM_HBKWIN, 0, 0);弹出数字键盘
// USER START (Optionally insert code for reacting on notification message)
// USER END
break;
case WM_NOTIFICATION_RELEASED:
// USER START (Optionally insert code for reacting on notification message)
// USER END
break;
case WM_NOTIFICATION_VALUE_CHANGED:
// USER START (Optionally insert code for reacting on notification message)
// USER END
break;
// USER START (Optionally insert additional code for further notification handling)
// USER END
}
break;
case ID_EDIT_1: // Notifications sent by 'Edit2'
switch(NCode) {
case WM_NOTIFICATION_CLICKED:
hItem_slave = WM_GetDialogItem(pMsg->hWin, ID_EDIT_1);//先点击的EDit2 返回它的句柄
GUI_CreateDialogBox(_aDialogCreate5, GUI_COUNTOF(_aDialogCreate5), _cbDialog5, WM_HBKWIN, 0, 0);弹出数字键盘
// USER START (Optionally insert code for reacting on notification message)
// USER END
break;
case WM_NOTIFICATION_RELEASED:
// USER START (Optionally insert code for reacting on notification message)
// USER END
break;
case WM_NOTIFICATION_VALUE_CHANGED:
// USER START (Optionally insert code for reacting on notification message)
// USER END
break;
// USER START (Optionally insert additional code for further notification handling)
// USER END
}
break;
// USER START (Optionally insert additional code for further Ids)
// USER END
}
break;
3.点击数字键盘上的ESC和OK可以隐藏(结束)数字键盘
,然后运行,就可以弹出和关闭数字键盘了
4.按下数字键盘上的0-9 数字键盘上的Edti框会显示出来
处理如下
先把按键禁止聚焦
WM_HWIN hItem;
WM_HWIN Numbpad;//数字键盘的句柄
int NCode;
int Id;
int i;
unsigned short int buff_length = 0;
static U8 back = 0;
static U16 setting_ml = 300;//默认灌装容量,单位ml
Numbpad = pMsg->hWin;
switch (pMsg->MsgId) {
case WM_INIT_DIALOG:
for (i = 0xE1; i <= 0xEF; i++) {
hItem = WM_GetDialogItem(Numbpad, GUI_ID_USER + i);
BUTTON_SetFocussable(hItem, 0);/* 设置所有按钮不对焦 */
// BUTTON_SetFont(hItem, GUI_FONT_24_1);
}WM_GetDialogItem(Numbpad, GUI_ID_USER + 0XEB);
然后对0-9按键点击做一下处理
case ID_BUTTON_50: // Notifications sent by '0'
switch (NCode) {
case WM_NOTIFICATION_CLICKED://控件被点击
if (strlen(Edit_buff) < 4)//就是最多只能输入4个0
strcat(Edit_buff, "0");//按下0的时候把“0”这个字符串放到Edit_buff字符串数组里面
Edit_SetText(Edit_buff);//同时更新键盘上Edit的值
// USER END
break;
case WM_NOTIFICATION_RELEASED://控件被释放
// USER END
break;
// USER START (Optionally insert additional code for further notification handling)
// USER END
}
break;
case ID_BUTTON_51: // Notifications sent by '1'
switch (NCode) {
case WM_NOTIFICATION_CLICKED:
if (strlen(Edit_buff) < 4)
strcat(Edit_buff, "1");
Edit_SetText(Edit_buff);
// USER END
break;
case WM_NOTIFICATION_RELEASED:
// USER END
break;
// USER START (Optionally insert additional code for further notification handling)
// USER END
}
break;
我的控件ID_BUTTON_50 到ID_BUTTON_59分别就是0-9
0-9的处理基本一样,Delete、 ESC、 OK稍有不同
ESC是结束数字键盘
case ID_BUTTON_5A: // Notifications sent by 'ESC'
switch (NCode) {
case WM_NOTIFICATION_CLICKED:
GUI_EndDialog(pMsg->hWin, 0);//结束当前窗口
// USER END
break;
case WM_NOTIFICATION_RELEASED:
// USER END
break;
// USER START (Optionally insert additional code for further notification handling)
// USER END
}
break;
Delete是减掉一个字符
case ID_BUTTON_5B: // Notifications sent by 'Delete'
switch (NCode) {
case WM_NOTIFICATION_CLICKED:
if (strlen(Edit_buff)> 0)
{
buff_length = strlen(Edit_buff);
Edit_buff[buff_length - 1] = '\0';
Edit_SetText(Edit_buff);
}
// USER END
break;
case WM_NOTIFICATION_RELEASED:
// USER END
break;
// USER START (Optionally insert additional code for further notification handling)
// USER END
}
break;
接下来是OK的处理
case ID_BUTTON_5C: // Notifications sent by 'OK'
switch (NCode) {
case WM_NOTIFICATION_CLICKED:
Write_edit(Edit_buff); //把数字键盘上的数值写入到Edit上
memset(Edit_buff, '\0', sizeof Edit_buff);//用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘\0’
EDIT_SetText(WM_GetDialogItem(pMsg->hWin, ID_EDIT_50), "");//把数字键盘的Edit设为空白
//WM_HideWindow(layer[5]);
GUI_EndDialog(pMsg->hWin, 0);//结束数字小键盘
// USER END
break;
case WM_NOTIFICATION_RELEASED:
// USER END
break;
// USER START (Optionally insert additional code for further notification handling)
// USER END
}
break;
// USER START (Optionally insert additional code for further Ids)
// USER END
}
break;
ESC
case ID_BUTTON_5A: // Notifications sent by 'ESC'
switch (NCode) {
case WM_NOTIFICATION_CLICKED:
GUI_EndDialog(pMsg->hWin, 0);//结束当前窗口
// USER END
break;
case WM_NOTIFICATION_RELEASED:
// USER END
break;
// USER START (Optionally insert additional code for further notification handling)
// USER END
}
break;
然后在OK里面有一个Write_edit(Edit_buff);
这个函数作用就是把在数字键盘的Edit里面的字符先赋值给一个数组,然后再同时更新到Edit
代码在这
void Write_edit(char * edit_buff)
{
if (Edit_buff[0] != '\0')
edit_buff = Edit_buff;
if (EDIT_botttle == hItem_slave)
{
strcpy(bottle_buff, Edit_buff);//如果点击了总瓶数Edit框,则把Eidt的值赋值给bottle_buff数组
}
else if (EDIT_capacity == hItem_slave)
{
strcpy(capacity_buff, Edit_buff);如果点击了灌装量Edit框,则把Eidt的值赋值给capacity_buff数组
}
EDIT_SetText(hItem_slave, edit_buff);//把数字键盘的值更新到当前点击的Edit框
}
里面有个hItem_slave,在你点击哪一个Edit的时候,那就获取该Edit的句柄,这里就判断你先按下的哪一个句柄,这个在后面的更新显示到Text很重要
到这里的话,基本就可以实现点击Edit1 弹出数字键盘,按下数字,点击ok,数字更新到Edit1
6.接下来就是按下确认键后,更新到TEXE
case ID_BUTTON_0: // Notifications sent by '确认'
switch(NCode) {
case WM_NOTIFICATION_CLICKED:
Write_Bottletext(bottle_buff);
Write_Capacitytext(capacity_buff);
// USER START (Optionally insert code for reacting on notification message)
// USER END
break;
case WM_NOTIFICATION_RELEASED:
// USER START (Optionally insert code for reacting on notification message)
// USER END
break;
// USER START (Optionally insert additional code for further notification handling)
// USER END
}
break;
这两个Write_Bottletext(bottle_buff);
Write_Capacitytext(capacity_buff);
我已经在按下确认键的时候Edit1和Edit2 的值就已经在bottle_buff、capacity_buff中已经储存好了
函数原型
void Write_Bottletext(char * Bottle_buff)//把bottle_buff数组的里字符串更新到Text上显示出来,显示设定总瓶数
{
if (bottle_buff[0] != '\0')
Bottle_buff = bottle_buff;
//programmax = atoi(bottle_buff);//把bottle_buff数组的里字符串转换成int数值并在
//bottle_value = programmax;
TEXT_SetText(hItem_bottle, Bottle_buff);
}
void Write_Capacitytext(char * Capacity_buff)//把capacity_buff数组里的字符串更新到Text上显示出来,显示灌装容量
{
if (capacity_buff[0] != '\0')
Capacity_buff = capacity_buff;
//capacity1 = atoi(Capacity_buff);//把capacity_buff数组里的字符串转换成int数值并曲线显示
TEXT_SetText(hItem_capacity, Capacity_buff);
}
效果显示
传送门 https://pan.baidu.com/s/1etC5P-f1ti0pe-bSG9J64Q?pwd=2024
提取码:2024
–来自百度网盘超级会员V5的分享
https://github.com/Cheng0717/Emwinnumbpad