找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 270|回复: 1

Windows界面编程第八篇 listbox彩色显示隔行变色

[复制链接]

52

主题

62

回帖

0

积分

版主

积分
0
发表于 2013-10-9 21:04:45 | 显示全部楼层 |阅读模式
[attachment=1132]

要设置listbox的颜色及让listbox隔行变色主要是通过处理WM_DRAWITEM消息,这个消息在MSDN的解释如下:
The WM_DRAWITEM message is sent to the parent window of an owner-drawn button, combo box, list box, or menu when a visual aspect of the button, combo box, list box, or menu has changed.
通过这个消息最主要可以得到一个DRAWITEMSTRUCT类型的结构体。这个结构体的原型如下:
//By MoreWindows-(http://blog.csdn.net/MoreWindows)
typedef struct tagDRAWITEMSTRUCT {
    UINTCtlType;
    UINTCtlID;
    UINTitemID;
    UINTitemAction;
    UINTitemState;
    HWNDhwndItem;
    HDChDC;
    RECT rcItem;
   ULONG_PTR itemData;
} DRAWITEMSTRUCT;
这个结构体比较复杂,功能也很强大。全部记住它的功能是不必要的。因此直接查看下如何设置listbox的颜色,如何让listbox隔行变色这两个功能是如何完成的即可。
  1. //Windows界面编程第八篇 listbox彩色显示隔行变色
  2. //http://blog.csdn.net/morewindows/article/details/8656061
  3. //By MoreWindows-(http://blog.csdn.net/MoreWindows)
  4. #include "stdafx.h"
  5. #include "resource.h"
  6. BOOL CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  7. //使窗口居中
  8. void CenterWindow(HWND hwnd);
  9. int APIENTRY WinMain(HINSTANCE hInstance,
  10.                      HINSTANCE hPrevInstance,
  11.                      LPSTR     lpCmdLine,
  12.                      int       nCmdShow)
  13. {
  14.      // TODO: Place code here.
  15.     DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc);
  16.     return 0;
  17. }
  18. const int MAX_NUMBER = 8;
  19. const int MAX_STRING_LEN = 50;
  20. const char STR_TEXT[MAX_NUMBER][MAX_STRING_LEN] = {
  21.     "MoreWindows",
  22.     "Master",
  23.     "blog.csdn.net/MoreWindows",
  24.     "weibo.com/MoreWindows",
  25.     "Windows界面编程",
  26.     "秒杀多线程面试题系列",
  27.     "Windows界面编程第八篇",
  28.     "listbox彩色显示隔行变色"
  29. };
  30. BOOL CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  31. {
  32.     static HWND s_hLBColorListBox;
  33.     switch (message)
  34.     {
  35.     case WM_INITDIALOG:
  36.         {
  37.             CenterWindow(hDlg);
  38.             s_hLBColorListBox = GetDlgItem(hDlg, IDC_LB_COLORLISTBOX);
  39.             //添加数据
  40.             for (int i = 0; i < MAX_NUMBER; i++)
  41.                 SendMessage(s_hLBColorListBox, LB_ADDSTRING, 0, (LPARAM)STR_TEXT[i]);
  42.         }
  43.         return FALSE;
  44.         
  45.         
  46.     case WM_COMMAND:
  47.         switch (LOWORD(wParam))
  48.         {
  49.         case IDOK:
  50.         case IDCANCEL:
  51.             EndDialog(hDlg, FALSE);
  52.             return TRUE;
  53.         case IDC_LB_COLORLISTBOX:  //listbox control ID
  54.             switch (HIWORD(wParam))
  55.             {
  56.             case LBN_DBLCLK:   //双击事件
  57.                 {   
  58.                     char szBuffer[MAX_STRING_LEN];
  59.                     int nCurSel = SendMessage(s_hLBColorListBox, LB_GETCURSEL, 0, 0);
  60.                     SendMessage(s_hLBColorListBox, LB_GETTEXT, (WPARAM)nCurSel, (LPARAM)szBuffer);
  61.                     MessageBox(NULL, szBuffer, "listbox item", MB_OK);
  62.                 }
  63.                 return TRUE;
  64.             }
  65.             break;
  66.         }
  67.         break;
  68.     case WM_DRAWITEM:
  69.         {
  70.             LPDRAWITEMSTRUCT lpDrawItem = (LPDRAWITEMSTRUCT)lParam;
  71.             if (lpDrawItem->CtlType == ODT_LISTBOX || lpDrawItem->CtlID == IDC_LB_COLORLISTBOX)
  72.             {
  73.                 if (lpDrawItem->itemID == -1)
  74.                     break;
  75.                
  76.                 char szItemString[MAX_STRING_LEN];
  77.                 int nItemStringLen;
  78.                 SendMessage(s_hLBColorListBox, LB_GETTEXT, (WPARAM)lpDrawItem->itemID, (LPARAM)szItemString);
  79.                 nItemStringLen = strlen(szItemString);
  80.                 // 设置选中一行时,这一行加边框,反色
  81.                 if ((lpDrawItem->itemState & ODS_SELECTED) &&   
  82.                     (lpDrawItem->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
  83.                 {
  84.                     DrawFocusRect(lpDrawItem->hDC, &lpDrawItem->rcItem);
  85.                     InvertRect(lpDrawItem->hDC, &lpDrawItem->rcItem);
  86.                 }
  87.                 else if (!(lpDrawItem->itemState & ODS_SELECTED) &&
  88.                     (lpDrawItem->itemAction & ODA_SELECT))
  89.                 {
  90.                     DrawFocusRect(lpDrawItem->hDC, &lpDrawItem->rcItem);
  91.                     InvertRect(lpDrawItem->hDC, &lpDrawItem->rcItem);
  92.                 }
  93.                
  94.                 // 隔行变色
  95.                 if (lpDrawItem->itemID % 2 == 0)
  96.                     SetTextColor(lpDrawItem->hDC, RGB(25, 125, 0));
  97.                 else
  98.                     SetTextColor(lpDrawItem->hDC, RGB(0, 25, 125));
  99.                
  100.                 // 文字背景色
  101.                 if (lpDrawItem->itemState & ODS_SELECTED)
  102.                 {
  103.                     SetBkMode(lpDrawItem->hDC, TRANSPARENT);
  104.                     SetTextColor(lpDrawItem->hDC, RGB(255, 255, 255));
  105.                 }
  106.                 else
  107.                 {
  108.                     SetBkMode(lpDrawItem->hDC, OPAQUE);
  109.                     SetBkColor(lpDrawItem->hDC, GetSysColor(COLOR_WINDOW));
  110.                 }
  111.                
  112.                 //输出文字
  113.                 DrawText(lpDrawItem->hDC, szItemString, nItemStringLen, &lpDrawItem->rcItem, DT_LEFT | DT_SINGLELINE);
  114.                 return TRUE;
  115.             }   
  116.         }
  117.         break;
  118.     }
  119.     return FALSE;
  120. }
  121. void CenterWindow(HWND hwnd)
  122. {
  123.     RECT   rcDlg;
  124.     int    nDlgWidth, nDlgHight;
  125.     int    nScreenWidth, nScreenHight;
  126.    
  127.     nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  128.     nScreenHight = GetSystemMetrics(SM_CYSCREEN);
  129.    
  130.     GetWindowRect(hwnd, &rcDlg);
  131.     nDlgWidth = rcDlg.right - rcDlg.left;
  132.     nDlgHight = rcDlg.bottom - rcDlg.top;
  133.    
  134.     MoveWindow(hwnd, (nScreenWidth  - nDlgWidth) / 2, (nScreenHight - nDlgHight) / 2, nDlgWidth, nDlgHight, FALSE);
  135. }
复制代码
转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8656061


源码下载
[attachment=1134]




1

主题

23

回帖

0

积分

新手上路

积分
0
发表于 2013-10-15 09:19:03 | 显示全部楼层
:                                 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-1 15:15 , Processed in 0.069305 second(s), 21 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表