欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

emWin的Button控件的单击、双击、长按等识别

程序员文章站 2022-07-13 23:09:33
...

emWin的button按钮控件只有按下和弹起两种消息,想识别emWin的双击、长按等还不得不另寻他法。
突然想到emWin的button和物理机械按钮有什么不同呢?MulitiButton库应该可以用,try to do it.

MulitiButton仓库地址:GIthub地址:https://github.com/0x1abin/MultiButton

使用起来和物理机械按键一样

1、定义button对象

static struct Button g_PressureUp;   //
static struct Button g_PressureDown; //
static struct Button g_FlowUpBtn;    //

2、定义电平获取函数

static uint8_t pressureUp = 1;
static uint8_t ReadPressureUpBtn(void)
{
    return pressureUp;
}

static uint8_t pressureDown = 1;
static uint8_t ReadPressureDownBtn(void)
{
    return pressureDown;
}

static uint8_t flowUp = 1;
static uint8_t ReadFlowUpBtn(void)
{
    return flowUp;
}

和物理机械按键不同就在于获取按钮电平不同:
在Button的消息处理,将标志电平变量在WM_NOTIFICATION_CLICKED消息中清零
WM_NOTIFICATION_RELEASED消息中置1

 switch (id)
 {
    case ID_BUTTON_0: // Notifications sent by 'pressure up button'
    {
        switch (NCode)
        {
        case WM_NOTIFICATION_CLICKED:
            pressureUp = 0;
            break;
        case WM_NOTIFICATION_RELEASED:
            pressureUp = 1;
            break;
        }
        break;
    }
    case ID_BUTTON_1: // Notifications sent by 'Pressure down button'
    {
        switch (NCode)
        {
        case WM_NOTIFICATION_CLICKED:
            pressureDown = 0;
            break;
        case WM_NOTIFICATION_RELEASED:
            pressureDown = 1;
            break;
        }
        break;
    }
    ...
    ...
}

3、初始化、设置回调处理函数以及启动Button

button_init(&g_PressureUp, ReadPressureUpBtn, 0);
button_init(&g_PressureDown, ReadPressureDownBtn, 0);
button_init(&g_FlowUpBtn, ReadFlowUpBtn, 0);

//	button_attach(&btn1, PRESS_REPEAT,     BTN1_PRESS_REPEAT_Handler);
button_attach(&g_PressureUp, SINGLE_CLICK, PressureUpBtnSingleClickHandler);
button_attach(&g_PressureUp, DOUBLE_CLICK, PressureUpBtnDoubleClickHandler);

button_attach(&g_PressureDown, SINGLE_CLICK, PressureUpBtnSingleClickHandler);

button_attach(&g_FlowUpBtn, SINGLE_CLICK, PressureUpBtnSingleClickHandler);

button_start(&g_PressureUp);
button_start(&g_PressureDown);
button_start(&g_FlowUpBtn);

4、给MulitButton提供时基

 WM_CreateTimer(hWinParent, 0, 5, 0);	  //创建5ms定时器

 switch (pMsg->MsgId)
 {
      case WM_INIT_DIALOG:
      {
           InitMainWindow(pMsg->hWin);
           break;
      }
      case WM_PAINT:
      {                           
           WindowPaint();
           break;
      }
      case WM_TIMER:           //time out msg  
      {
			button_ticks();
           	WM_RestartTimer(pMsg->Data.v, 5);  //重启定时器
           break;
      }
}