Hi,
Try the code below. The font gets created within the parent window of a button. The font data and the handle to the file needs to be valid as long as the button uses the font. Therefore the variables are declared static.
On delete of the window (which does not occur in this example) the button gets also deleted (automatically). At this point the font is not used anymore and can be deleted, too. Although, the window never gets deleted in this example it should show how to make sure a font stays valid as long as it is used.
Display All
Regards,
Sven
Try the code below. The font gets created within the parent window of a button. The font data and the handle to the file needs to be valid as long as the button uses the font. Therefore the variables are declared static.
On delete of the window (which does not occur in this example) the button gets also deleted (automatically). At this point the font is not used anymore and can be deleted, too. Although, the window never gets deleted in this example it should show how to make sure a font stays valid as long as it is used.
C Source 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 : GUI_SetExternalFont_XBF.c
- Purpose : Sample that demonstrates how to create a custom font
- from an external .XBF file
- This sample supports both usage on a Windows PC as well
- as usage on a target device using emFile.
- Requirements: WindowManager - ( )
- MemoryDevices - ( )
- AntiAliasing - ( )
- VNC-Server - ( )
- PNG-Library - ( )
- TrueTypeFonts - ( )
- ---------------------------END-OF-HEADER------------------------------
- */
- #include "DIALOG.h"
- #include <Windows.h>
- /*********************************************************************
- *
- * Defines
- *
- **********************************************************************
- */
- #define FILE_PATH "C:\\Work\\emWin\\Intern\\TutorialV6\\Core\\Fonts\\Roboto30.xbf"
- /*********************************************************************
- *
- * Static code
- *
- **********************************************************************
- */
- /*********************************************************************
- *
- * _GetData
- */
- static int _GetData(U32 Off, U16 NumBytes, void * pVoid, void * pBuffer) {
- HANDLE hFile;
- DWORD NumBytesRead;
- hFile = *(HANDLE *)pVoid;
- //
- // Set file pointer to the requested position
- //
- if (SetFilePointer(hFile, Off, 0, FILE_BEGIN) == 0xFFFFFFFF) {
- return 1; // Error
- }
- //
- // Read font data
- //
- if (!ReadFile(hFile, pBuffer, NumBytes, &NumBytesRead, 0)) {
- return 1; // Error
- }
- if (NumBytesRead != NumBytes) {
- return 1; // Error
- }
- return 0; // Ok
- }
- /*********************************************************************
- *
- * _cbBk
- */
- static void _cbBk(WM_MESSAGE * pMsg) {
- static HANDLE hFile;
- static GUI_RECT Rect;
- static GUI_FONT Font;
- static GUI_XBF_DATA Data;
- WM_HWIN hItem;
- switch (pMsg->MsgId) {
- case WM_CREATE:
- //
- // Create file handle
- //
- hFile = CreateFile(FILE_PATH, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
- //
- // Create the font from the XBF file
- // The font and xbf data will be loaded into the two variables created above.
- // This data has to be valid during the time we use the font.
- //
- GUI_XBF_CreateFont((GUI_FONT*)&Font, &Data, GUI_XBF_TYPE_PROP_AA4_EXT, _GetData, &hFile);
- hItem = BUTTON_CreateEx(10, 10, 160, 40, pMsg->hWin, WM_CF_SHOW, 0, 0);
- BUTTON_SetFont(hItem, &Font);
- BUTTON_SetText(hItem, "A button");
- break;
- case WM_PAINT:
- GUI_SetBkColor(GUI_BLACK);
- GUI_Clear();
- break;
- case WM_DELETE:
- //
- // Close file handle
- //
- CloseHandle(hFile);
- //
- // When the font is no longer currently used, it should be deleted.
- //
- GUI_XBF_DeleteFont(&Font);
- break;
- default:
- WM_DefaultProc(pMsg);
- break;
- }
- }
- /*********************************************************************
- *
- * Public code
- *
- **********************************************************************
- */
- /*********************************************************************
- *
- * MainTask
- */
- void MainTask(void) {
- //
- // Init emWin.
- //
- GUI_Init();
- WM_MULTIBUF_Enable(1);
- WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbBk, 0);
- while (1) {
- GUI_Delay(100);
- }
- }
- /*************************** End of file ****************************/
Regards,
Sven
Please read the forum rules before posting.
Keep in mind, this is *not* a support forum.
Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
Should you be entitled to support you can contact us via our support system: segger.com/ticket/
Or you can contact us via e-mail.
Keep in mind, this is *not* a support forum.
Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
Should you be entitled to support you can contact us via our support system: segger.com/ticket/
Or you can contact us via e-mail.