EMWIN密码验证窗口创建

EMWIN密码验证窗口创建

    • 调用接口

/**
 *  @file PasswdCheck.c
 *
 *  @date 2020-7-6
 *
 *  @author aron566
 *
 *  @copyright None
 *
 *  @brief 通用密码验证页面
 *
 *  @details None
 *
 *  @version V1.0
 */
#ifdef __cplusplus ///
extern "C" {
#endif
/** Includes -----------------------------------------------------------------*/
/* Private includes ----------------------------------------------------------*/
#include "PasswdCheck.h"
#include "DIALOG.h"
/** Private typedef ----------------------------------------------------------*/

/** Private macros -----------------------------------------------------------*/
#define ID_FRAMEWIN_0                           (GUI_ID_USER + 0x00)
#define ID_TEXT_0                               (ID_FRAMEWIN_0 + 0x01)
#define ID_TEXT_1                               (ID_FRAMEWIN_0 + 0x02)
#define ID_EDIT_0                               (ID_FRAMEWIN_0 + 0x03)

#define PASSWD_MAX_CHAR_NUM											4
/** Private constants --------------------------------------------------------*/
/** Public variables ---------------------------------------------------------*/
/** Private variables --------------------------------------------------------*/
static char Password_Input[PASSWD_MAX_CHAR_NUM+1] = "0000";
static char *Password_Raw = NULL;
static uint8_t PasswdCheckTaskResult = 0;						/**< 窗口运行结果*/
static WM_HWIN MAINWin = 0;
/** Private function prototypes ----------------------------------------------*/
static void PasswdCheckWindowHide(void);						/**< 窗口隐藏*/

static WM_HWIN CreateitemsPasswdCheckWindow(void);	        /**< 窗口建立*/
static void PasswdCheckPage_Init(char *str);				/**< 窗口页面信息初始化*/
static void cbDialog(WM_MESSAGE *pMsg);						/**< 页面控件建立*/
static void CreateTittle(WM_MESSAGE *pMsg);					/**< 建立窗口标题*/
static void CreateEditBox(WM_MESSAGE *pMsg);				/**< 建立窗口编辑框*/
static void CreateTipsText(WM_MESSAGE *pMsg);				/**< 建立窗口提示文本*/
static void PasswdFocusUpdate(int postion);					/**< 更新编辑框聚焦*/
static void PasswdCheckPage_Back(void);						/**< 左移处理*/
static void PasswdCheckPage_Forward(void);					/**< 右移处理*/
static void PasswdCheckPage_Up(void);						/**< 上键处理*/
static void PasswdCheckPage_Down(void);						/**< 下键处理*/
/** Private user code --------------------------------------------------------*/

/** Private application code -------------------------------------------------*/
/*******************************************************************************
*
*       Static code
*
********************************************************************************
*/
static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
  { FRAMEWIN_CreateIndirect, "Framewin", ID_FRAMEWIN_0, PASSWD_DIGLOG_BOX_START_X, PASSWD_DIGLOG_BOX_START_Y, PASSWD_DIGLOG_BOX_END_X, PASSWD_DIGLOG_BOX_END_Y, 0, 0x0, 0 },
  { TEXT_CreateIndirect, "Text",        ID_TEXT_0, PASSWD_DIGLOG_BOX_TEXT_START_X, PASSWD_DIGLOG_BOX_TEXT_START_Y, PASSWD_DIGLOG_BOX_TEXT_END_X, PASSWD_DIGLOG_BOX_TEXT_END_Y, 0, 0x0, 0 },
  { EDIT_CreateIndirect, "Edit",        ID_EDIT_0, PASSWD_DIGLOG_BOX_EDIT_START_X, PASSWD_DIGLOG_BOX_EDIT_START_Y, PASSWD_DIGLOG_BOX_EDIT_END_X, PASSWD_DIGLOG_BOX_EDIT_END_Y, 0, 0x0, 0 },
};

/**
  ******************************************************************
  * @brief   建立主窗口
  * @param   [in]None
  * @retval  窗口句柄
  * @author  aron566
  * @version V1.0
  * @date    2020-7-6
  ******************************************************************
  */
static WM_HWIN CreateitemsPasswdCheckWindow(void)
{
	return GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), cbDialog, WM_HBKWIN, 0, 0);
}

/**
  ******************************************************************
  * @brief   初始化密码验证页面
  * @param   [in]WM_MESSAGE 窗口管理消息指针
  * @retval  None
  * @author  aron566
  * @version V1.0
  * @date    2020-7-6
  ******************************************************************
  */
static void cbDialog(WM_MESSAGE * pMsg)
{
    WM_EnableMemdev(pMsg->hWin);

    switch (pMsg->MsgId) 
    {
        case WM_INIT_DIALOG:
			/*创建标题*/
            CreateTittle(pMsg);
			/*创建编辑框*/
            CreateEditBox(pMsg);
			/*创建提示文本*/
			CreateTipsText(pMsg);
            break;
        case WM_NOTIFY_PARENT:
            break;
        case GW_WM_KEY:
        case WM_KEY:
            break;
        case WM_TIMER:
            break;
        default:
            WM_DefaultProc(pMsg);
            break;
    }
}

/**
  ******************************************************************
  * @brief   页面信息初始化
  * @param   [in]None
  * @retval  None
  * @author  aron566
  * @version V1.0
  * @date    2020-7-6
  ******************************************************************
  */
static void PasswdCheckPage_Init(char *str)
{
	strcpy(Password_Input ,"0000");
	Password_Raw = str;
	PasswdCheckTaskResult = 0;
}

/**
  ******************************************************************
  * @brief   初始化密码验证页面标题
  * @param   [in]WM_MESSAGE 窗口管理消息指针
  * @retval  None
  * @author  aron566
  * @version V1.0
  * @date    2020-7-6
  ******************************************************************
  */
static void CreateTittle(WM_MESSAGE * pMsg)
{
    WM_HWIN hItem;
    WM_EnableMemdev(pMsg->hWin);

	hItem = pMsg->hWin;
	
	/*设定标题*/
    FRAMEWIN_SetText(hItem, "Tips");
}

/**
  ******************************************************************
  * @brief   初始化密码验证页面编辑框
  * @param   [in]WM_MESSAGE 窗口管理消息指针
  * @retval  None
  * @author  aron566
  * @version V1.0
  * @date    2020-7-6
  ******************************************************************
  */
static void CreateEditBox(WM_MESSAGE * pMsg)
{
    WM_HWIN hItem;
    WM_EnableMemdev(pMsg->hWin);

	/*判断当前页面可创建按钮数目*/
    hItem = WM_GetDialogItem(pMsg->hWin, ID_EDIT_0);
	
	/*设置编辑框初始显示*/
  	EDIT_SetTextAlign(hItem, GUI_TA_VCENTER|GUI_TA_LEFT);
    EDIT_SetFont(hItem, &GUI_FontSongTi16);
    EDIT_EnableInversion(hItem,0);
    EDIT_SetText(hItem, Password_Input);

	/*开启聚焦与闪烁功能*/
    EDIT_SetFocussable(hItem,1);
    EDIT_EnableBlink(hItem,500,1);
	
	/*初始化聚焦字符*/
    EDIT_SetCursorAtChar(hItem, 0);
    WM_SetFocus(hItem);
}

/**
  ******************************************************************
  * @brief   初始化密码验证页面提示文本
  * @param   [in]WM_MESSAGE 窗口管理消息指针
  * @retval  None
  * @author  aron566
  * @version V1.0
  * @date    2020-7-6
  ******************************************************************
  */
static void CreateTipsText(WM_MESSAGE *pMsg)
{
	WM_HWIN hItem;
    hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_0);
		
	/*设置提示文本*/
    TEXT_SetFont(hItem, &GUI_FontSongTi16);
    TEXT_SetText(hItem, tr(GL_please_input_password));
}

/**
  ******************************************************************
  * @brief   更新页面编辑框聚焦
  * @param   [in]None
  * @retval  None
  * @author  aron566
  * @version V1.0
  * @date    2020-7-6
  ******************************************************************
  */
static void PasswdFocusUpdate(int postion)
{
	/*判断主窗口已建立*/
	if(MAINWin != 0)
	{
		WM_HWIN hItem = WM_GetDialogItem(MAINWin, ID_EDIT_0);
		WM_EnableMemdev(hItem);
		EDIT_SetCursorAtChar(hItem, postion);
	}
}

/**
  ******************************************************************
  * @brief   左移处理
  * @param   [in]None
  * @retval  None
  * @author  aron566
  * @version V1.0
  * @date    2020-7-6
  ******************************************************************
  */
static void PasswdCheckPage_Back(void)
{
    int Postion = 0;

    WM_HWIN hItem;
    hItem = WM_GetDialogItem(MAINWin, ID_EDIT_0);
    WM_EnableMemdev(hItem);
	
	/*获取当前编辑框聚焦的位置*/
    Postion = EDIT_GetCursorCharPos(hItem);
	
	/*翻转移动*/
	Postion = Postion-1 < 0?PASSWD_MAX_CHAR_NUM-1:Postion-1;

	/*更新聚焦*/
	PasswdFocusUpdate(Postion);
}

/**
  ******************************************************************
  * @brief   右移处理
  * @param   [in]None
  * @retval  None
  * @author  aron566
  * @version V1.0
  * @date    2020-5-20
  ******************************************************************
  */
static void PasswdCheckPage_Forward(void)
{
    int Postion = 0;

    WM_HWIN hItem;
    hItem = WM_GetDialogItem(MAINWin, ID_EDIT_0);
    WM_EnableMemdev(hItem);
	
	/*获取当前编辑框聚焦的位置*/
    Postion = EDIT_GetCursorCharPos(hItem);
	
	/*翻转移动*/
	Postion = Postion+1 > PASSWD_MAX_CHAR_NUM-1?0:Postion+1;

	/*更新聚焦*/
	PasswdFocusUpdate(Postion);		
}

/**
  ******************************************************************
  * @brief	 上键处理
  * @param   [in]None
  * @retval  None
  * @author  aron566
  * @version V1.0
  * @date    2020-7-6
  ******************************************************************
  */
static void PasswdCheckPage_Up(void)
{
    int Postion = 0;

    WM_HWIN hItem;
    hItem = WM_GetDialogItem(MAINWin, ID_EDIT_0);
    WM_EnableMemdev(hItem);
	
	/*获取当前编辑框聚焦的位置*/
    Postion = EDIT_GetCursorCharPos(hItem);
	
	/*Postion范围0-3,限制访问越界问题*/
	Postion = Postion > PASSWD_MAX_CHAR_NUM?0:Postion;

	Password_Input[Postion]++;

	/*限制输入范围[0-9]*/
	Password_Input[Postion] = Password_Input[Postion] > '9'?'0':Password_Input[Postion];

	/*重新设置编辑框文本*/
	EDIT_SetText(hItem ,Password_Input);

	/*更新聚焦*/
	PasswdFocusUpdate(Postion);		
}

/**
  ******************************************************************
  * @brief	 下键处理
  * @param   [in]None
  * @retval  None
  * @author  aron566
  * @version V1.0
  * @date    2020-5-20
  ******************************************************************
  */
static void PasswdCheckPage_Down(void)
{
    int Postion = 0;

    WM_HWIN hItem;
    hItem = WM_GetDialogItem(MAINWin, ID_EDIT_0);
    WM_EnableMemdev(hItem);
	
	/*获取当前编辑框聚焦的位置*/
    Postion = EDIT_GetCursorCharPos(hItem);
	
	/*Postion范围0-3,限制访问越界问题*/
	Postion = Postion > PASSWD_MAX_CHAR_NUM?0:Postion;

	Password_Input[Postion]--;

	/*限制输入范围[0-9]*/
	Password_Input[Postion] = Password_Input[Postion] < '0'?'9':Password_Input[Postion];

	/*重新设置编辑框文本*/
	EDIT_SetText(hItem ,Password_Input);

	/*更新聚焦*/
	PasswdFocusUpdate(Postion);
}

/**
  ******************************************************************
  * @brief	 OK键处理
  * @param   [in]None
  * @retval  None
  * @author  aron566
  * @version V1.0
  * @date    2020-5-20
  ******************************************************************
  */
static void PasswdCheckPage_Ok(void)
{
	/*验证密码是否正确*/
	if(0 == strcmp(Password_Input ,Password_Raw))
	{
		PasswdCheckTaskResult = 1;
		PasswdCheckWindowHide();
	}
	else
	{
		PasswdCheckTaskResult = 0;
	}
}

/**
  ******************************************************************
  * @brief	 返回键处理
  * @param   [in]None
  * @retval  None
  * @author  aron566
  * @version V1.0
  * @date    2020-5-20
  ******************************************************************
  */
static void PasswdCheckPage_Return(void)
{
	PasswdCheckWindowHide();
}

/**
  ******************************************************************
  * @brief   密码验证窗口隐藏
  * @param   [in]None
  * @retval  None
  * @author  aron566
  * @version V1.0
  * @date    2020-7-6
  ******************************************************************
  */
static void PasswdCheckWindowHide(void)
{
    if(MAINWin)
    {
        WM_DeleteWindow(MAINWin);
        MAINWin = 0;
		Password_Raw = NULL;
    }
}
/** Public application code --------------------------------------------------*/
/*******************************************************************************
*
*       Public code
*
********************************************************************************
*/
/**
  ******************************************************************
  * @brief   获取运行结果状态
  * @param   [in]None
  * @retval  1  成功
  * @retval  0  失败
  * @author  aron566
  * @version V1.0
  * @date    2020-5-21
  ******************************************************************
  */
uint32_t PasswdCheckWindowGetTaskResult(void)
{
	if(PasswdCheckTaskResult)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

/**
  ******************************************************************
  * @brief   密码验证窗口按键处理
  * @param   [in]Key
  * @retval  当前窗口状态:1显示 0关闭
  * @author  aron566
  * @version V1.0
  * @date    2020-7-6
  ******************************************************************
  */
uint8_t PasswdCheckWindowSendKey(int key)
{
	/*判断窗口是否处于激活状态则下发按键*/
	if(PasswdCheckWindowGetState())
	{
		switch(key)
		{
			case GUI_KEY_UP:
			case KEY_BUTTON_UP:
			case KEY_UP:
				PasswdCheckPage_Up();
				break;
			case GUI_KEY_DOWN:
			case KEY_BUTTON_DOWN:
			case KEY_DOWN:
				PasswdCheckPage_Down();
				break;
			case GUI_KEY_LEFT:
			case KEY_BUTTON_LEFT:
			case KEY_LEFT:
				PasswdCheckPage_Back();
				break;
			case GUI_KEY_RIGHT:
			case KEY_BUTTON_RIGHT:
			case KEY_RIGHT:
				PasswdCheckPage_Forward();
				break;
			case GUI_KEY_ENTER:
			case KEY_OK:
				PasswdCheckPage_Ok();
				break;
			case GUI_KEY_ESCAPE:
			case KEY_ESC:
				PasswdCheckPage_Return();
				break;
			default:
				break;
		}
	}
	/*返回当前窗口状态*/
	return PasswdCheckWindowGetState();
}

/**
  ******************************************************************
  * @brief   密码验证窗口,状态获取
  * @param   [in]None
  * @retval  1  窗口显示中
  * @retval  0  窗口已失效
  * @author  aron566
  * @version V1.0
  * @date    2020-7-6
  ******************************************************************
  */
uint8_t PasswdCheckWindowGetState(void)
{
	/*获取窗口状态*/
	if(MAINWin)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

/**
  ******************************************************************
  * @brief   密码验证窗口显示
  * @param   [in]需进行验证的字符串
  * @retval  None
  * @author  aron566
  * @version V1.0
  * @date    2020-7-6
  ******************************************************************
  */
void PasswdCheckWindowShow(char *checkKey)
{
	if(MAINWin == 0)
	{
		/*重置窗口资源*/
		PasswdCheckPage_Init(checkKey);
		MAINWin = CreateitemsPasswdCheckWindow();
	}
}

#ifdef __cplusplus ///
}
#endif

调用接口

PasswdCheck.h

void PasswdCheckWindowShow(char *checkKey);
uint8_t PasswdCheckWindowGetState(void);
uint8_t PasswdCheckWindowSendKey(int key);
uint32_t PasswdCheckWindowGetTaskResult(void);

你可能感兴趣的:(STemWin)