emWin: 键盘EDIT自动获取点击EDIT框里的值并修改
1、数字软键盘修改数据,先上照片:
2、实现如下:
typedef struct tag_mdfKey
{
u8 flag_ent; //确认标志
u32 id_press; //修改控件:ID
char mdf_buff[30]; //修改数据缓冲区
}KPad_MDF;
static WM_MESSAGE kp_Msg;
KPad_MDF kpad_mdf; //数字键盘修改结构体变量
//数字小键盘:回调函数:
/*
********************************************************************
*
* _cbDialogNumPad
*********************************************************************
*/
static void _cbDialogNumPad(WM_MESSAGE * pMsg) {
GUI_RECT r;
int i, NCode, Id, Pressed = 0;
WM_HWIN hDlg, hItem;
hDlg = pMsg->hWin;
switch (pMsg->MsgId) {
case KEYPAD_UPDATE_MSG: //数字小键盘: 更新数据消息
if(kpad_mdf.id_press>0){
hItem = WM_GetDialogItem(hDlg, GUI_ID_EDIT1);
EDIT_SetText(hItem,kpad_mdf.mdf_buff);
}else{
hItem = WM_GetDialogItem(hDlg, GUI_ID_EDIT1);
EDIT_SetText(hItem,"");
}
WM_SetFocus(hItem); //聚焦于小键盘上的EDIT编辑框
break;
case WM_PAINT:
WM_GetClientRect(&r);
GUI_SetColor(0x000000);
GUI_DrawRect(r.x0, r.y0, r.x1, r.y1); /* Draw rectangle around it */
/* Draw the bright sides */
GUI_SetColor(0xffffff);
GUI_DrawHLine(r.y0 + 1, r.x0 + 1, r.x1 - 2); /* Draw top line */
GUI_DrawVLine(r.x0 + 1, r.y0 + 1, r.y1 - 2);
/* Draw the dark sides */
GUI_SetColor(0x555555);
GUI_DrawHLine(r.y1-1, r.x0 + 1, r.x1 - 1);
GUI_DrawVLine(r.x1-1, r.y0 + 1, r.y1 - 2);
break;
case WM_INIT_DIALOG:
for (i = 0; i < GUI_COUNTOF(_aDialogNumPad) - 1; i++) {
hItem = WM_GetDialogItem(hDlg, GUI_ID_USER + i);
BUTTON_SetFont(hItem, &GUI_FontHZ_Saf_16);
BUTTON_SetFocussable(hItem, 0); /* Set all buttons non focussable */
switch (i) {
case 18:
BUTTON_SetBitmapEx(hItem, 0, &_bmArrowLeft, 10, 12); /* Set bitmap for arrow left button (unpressed) */
BUTTON_SetBitmapEx(hItem, 1, &_bmArrowLeft, 10, 12); /* Set bitmap for arrow left button (pressed) */
break;
case 19:
BUTTON_SetBitmapEx(hItem, 0, &_bmArrowRight, 10, 12); /* Set bitmap for arrow right button (unpressed) */
BUTTON_SetBitmapEx(hItem, 1, &_bmArrowRight, 10, 12); /* Set bitmap for arrow right button (pressed) */
break;
}
}
hItem = WM_GetDialogItem(hDlg, GUI_ID_EDIT1); //数字小键盘上的:EDIT控件
EDIT_SetMaxLen(hItem,30);
EDIT_SetBkColor(hItem,0,GUI_BLACK);
EDIT_SetFont(hItem,&GUI_Font24B_1);
EDIT_SetTextAlign(hItem,GUI_TA_VCENTER|GUI_TA_LEFT);
EDIT_SetTextColor(hItem,0,GUI_RED);
break;
case WM_NOTIFY_PARENT:
Id = WM_GetId(pMsg->hWinSrc); /* Id of widget */
NCode = pMsg->Data.v; /* Notification code */
switch (NCode) {
case WM_NOTIFICATION_CLICKED:
if(Id ==(GUI_ID_USER + 17)){ //"ENT"按键----按下
hItem =WM_GetDialogItem(hDlg, GUI_ID_USER + 17);
BUTTON_SetBkColor(hItem,BUTTON_CI_PRESSED,GUI_GREEN); //按下时--绿色背景
}
Pressed = 1;
break;
case WM_NOTIFICATION_RELEASED:
if ((Id >= GUI_ID_USER) && (Id <= (GUI_ID_USER + GUI_COUNTOF(_aDialogNumPad) - 1))) {
int Key;
if (Id < GUI_ID_USER + 14) {
char acBuffer[10];
BUTTON_GetText(pMsg->hWinSrc, acBuffer, sizeof(acBuffer)); /* Get the text of the button */
Key = acBuffer[0];
} else {
Key = _aKey[Id - GUI_ID_USER - 13]; /* Get the text from the array */
if(Id ==(GUI_ID_USER + 17)){ //"ENT"按键---抬起
hItem =WM_GetDialogItem(hDlg, GUI_ID_USER + 17);
BUTTON_SetBkColor(hItem,BUTTON_CI_UNPRESSED,0xAAAAAA);
hItem = WM_GetDialogItem(hDlg, GUI_ID_EDIT1);
EDIT_GetText(hItem,kpad_mdf.mdf_buff,30); //获取编辑后的内容
kpad_mdf.flag_ent=1;//set
}
}
Pressed = 1;
GUI_SendKeyMsg(Key, Pressed); /* Send a key message to the focussed window */
}
break;
}
default:
WM_DefaultProc(pMsg);
}
}
//主窗体下的控件处理:
/****************************************************************************
* 功 能: 数字小键盘---更新数据处理函数
*---------------------------------------------------------------------------*
* 入口参数: hDlg------窗口句柄
* Id--------控件ID
* 出口参数: nil
****************************************************************************/
void KeyPad_Update_Process(WM_HWIN hDlg,int Id)
{
WM_HWIN hItem;
memset(kpad_mdf.mdf_buff,0,30);//clear
kpad_mdf.flag_ent=0; //clear
kpad_mdf.id_press=Id;
hItem = WM_GetDialogItem(hDlg, Id);
EDIT_GetText(hItem,kpad_mdf.mdf_buff,30); //获取点击的EDIT编辑框的内容
kp_Msg.MsgId = KEYPAD_UPDATE_MSG;
WM_SendMessage(WM_GetClientWindow(hNumPad), &kp_Msg); //给小键盘发送消息
}
/****************************************************************************
* 功 能: 数字小键盘---更新完成函数
*----------------------------------------------------------------------------------------------------------------*
* 入口参数: hDlg------窗口句柄
* Id--------控件ID
* 出口参数: nil
****************************************************************************/
void KeyPad_Update_Finish(WM_HWIN hDlg)
{
u8 i;
WM_HWIN hItem;
#if OS_CRITICAL_METHOD == 3
OS_CPU_SR cpu_sr = 0;
#endif
if(kpad_mdf.flag_ent==1){ //数字小键盘:确认修改
if((kpad_mdf.id_press== GUI_ID_EDIT0)||
(kpad_mdf.id_press== GUI_ID_EDIT1)||
(kpad_mdf.id_press== GUI_ID_EDIT2)){
hItem = WM_GetDialogItem(hDlg, kpad_mdf.id_press); //要点击修改的EDIT编辑框
EDIT_SetText(hItem,kpad_mdf.mdf_buff); //更新为:小键盘EDIT的内容
i=kpad_mdf.id_press-GUI_ID_EDIT0;
//添加自己的代码, 存储数据
Buzzer_out(); //蜂鸣器鸣响--->提示
}
memset(&kpad_mdf,0,sizeof(KPad_MDF)); //clear
WM_HideWindow(hwin_keypad); //
WM_HideWindow(hNumPad); //隐藏虚拟小键盘
}
}
//主窗体: 回调函数
static void _cbCallback(WM_MESSAGE * pMsg)
{
WM_HWIN hItem;
WM_HWIN hDlg = pMsg->hWin;
switch (pMsg->MsgId) {
case WM_TOUCH:
//....................
break;
case WM_PID_STATE_CHANGED: //the pressed state has been changed.
//....................
break;
case UPDATE_MSG_TEXT:
//....................
KeyPad_Update_Finish(hDlg); //主窗体---更新小键盘的数据
break;
case WM_NOTIFY_PARENT:
Id = WM_GetId(pMsg->hWinSrc);
NCode = pMsg->Data.v;
switch(NCode)
{
case WM_NOTIFICATION_CLICKED: //编辑框内点击------数字小键盘显示
switch(Id)
{
if((Id == GUI_ID_EDIT0)||
(Id == GUI_ID_EDIT1)||
(Id == GUI_ID_EDIT2))
{
int x_pos,y_pos;
hItem = WM_GetDialogItem(hDlg, GUI_ID_EDIT2);
x_pos=WM_GetWindowOrgX(hItem);
y_pos=WM_GetWindowOrgY(hItem);
KeyPad_Update_Process(hDlg,Id); //数字小键盘更新数据处理
WM_MoveTo(hwin_keypad,x_pos-15,y_pos-20); //移动窗口
WM_ShowWindow(hwin_keypad); //
WM_ShowWindow(hNumPad); //显示虚拟键盘
}
}
break;
}
}
}
3、最后的总结如下:
|
|