BUTTON - Custom (Sample)

From SEGGER Wiki
Jump to: navigation, search
BUTTON_Custom.c
BUTTON Custom.gif
File(s) required
  • BUTTON_Custom.c
Runs in simulation Yes
Runs on target Yes
Download BUTTON_Custom.c

This sample demonstrates custom drawing of a BUTTON widget.

Code

/*********************************************************************
*                    SEGGER Microcontroller GmbH                     *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2020  SEGGER Microcontroller GmbH                *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V6.10 - Graphical user interface for embedded applications **
emWin is protected by international copyright laws.   Knowledge of the
source code may not be used to write a similar product.  This file may
only  be used  in accordance  with  a license  and should  not be  re-
distributed in any way. We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : BUTTON_Custom.c
Purpose     : Sample that demonstrates how the user can give a
              BUTTON widget a custom look.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include "DIALOG.h"

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
static void _cbButton(WM_MESSAGE * pMsg) {
  GUI_RECT Rect;

  switch(pMsg->MsgId) {
  case WM_PAINT:
    //
    // Anything drawn here will be how the button looks.
    //
    if(BUTTON_IsPressed(pMsg->hWin)) {
      GUI_SetColor(GUI_LIGHTRED);
    } else {
      GUI_SetColor(GUI_GRAY_AA);
    }
    WM_GetClientRect(&Rect);
    Rect.x0 += 1;
    Rect.x1 -= 1;
    Rect.y0 += 1;
    Rect.y1 -= 1;
    GUI_FillRoundedRectEx(&Rect, 3);
    GUI_SetColor(GUI_BLACK);
    GUI_DrawRoundedRect(Rect.x0, Rect.y0, Rect.x1, Rect.y1, 3);
    GUI_SetColor(GUI_WHITE);
    GUI_SetTextMode(GUI_TM_TRANS);
    GUI_DispStringInRect("Button", &Rect, GUI_TA_HCENTER | GUI_TA_VCENTER);
    break;
  default:
    //
    // Anything else apart from drawing is handled by the default callback.
    //
    BUTTON_Callback(pMsg);
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  BUTTON_Handle hButton;

  //
  // Init emWin.
  //
  GUI_Init();
  WM_SetDesktopColor(GUI_WHITE);
  //
  // Create BUTTON widget.
  //
  hButton = BUTTON_CreateEx(10, 10, 100, 30, WM_HBKWIN, WM_CF_SHOW | WM_CF_HASTRANS, 0, GUI_ID_BUTTON0);
  //
  // Overwrite callback of BUTTON.
  //
  WM_SetCallback(hButton, _cbButton);

  while (1) {
    GUI_Delay(100);
  }
}

/*************************** End of file ****************************/