|
楼主 |
发表于 2017-3-28 18:10:26
|
显示全部楼层
60.4 官方DIALOG_SliderColor.c实例讲解
这个例子在模拟器中的位置:
主要功能介绍:
这个例子非常具有代表性,而且实战。这个例子实现了一个简单的颜色板功能,通过三个滑块控件分别控制R,G,B三个颜色通道,数值范围都是0-255。用户调节了滑块控件后,将最终的颜色效果显示出来。
程序代码如下:
- #include <stddef.h>
- #include "GUI.h"
- #include "DIALOG.h"
-
- /*********************************************************************
- *
- * Defines
- *
- **********************************************************************
- */
- //
- // Recommended memory to run the sample with adequate performance
- //
- #define RECOMMENDED_MEMORY (1024L * 15)
-
- /*********************************************************************
- *
- * Static data, SeggerLogo
- *
- **********************************************************************
- */
- /*********************************************************************
- *
- * Bitmap SeggerLogo
- */
- static const GUI_COLOR ColorsSeggerLogoBlue16[] = {
- 0x0000FF,0x000000,0x888888,0xFFFFFF
- ,0xFF0000,0xBBBBBB
- };
-
- static const GUI_LOGPALETTE PalSeggerLogoBlue16 = {
- 6, // number of entries
- 1, // Has transparency
- &ColorsSeggerLogoBlue16[0]
- };
-
- static const unsigned char acSeggerLogoBlue16[] = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- /* 其余代码省略未贴 */
- };
-
- static const GUI_BITMAP bmSeggerLogoBlue = {
- 138, // XSize
- 65, // YSize
- 69, // BytesPerLine
- 4, // BitsPerPixel
- acSeggerLogoBlue16, // Pointer to picture data (indices)
- &PalSeggerLogoBlue16 // Pointer to palette
- };
-
- /*********************************************************************
- *
- * Static data, dialog resource
- *
- **********************************************************************
- */
- /*********************************************************************
- *
- * Dialog resource
- *
- * Function description
- * This table conatins the info required to create the dialog.
- * It has been created manually, but could also be created by a GUI-builder.
- */
- static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = { //--------------(1)
- { FRAMEWIN_CreateIndirect, "Adjust color", 0, 10, 40, 300, 160, FRAMEWIN_CF_MOVEABLE},
- { TEXT_CreateIndirect, "Red:" , 0, 5, 20, 35, 20, TEXT_CF_LEFT },
- { TEXT_CreateIndirect, "Green:", 0, 5, 50, 35, 20, TEXT_CF_LEFT },
- { TEXT_CreateIndirect, "Blue:", 0, 5, 80, 35, 20, TEXT_CF_LEFT },
- { TEXT_CreateIndirect, "Preview",0, 205, 4, 81, 15, TEXT_CF_HCENTER },
- { SLIDER_CreateIndirect, NULL, GUI_ID_SLIDER0, 40, 20, 100, 20 },
- { SLIDER_CreateIndirect, NULL, GUI_ID_SLIDER1, 40, 50, 100, 20 },
- { SLIDER_CreateIndirect, NULL, GUI_ID_SLIDER2, 40, 80, 100, 20 },
- { EDIT_CreateIndirect, NULL, GUI_ID_EDIT0, 145, 20, 30, 20, 0, 3 },
- { EDIT_CreateIndirect, NULL, GUI_ID_EDIT1, 145, 50, 30, 20, 0, 3 },
- { EDIT_CreateIndirect, NULL, GUI_ID_EDIT2, 145, 80, 30, 20, 0, 3 },
- { BUTTON_CreateIndirect, "OK", GUI_ID_OK, 10, 110, 60, 20 },
- { BUTTON_CreateIndirect, "Cancel", GUI_ID_CANCEL, 230, 110, 60, 20 },
- };
-
- /*********************************************************************
- *
- * Static data, colors
- *
- **********************************************************************
- */
- static U8 _aColorSep[3] = {0, 127, 255}; // Red, green and blue components
-
- /*********************************************************************
- *
- * Static code
- *
- **********************************************************************
- */
- /*********************************************************************
- *
- * _OnPaint
- *
- * Function description
- * This routine draws the color rectangles.
- * The widgets are drawn automatically.
- */
- static void _OnPaint(void) { //--------------(2)
- //
- // Draw RGB values
- //
- GUI_SetColor(_aColorSep[0]);
- GUI_FillRect(180, 20, 199, 39);
- GUI_SetColor(_aColorSep[1] << 8);
- GUI_FillRect(180, 50, 199, 69);
- GUI_SetColor(((U32)_aColorSep[2]) << 16);
- GUI_FillRect(180, 80, 199, 99);
- //
- // Draw resulting color
- //
- GUI_SetColor(_aColorSep[0] | (((U32)_aColorSep[1]) << 8) | (((U32)_aColorSep[2]) << 16));
- GUI_FillRect(205, 20, 285, 99);
- }
-
- /*********************************************************************
- *
- * _OnValueChanged
- */
- static void _OnValueChanged(WM_HWIN hDlg, int Id) {
- unsigned Index;
- unsigned v;
- WM_HWIN hSlider;
- WM_HWIN hEdit;
-
- Index = 0;
- v = 0;
- if ((Id >= GUI_ID_SLIDER0) && (Id <= GUI_ID_SLIDER2)) { //--------------(3)
- Index = Id - GUI_ID_SLIDER0;
- //
- // SLIDER-widget has changed, update EDIT-widget //--------------(4)
- //
- hSlider = WM_GetDialogItem(hDlg, GUI_ID_SLIDER0 + Index);
- hEdit = WM_GetDialogItem(hDlg, GUI_ID_EDIT0 + Index);
- v = SLIDER_GetValue(hSlider);
- EDIT_SetValue(hEdit, v);
- } else if ((Id >= GUI_ID_EDIT0) && (Id <= GUI_ID_EDIT2)) {
- Index = Id - GUI_ID_EDIT0;
- //
- // If EDIT-widget has changed, update SLIDER-widget //--------------(5)
- //
- hSlider = WM_GetDialogItem(hDlg, GUI_ID_SLIDER0 + Index);
- hEdit = WM_GetDialogItem(hDlg, GUI_ID_EDIT0 + Index);
- v = EDIT_GetValue(hEdit);
- SLIDER_SetValue(hSlider, v);
- }
- _aColorSep[Index] = v;
- //
- // At last invalidate dialog client window //--------------(6)
- //
- WM_InvalidateWindow(WM_GetClientWindow(hDlg));
- }
-
- /*********************************************************************
- *
- * _cbBkWindow
- */
- static void _cbBkWindow(WM_MESSAGE * pMsg) { //--------------(7)
- int xPos;
- int yPos;
-
- xPos = LCD_GetXSize() / 2 - bmSeggerLogoBlue.XSize/2;
- yPos = LCD_GetYSize() / 2 - bmSeggerLogoBlue.YSize/2 ;
- switch (pMsg->MsgId) {
- case WM_PAINT:
- GUI_SetBkColor(GUI_BLUE);
- GUI_Clear();
- GUI_SetColor(GUI_WHITE);
- GUI_SetFont(&GUI_Font24_ASCII);
- GUI_DispStringHCenterAt("DIALOG_SliderColor - Sample", 160, 5);
- GUI_DrawBitmap(&bmSeggerLogoBlue, xPos, yPos );
- default:
- WM_DefaultProc(pMsg);
- }
- }
-
- /*********************************************************************
- *
- * _cbCallback
- */
- static void _cbCallback(WM_MESSAGE * pMsg) {
- WM_HWIN hDlg;
- WM_HWIN hItem;
- int i;
- int NCode;
- int Id;
-
- hDlg = pMsg->hWin;
- switch (pMsg->MsgId) {
- case WM_PAINT:
- _OnPaint();
- return;
- case WM_INIT_DIALOG:
- for (i = 0; i < 3; i++) { //--------------(8)
- hItem = WM_GetDialogItem(hDlg, GUI_ID_SLIDER0 + i);
- SLIDER_SetRange(hItem, 0, 255);
- SLIDER_SetValue(hItem, _aColorSep[i]);
- //
- // Init EDIT-widgets
- //
- hItem = WM_GetDialogItem(hDlg, GUI_ID_EDIT0 + i);
- EDIT_SetDecMode(hItem, _aColorSep[i], 0, 255, 0, 0);
- }
- break;
- case WM_KEY:
- switch (((WM_KEY_INFO*)(pMsg->Data.p))->Key) {
- case GUI_KEY_ESCAPE:
- ;
- break;
- case GUI_KEY_ENTER:
- GUI_EndDialog(hDlg, 0);
- break;
- }
- break;
- case WM_NOTIFY_PARENT:
- Id = WM_GetId(pMsg->hWinSrc); // Id of widget
- NCode = pMsg->Data.v; // Notification code
- switch (NCode) {
- case WM_NOTIFICATION_RELEASED: // React only if released
- if (Id == GUI_ID_OK) { // OK Button
- GUI_EndDialog(hDlg, 0);
- }
- if (Id == GUI_ID_CANCEL) { // Cancel Button
- GUI_EndDialog(hDlg, 1);
- }
- break;
- case WM_NOTIFICATION_VALUE_CHANGED: // Value has changed //--------------(9)
- _OnValueChanged(hDlg, Id);
- break;
- }
- break;
- default:
- WM_DefaultProc(pMsg);
- }
- }
-
- /*********************************************************************
- *
- * Public code
- *
- **********************************************************************
- */
- /*********************************************************************
- *
- * MainTask
- */
- void MainTask(void) {
- WM_SetCreateFlags(WM_CF_MEMDEV); // Use memory devices on all windows to avoid flicker
- GUI_Init();
- //
- // Check if recommended memory for the sample is available
- //
- if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
- GUI_ErrorOut("Not enough memory available.");
- return;
- }
- GUI_CURSOR_Show();
- WM_SetCallback(WM_HBKWIN, _cbBkWindow);
- while (1) {
- GUI_ExecDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), &_cbCallback, 0, 0, 0);
- GUI_Delay(1000);
- }
- }
复制代码 1. 对话框资源列表。
2. 这个函数用于绘制三个小的单色矩形方框和一个大的矩形方框。
3. 通过控件ID号判断此控件是否是滑块控件。
4. 如果是滑块控件,获取滑块控件的值来设置编辑框显示的数值,保证两者是统一的。
5. 通过控件ID号判断此控件是否是编辑框控件。如果是编辑框控件,获取编辑框控件的值来设置滑块控件的数值,保证两者是统一的。
6. 通过函数WM_InvalidateWindow让对话框客户端窗口无效,从而触发对话框回调函数中WM_PAINT重绘消息的执行。
7. 桌面窗口回调函数。
8. 对话框初始化消息,用来初始化三个滑块控件和三个编辑框控件。其中,滑块控件的范围是通过函数SLIDER_SetRange来设置,
而滑块控件的数值通过函数SLIDER_SetValue来设置。
9. 编辑框的数值或者滑块控件中滑动条的位置发生改变,都会触发这个消息。
实际显示效果如下: |
|