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

在CSDN论坛上看到很多询问如何设置listbox的颜色,如何让listbox隔行变色等等。因此专门写了一篇《 Windows界面编程第八篇 listbox彩色显示隔行变色 》来为大家解惑。

       首先看看下图(如果看不到,可以通过地址http://blog.csdn.net/morewindows/article/details/8656061访问)。

要设置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 structtagDRAWITEMSTRUCT {

   UINTCtlType;

   UINTCtlID;

   UINTitemID;

   UINTitemAction;

   UINTitemState;

   HWNDhwndItem;

   HDChDC;

   RECT rcItem;

   ULONG_PTRitemData;

}DRAWITEMSTRUCT;

这个结构体比较复杂,功能也很强大。全部记住它的功能是不必要的。因此直接查看下如何设置listbox的颜色,如何让listbox隔行变色这两个功能是如何完成的即可。完整代码+注释如下(可以直接到http://download.csdn.net/detail/morewindows/5128660(免积分)下载)。

  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.  
  7. BOOL CALLBACK DlgProc(HWND hDlg,UINT message, WPARAM wParam,LPARAM lParam); 
  8. //使窗口居中 
  9. void CenterWindow(HWND hwnd); 
  10.  
  11. int APIENTRY WinMain(HINSTANCE hInstance, 
  12.                      HINSTANCE hPrevInstance, 
  13.                     LPSTR     lpCmdLine, 
  14.                      int       nCmdShow) 
  15.     // TODO: Place code here. 
  16.     DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc); 
  17.     return 0; 
  18.  
  19. constint MAX_NUMBER = 8; 
  20. constint MAX_STRING_LEN = 50; 
  21. constchar STR_TEXT[MAX_NUMBER][MAX_STRING_LEN] = { 
  22.     "MoreWindows",  
  23.     "Master"
  24.     "blog.csdn.net/MoreWindows",  
  25.     "weibo.com/MoreWindows"
  26.     "Windows界面编程"
  27.     "秒杀多线程面试题系列"
  28.     "Windows界面编程第八篇"
  29.     "listbox彩色显示隔行变色" 
  30. }; 
  31. BOOL CALLBACK DlgProc(HWND hDlg,UINT message, WPARAM wParam,LPARAM lParam) 
  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.  
  54.         case IDC_LB_COLORLISTBOX:  //listbox control ID 
  55.             switch (HIWORD(wParam)) 
  56.             { 
  57.             case LBN_DBLCLK:   //双击事件 
  58.                 {    
  59.                     char szBuffer[MAX_STRING_LEN]; 
  60.                    int nCurSel = SendMessage(s_hLBColorListBox, LB_GETCURSEL, 0, 0); 
  61.                     SendMessage(s_hLBColorListBox, LB_GETTEXT, (WPARAM)nCurSel, (LPARAM)szBuffer); 
  62.                     MessageBox(NULL, szBuffer,"listbox item", MB_OK); 
  63.                 } 
  64.                 return TRUE; 
  65.             } 
  66.             break
  67.         } 
  68.         break
  69.  
  70.  
  71.     case WM_DRAWITEM: 
  72.         { 
  73.             LPDRAWITEMSTRUCT lpDrawItem = (LPDRAWITEMSTRUCT)lParam; 
  74.             if (lpDrawItem->CtlType == ODT_LISTBOX || lpDrawItem->CtlID == IDC_LB_COLORLISTBOX) 
  75.             { 
  76.                 if (lpDrawItem->itemID == -1) 
  77.                     break
  78.                  
  79.                 char szItemString[MAX_STRING_LEN]; 
  80.                 int nItemStringLen; 
  81.                 SendMessage(s_hLBColorListBox, LB_GETTEXT, (WPARAM)lpDrawItem->itemID, (LPARAM)szItemString); 
  82.                 nItemStringLen = strlen(szItemString); 
  83.  
  84.                 // 设置选中一行时,这一行加边框,反色 
  85.                 if ((lpDrawItem->itemState & ODS_SELECTED) &&    
  86.                     (lpDrawItem->itemAction & (ODA_SELECT | ODA_DRAWENTIRE))) 
  87.                 { 
  88.                     DrawFocusRect(lpDrawItem->hDC, &lpDrawItem->rcItem);  
  89.                     InvertRect(lpDrawItem->hDC, &lpDrawItem->rcItem); 
  90.                 } 
  91.                 else if (!(lpDrawItem->itemState & ODS_SELECTED) &&  
  92.                     (lpDrawItem->itemAction & ODA_SELECT)) 
  93.                 { 
  94.                     DrawFocusRect(lpDrawItem->hDC, &lpDrawItem->rcItem); 
  95.                     InvertRect(lpDrawItem->hDC, &lpDrawItem->rcItem); 
  96.                 } 
  97.                  
  98.                 // 隔行变色 
  99.                 if (lpDrawItem->itemID % 2 == 0) 
  100.                     SetTextColor(lpDrawItem->hDC, RGB(25, 125, 0)); 
  101.                 else 
  102.                     SetTextColor(lpDrawItem->hDC, RGB(0, 25, 125)); 
  103.                  
  104.                 // 文字背景色 
  105.                 if (lpDrawItem->itemState & ODS_SELECTED) 
  106.                 { 
  107.                     SetBkMode(lpDrawItem->hDC, TRANSPARENT); 
  108.                     SetTextColor(lpDrawItem->hDC, RGB(255, 255, 255)); 
  109.                 } 
  110.                 else  
  111.                 { 
  112.                     SetBkMode(lpDrawItem->hDC, OPAQUE); 
  113.                     SetBkColor(lpDrawItem->hDC, GetSysColor(COLOR_WINDOW)); 
  114.                 } 
  115.                  
  116.                 //输出文字 
  117.                 DrawText(lpDrawItem->hDC, szItemString, nItemStringLen, &lpDrawItem->rcItem, DT_LEFT | DT_SINGLELINE); 
  118.                 return TRUE; 
  119.             }    
  120.         } 
  121.         break
  122.     } 
  123.     return FALSE; 
  124.  
  125. void CenterWindow(HWND hwnd) 
  126.     RECT   rcDlg; 
  127.     int    nDlgWidth, nDlgHight; 
  128.     int    nScreenWidth, nScreenHight; 
  129.      
  130.     nScreenWidth = GetSystemMetrics(SM_CXSCREEN); 
  131.     nScreenHight = GetSystemMetrics(SM_CYSCREEN); 
  132.      
  133.     GetWindowRect(hwnd, &rcDlg); 
  134.     nDlgWidth = rcDlg.right - rcDlg.left; 
  135.     nDlgHight = rcDlg.bottom - rcDlg.top; 
  136.      
  137.     MoveWindow(hwnd, (nScreenWidth  - nDlgWidth) / 2, (nScreenHight - nDlgHight) / 2, nDlgWidth, nDlgHight, FALSE); 
//Windows界面编程第八篇 listbox彩色显示隔行变色
//http://blog.csdn.net/morewindows/article/details/8656061
//By MoreWindows-(http://blog.csdn.net/MoreWindows) 
#include "stdafx.h"
#include "resource.h"

BOOL CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
//使窗口居中
void CenterWindow(HWND hwnd);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc);
	return 0;
}

const int MAX_NUMBER = 8;
const int MAX_STRING_LEN = 50;
const char STR_TEXT[MAX_NUMBER][MAX_STRING_LEN] = {
	"MoreWindows", 
	"Master",
	"blog.csdn.net/MoreWindows", 
	"weibo.com/MoreWindows",
	"Windows界面编程",
	"秒杀多线程面试题系列",
	"Windows界面编程第八篇",
	"listbox彩色显示隔行变色"
};
BOOL CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	static HWND s_hLBColorListBox;
	switch (message)
	{
	case WM_INITDIALOG:
		{
			CenterWindow(hDlg);
			s_hLBColorListBox = GetDlgItem(hDlg, IDC_LB_COLORLISTBOX);
			//添加数据
			for (int i = 0; i < MAX_NUMBER; i++)
				SendMessage(s_hLBColorListBox, LB_ADDSTRING, 0, (LPARAM)STR_TEXT[i]);
		}
		return FALSE;
		
		
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDOK:
		case IDCANCEL:
			EndDialog(hDlg, FALSE);
			return TRUE;

		case IDC_LB_COLORLISTBOX:  //listbox control ID
			switch (HIWORD(wParam))
			{
			case LBN_DBLCLK:   //双击事件
				{	
					char szBuffer[MAX_STRING_LEN];
					int nCurSel = SendMessage(s_hLBColorListBox, LB_GETCURSEL, 0, 0);
					SendMessage(s_hLBColorListBox, LB_GETTEXT, (WPARAM)nCurSel, (LPARAM)szBuffer);
					MessageBox(NULL, szBuffer, "listbox item", MB_OK);
				}
				return TRUE;
			}
			break;
		}
		break;


	case WM_DRAWITEM:
		{
			LPDRAWITEMSTRUCT lpDrawItem = (LPDRAWITEMSTRUCT)lParam;
			if (lpDrawItem->CtlType == ODT_LISTBOX || lpDrawItem->CtlID == IDC_LB_COLORLISTBOX)
			{
				if (lpDrawItem->itemID == -1)
					break;
				
				char szItemString[MAX_STRING_LEN];
				int nItemStringLen;
				SendMessage(s_hLBColorListBox, LB_GETTEXT, (WPARAM)lpDrawItem->itemID, (LPARAM)szItemString);
				nItemStringLen = strlen(szItemString);

				// 设置选中一行时,这一行加边框,反色
				if ((lpDrawItem->itemState & ODS_SELECTED) &&   
					(lpDrawItem->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
				{
					DrawFocusRect(lpDrawItem->hDC, &lpDrawItem->rcItem); 
					InvertRect(lpDrawItem->hDC, &lpDrawItem->rcItem);
				}
				else if (!(lpDrawItem->itemState & ODS_SELECTED) && 
					(lpDrawItem->itemAction & ODA_SELECT))
				{
					DrawFocusRect(lpDrawItem->hDC, &lpDrawItem->rcItem);
					InvertRect(lpDrawItem->hDC, &lpDrawItem->rcItem);
				}
				
				// 隔行变色
				if (lpDrawItem->itemID % 2 == 0)
					SetTextColor(lpDrawItem->hDC, RGB(25, 125, 0));
				else
					SetTextColor(lpDrawItem->hDC, RGB(0, 25, 125));
				
				// 文字背景色
				if (lpDrawItem->itemState & ODS_SELECTED)
				{
					SetBkMode(lpDrawItem->hDC, TRANSPARENT);
					SetTextColor(lpDrawItem->hDC, RGB(255, 255, 255));
				}
				else 
				{
					SetBkMode(lpDrawItem->hDC, OPAQUE);
					SetBkColor(lpDrawItem->hDC, GetSysColor(COLOR_WINDOW));
				}
				
				//输出文字
				DrawText(lpDrawItem->hDC, szItemString, nItemStringLen, &lpDrawItem->rcItem, DT_LEFT | DT_SINGLELINE);
				return TRUE;
			}	
		}
		break;
	}
	return FALSE;
}

void CenterWindow(HWND hwnd)
{
	RECT   rcDlg;
	int    nDlgWidth, nDlgHight;
	int    nScreenWidth, nScreenHight;
	
	nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
	nScreenHight = GetSystemMetrics(SM_CYSCREEN);
	
	GetWindowRect(hwnd, &rcDlg);
	nDlgWidth = rcDlg.right - rcDlg.left;
	nDlgHight = rcDlg.bottom - rcDlg.top;
	
	MoveWindow(hwnd, (nScreenWidth  - nDlgWidth) / 2, (nScreenHight - nDlgHight) / 2, nDlgWidth, nDlgHight, FALSE);
}

下面列出全部目录,方便大家浏览:

1. 《Windows界面编程第一篇位图背景与位图画刷

2. 《Windows界面编程第二篇半透明窗体

3. 《Windows界面编程第三篇异形窗体普通版

4. 《Windows界面编程第四篇异形窗体高富帅版

5. 《Windows界面编程第五篇静态控件背景透明化

6.《Windows界面编程第六篇 动画启动效果(动画效果显示及隐藏窗口)

7.《Windows界面编程第七篇 文件拖拽(文件拖放)

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

本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5128660

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8656061

欢迎关注微博:http://weibo.com/MoreWindows

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值