|
楼主 |
发表于 2017-3-30 14:59:43
|
显示全部楼层
61.4官方WIDGET_ListView.c实例讲解
这个例子在模拟器中的位置:
主要功能介绍:
这个例子很好的演示了列表控件大部分函数的使用,而且比较容易理解,适合初学者学习参考。
程序代码如下:
- #include <stddef.h>
- #include <string.h>
- #include "GUI.h"
- #include "LISTVIEW.h"
- #include "FRAMEWIN.h"
-
- /*********************************************************************
- *
- * Defines
- *
- **********************************************************************
- */
- #define SPEED 650
- #define MSG_CHANGE_MAIN_TEXT (WM_USER + 0)
- #define MSG_CHANGE_INFO_TEXT (WM_USER + 1)
-
- //
- // Recommended memory to run the sample with adequate performance
- //
- #define RECOMMENDED_MEMORY (1024L * 10)
-
- /*********************************************************************
- *
- * Static data
- *
- **********************************************************************
- */
- static const GUI_COLOR Colors[] = { 0, GUI_BLUE };
-
- static const GUI_LOGPALETTE Palette = {
- 2, /* bps */
- 1, /* transparency */
- Colors /* pointer to colors */
- };
-
- static const unsigned char acBarCode[] = {
- _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_, ________, ________, ________, ________, _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_,
- _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_, ________, ________, ________, ________, _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_,
- _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_, ________, ________, ________, ________, _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_,
- _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_, ________, ________, ________, ________, _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_,
- _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_, ________, ________, ________, ________, _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_,
- _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_, ________, ________, ________, ________, _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_,
- _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_, ________, ________, ________, ________, _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_,
- _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_, ________, ________, ________, ________, _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_,
- _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_, ________, ________, ________, ________, _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_,
- _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_, ________, ________, ________, ________, _XXX_X__, XXX__XX_, X_X_X_XX, __X_XXX_,
- };
-
- static const GUI_BITMAP bmBarCode = { 96, 10, 12, 1, acBarCode, &Palette};
-
- static const char * _aTable_1[][3] = {
- { "623499-0010001", "1", "Item 1" },
- { "623499-0010002", "2", "Item 2" },
- { "623499-0010003", "3", "Item 3" },
- { "623499-0010004", "4", "Item 4" },
- { "623499-0010005", "5", "Item 5" },
- { "623499-0010006", "6", "Item 6" },
- { "623499-0010007", "7", "Item 7" },
- { "623499-0010008", "8", "Item 8" }
- };
-
- static const char * _aTable_2[][3] = {
- { "111111-1111111", "100-A", "Item AAA" },
- { "222222-2222222", "100-B", "Item BBB" },
- { "333333-3333333", "100-C", "Item CCC" },
- { "444444-4444444", "100-D", "Item DDD" },
- { "555555-5555555", "100-E", "Item EEE" },
- { "666666-6666666", "100-F", "Item FFF" },
- { "777777-7777777", "100-G", "Item GGG" },
- { "888888-8888888", "100-H", "Item HHH" }
- };
-
- static LISTVIEW_Handle _hListView;
-
- static char _acMainText[100];
- static char _acInfoText[100];
-
- /*********************************************************************
- *
- * Static code
- *
- **********************************************************************
- */
- /*******************************************************************
- *
- * _ChangeMainText
- *
- * Sends a message to the background window and invalidate it, so
- * the callback of the background window display the new text.
- */
- static void _ChangeMainText(char* pStr) { //--------------(1)
- WM_MESSAGE Message;
-
- Message.MsgId = MSG_CHANGE_MAIN_TEXT;
- Message.Data.p = pStr;
- WM_SendMessage(WM_HBKWIN, &Message);
- WM_InvalidateWindow(WM_HBKWIN);
- }
-
- /*******************************************************************
- *
- * _ChangeInfoText
- *
- * Sends a message to the background window and invalidate it, so
- * the callback of the background window display the new text.
- */
- static void _ChangeInfoText(char* pStr) { //--------------(2)
- WM_MESSAGE Message;
-
- Message.MsgId = MSG_CHANGE_INFO_TEXT;
- Message.Data.p = pStr;
- WM_SendMessage(WM_HBKWIN, &Message);
- WM_InvalidateWindow(WM_HBKWIN);
- GUI_Delay(SPEED);
- }
-
- /*******************************************************************
- *
- * _cbBkWindow
- */
- static void _cbBkWindow(WM_MESSAGE* pMsg) { //--------------(3)
- switch (pMsg->MsgId) {
- case MSG_CHANGE_MAIN_TEXT: //--------------(4)
- strcpy(_acMainText, (char const *)pMsg->Data.p);
- WM_InvalidateWindow(pMsg->hWin);
- break;
- case MSG_CHANGE_INFO_TEXT: //--------------(5)
- strcpy(_acInfoText, (char const *)pMsg->Data.p);
- WM_InvalidateWindow(pMsg->hWin);
- break;
- case WM_PAINT:
- GUI_SetBkColor(GUI_BLACK);
- GUI_Clear();
- GUI_SetColor(GUI_WHITE);
- GUI_SetFont(&GUI_Font24_ASCII);
- GUI_DispStringHCenterAt("LISTVIEW Widget - Sample", 160, 5);
- GUI_SetFont(&GUI_Font8x16);
- GUI_DispStringAt(_acMainText, 5, 40);
- GUI_SetFont(&GUI_Font8x8);
- GUI_DispStringAt(_acInfoText, 5, 60);
- break;
- default:
- WM_DefaultProc(pMsg);
- }
- }
-
- /*********************************************************************
- *
- * _Demo
- */
- static void _Demo(void) {
- unsigned int i;
- unsigned int j;
-
- int Key = 0;
- int Cnt = 15;
- char acInfoText[] = "-- sec to play with header control";
- HEADER_Handle hHeader;
- hHeader = LISTVIEW_GetHeader(_hListView); //--------------(6)
- WM_SetFocus(_hListView);
-
- _ChangeInfoText("LISTVIEW_AddColumn"); //--------------(7)
- LISTVIEW_AddColumn(_hListView, 100, "EAN", GUI_TA_CENTER);
- GUI_Delay(SPEED / 2);
- LISTVIEW_AddColumn(_hListView, 50, "Order #\\0x0", GUI_TA_CENTER);
- GUI_Delay(SPEED / 2);
- LISTVIEW_AddColumn(_hListView, 100, "Description", GUI_TA_CENTER);
- GUI_Delay(SPEED / 2);
-
- _ChangeInfoText("SCROLLBAR_CreateAttached"); //--------------(8)
- SCROLLBAR_CreateAttached(_hListView, SCROLLBAR_CF_VERTICAL);
- GUI_Delay(SPEED / 2);
-
- _ChangeInfoText("LISTVIEW_AddRow"); //--------------(9)
- for (i = 0; i < GUI_COUNTOF(_aTable_1); i++) {
- LISTVIEW_AddRow(_hListView, _aTable_1[i]);
- GUI_Delay(SPEED / 3);
- }
-
- _ChangeInfoText("LISTVIEW_IncSel"); //--------------(10)
- for (i = 0; i < LISTVIEW_GetNumRows(_hListView); i++) {
- LISTVIEW_IncSel(_hListView);
- GUI_Delay(SPEED / 4);
- }
- GUI_Delay(SPEED / 4);
-
- _ChangeInfoText("LISTVIEW_DecSel"); //--------------(11)
- for (i = 0; i < LISTVIEW_GetNumRows(_hListView); i++) {
- LISTVIEW_DecSel(_hListView);
- GUI_Delay(SPEED / 4);
- }
- GUI_Delay(SPEED / 4);
-
- _ChangeInfoText("LISTVIEW_SetTextAlign"); //--------------(12)
- LISTVIEW_SetTextAlign(_hListView, 0, GUI_TA_RIGHT);
- GUI_Delay((int)(SPEED * 0.7))
- ;
- _ChangeInfoText("LISTVIEW_SetColumnWidth"); //--------------(13)
- LISTVIEW_SetColumnWidth(_hListView, 1, 70);
- GUI_Delay((int)(SPEED * 0.7));
-
- _ChangeInfoText("HEADER_SetTextColor"); //--------------(14)
- HEADER_SetTextColor(hHeader, GUI_BLUE);
- GUI_Delay((int)(SPEED * 0.7));
-
- _ChangeInfoText("LISTVIEW_SetItemText"); //--------------(15)
- for (i = 0; i < GUI_COUNTOF(_aTable_2); i++) {
- for (j = 0; j < GUI_COUNTOF(_aTable_2[i]); j++) {
- LISTVIEW_SetItemText(_hListView, j, i, _aTable_2[i][j]);
- }
- }
- GUI_Delay(SPEED / 2);
-
- _ChangeInfoText("LISTVIEW_SetBkColor"); //--------------(16)
- LISTVIEW_SetBkColor(_hListView, 0, GUI_YELLOW);
- GUI_Delay((int)(SPEED * 0.7));
-
- _ChangeInfoText("LISTVIEW_SetGridVis"); //--------------(17)
- LISTVIEW_SetGridVis(_hListView, 1);
- GUI_Delay((int)(SPEED * 0.7));
-
- _ChangeInfoText("HEADER_SetHeight"); //--------------(18)
- HEADER_SetHeight(hHeader, 30);
- GUI_Delay((int)(SPEED * 0.7));
-
- _ChangeInfoText("HEADER_SetBitmapEx"); //--------------(19)
- HEADER_SetBitmap(hHeader, 0, &bmBarCode);
- GUI_Delay((int)(SPEED * 0.7));
-
- while ((Key != GUI_KEY_ENTER) && (Cnt > 0)) { //--------------(20)
- acInfoText[0] = '0' + (Cnt / 10);
- acInfoText[1] = '0' + (Cnt-- % 10);
- _ChangeInfoText(acInfoText);
- GUI_Delay(1000);
- Key = GUI_GetKey();
- }
- }
-
- /*********************************************************************
- *
- * _DemoListView
- */
- static void _DemoListView(void) {
- _ChangeMainText("LISTVIEW control without parent");
- _ChangeInfoText("LISTVIEW_Create");
- _hListView = LISTVIEW_Create(10, 80, 300, 89, 0, 1234, WM_CF_SHOW, 0); //--------------(21)
- GUI_Delay(SPEED / 2);
- _Demo();
- _ChangeInfoText("WM_DeleteWindow");
- WM_DeleteWindow(_hListView); //--------------(22)
- GUI_Delay(SPEED);
- }
-
- /*********************************************************************
- *
- * Public code
- *
- **********************************************************************
- */
- /*********************************************************************
- *
- * MainTask
- */
- void MainTask(void) {
- GUI_Init();
- //
- // Check if recommended memory for the sample is available
- //
- if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
- GUI_ErrorOut("Not enough memory available.");
- return;
- }
- WM_EnableMemdev(WM_HBKWIN);
- WM_SetCreateFlags(WM_CF_MEMDEV);
- WM_SetCallback(WM_HBKWIN, _cbBkWindow);
- GUI_CURSOR_Show();
- while(1) {
- _DemoListView();
- }
- }
复制代码 1. 此函数用于桌面窗口文本的更新,具体实现是通过函数WM_SendMessage给桌面窗口发自定义消息MSG_CHANGE_MAIN_TEXT,然后再调用函数WM_InvalidateWindow将桌面窗口无效,从而会触发窗口管理器去执行MSG_CHANGE_MAIN_TEXT消息,但不是调用了函数WM_InvalidateWindow后立即去执行的,而是在调用了函数GUI_Delay,GUI_Exec或者WM_Exec后才去执行的。
2. 同第1条解释中的函数一样,也是用桌面窗口文本的更新。
3. 桌面窗口的回调函数。
4. 用于处理函数_ChangeMainText发来的自定义消息MSG_CHANGE_MAIN_TEXT,将要显示的文本复制到缓冲_acMainText里面,然后调用函数WM_InvalidateWindow会触发桌面窗口去执行重绘消息WM_PIANT,在此消息里面绘制缓冲_acMainText里面的文本。
5. 用于处理函数_ChangeInfoText发来的自定义消息MSG_CHANGE_INFO_TEXT,将要显示的文本复制到缓冲_acInfoText里面,然后调用函数WM_InvalidateWindow会触发桌面窗口去执行重绘消息WM_PIANT,在此消息里面绘制缓冲_acMainText里面的文本。
6. 通过函数LISTVIEW_GetHeader获得列表控件中header控件的句柄。
7. 这一小段程序演示函数LISTVIEW_AddColumn给列表控件添加列。
8. 这一小段程序演示函数SCROLLBAR_CreateAttached给列表控件添加垂直滚动条。
9. 这一小段程序演示函数LISTVIEW_AddRow给列表控件添加行。
10. 这一小段程序演示函数LISTVIEW_IncSel递增选取行。
11. 这一小段程序演示函数LISTVIEW_DecSel递减选取行。
12. 这一小段程序演示函数LISTVIEW_SetTextAlign设置列表控件的文本对齐(是指各个选项中的文本,而不是header控件上显示的文本)。
13. 这一小段程序演示函数LISTVIEW_SetColumnWidth设置列的宽度。
14. 这一小段程序演示函数HEADER_SetTextColor设置列表控件上header控件的文本显示颜色。
15. 这一小段程序演示函数LISTVIEW_SetItemText设置列表控件中各个选项显示的文本。
16. 这一小段程序演示函数LISTVIEW_SetBkColor设置列表控件的背景色。
17. 这一小段程序演示函数LISTVIEW_SetGridVis设置列表控件的网格是否可见。
18. 这一小段程序演示函数HEADER_SetHeight设置列表控件上header控件的高度。
19. 这一小段程序演示函数HEADER_SetBitmapEx设置列表控件上header控件的位图显示。
20. 这一小段程序比较巧妙,设置15s的延迟供用户操作列表控件,如果在15s期间,用户通过键盘按下了回车键,将直接退出演示,如果未按下回车键,15s后再退出。
21. 通过函数创建LISTVIEW_Create创建列表控件。
22. 通过函数WM_DeleteWindow删除列表控件。
实际显示效果如下: |
|