WM Transparent Windows (Sample)
WM_TransparentWindows.c | |
---|---|
File(s) required |
|
Runs in simulation | Yes |
Runs on target | Yes |
Download | WM_TransparentWindows.c |
This sample demonstrates how transparent windows can be created. It also shows how semi-transparent drawing operations are performed in a transparent window.
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 : WM_TransparentWindows.c
Purpose : Sample that shows how to create fully transparent and
semi-transparent windows in emWin.
Requirements: WindowManager - (x)
MemoryDevices - ( )
AntiAliasing - ( )
VNC-Server - ( )
PNG-Library - ( )
TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/
#include "DIALOG.h"
/*********************************************************************
*
* Defines
*
**********************************************************************
*/
//
// Make sure to use colors that have transparency bits set
// When using BGR mode, the format of a color should look like this:
// 0xTTBBGGRR where T stands for transparency
//
#define COLOR_1 GUI_MAKE_COLOR(0x60EBC2FB)
#define COLOR_2 GUI_MAKE_COLOR(0xC0EEC1A6)
//
// Properties for gray background pattern.
// This pattern should emphasize the semi-transparent drawings.
//
#define BK_COLOR_1 GUI_MAKE_COLOR(0x00666666)
#define BK_COLOR_2 GUI_MAKE_COLOR(0x00999999)
#define RECT_SIZE 8
/*********************************************************************
*
* Static code
*
**********************************************************************
*/
/*********************************************************************
*
* _cbWin0
*/
static void _cbWin0(WM_MESSAGE * pMsg) {
GUI_RECT Rect;
switch (pMsg->MsgId) {
case WM_PAINT:
//
// Draw something
//
WM_GetClientRect(&Rect);
Rect.x0 += 5;
Rect.y0 += 5;
Rect.x1 -= 5;
Rect.y1 -= 5;
GUI_SetPenSize(5);
GUI_SetColor(GUI_RED);
GUI_AA_DrawRoundedRectEx(&Rect, 3);
break;
default:
WM_DefaultProc(pMsg);
break;
}
}
/*********************************************************************
*
* _cbWin1
*/
static void _cbWin1(WM_MESSAGE * pMsg) {
GUI_RECT Rect;
switch (pMsg->MsgId) {
case WM_PAINT:
//
// Enable alpha blending to use semi-transparency
//
GUI_EnableAlpha(1);
//
// Draw something...
// Make sure to use colors that have transparency bits set
// When using BGR mode, the format of a color should look like this:
// 0xTTBBGGRR where T stands for transparency
//
WM_GetClientRect(&Rect);
GUI_DrawGradientRoundedH(Rect.x0, Rect.y0, Rect.x1, Rect.y1, 3, COLOR_1, COLOR_2);
//
// Disable alpha blending when semi-transparent drawing operations are done.
// If it doesn't get disabled, it might have a heavy impact on performance.
//
GUI_EnableAlpha(0);
break;
default:
WM_DefaultProc(pMsg);
break;
}
}
/*********************************************************************
*
* _DrawBkPattern
*
* Purpose: This routine draws the grey background pattern to highlight
* the semi-transparent drawings made in this sample.
*/
static void _DrawBkPattern(void) {
int i, j;
int xSize, ySize;
GUI_COLOR Color;
U8 a;
xSize = LCD_GetXSize();
ySize = LCD_GetYSize();
a = 0;
for (i = 0; i < xSize; i += RECT_SIZE) {
a ^= 1;
for(j = 0; j < ySize; j += RECT_SIZE) {
a ^= 1;
Color = (a % 2) ? BK_COLOR_1 : BK_COLOR_2;
GUI_SetColor(Color);
GUI_FillRect(i, j, i + RECT_SIZE, j + RECT_SIZE);
}
}
}
/*********************************************************************
*
* Public code
*
**********************************************************************
*/
/*********************************************************************
*
* MainTask
*/
void MainTask(void) {
//
// Init GUI
//
GUI_Init();
_DrawBkPattern();
//
// Create two windows both having the WM_CF_HASTRANS flag
// This flag makes a window or widget fully transparent
//
WM_CreateWindowAsChild(10, 10, 100, 100, WM_HBKWIN, WM_CF_SHOW | WM_CF_HASTRANS, _cbWin0, 0);
WM_CreateWindowAsChild(120, 10, 100, 100, WM_HBKWIN, WM_CF_SHOW | WM_CF_HASTRANS, _cbWin1, 0);
while (1) {
GUI_Delay(100);
}
}
/*************************** End of file ****************************/