Bir Windows API programında kullanılan ListView kontrolünün başlığını ve item satırlarını kendi belirlediğimiz görünümlere göre çizmek için Custom Draw yöntemini kullanabiliriz. Bu amaçla, kontrolün içinde bulunduğu ana pencereye WM_NOTIFY mesajı yoluyla, kontrol tarafından gönderilen NM_CUSTOMDRAW bildirim mesajına işlem yapılır.
Programda yer alacak main.c, resource.rc ve manifest.xml dosyalarının içeriği aşağıdaki şekilde olacaktır:
main.c
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
// commctrl.h içindeki bazı verilerin kullanımı için gerekli
#define _WIN32_WINNT 0x0601
#define _WIN32_IE 0x0501
#define IDC_LISTVIEWCUSTOM 1001
#define RENKSUTUN01 RGB(240,230,140)
#define RENKSUTUN02 RGB(229,223,107)
#define RENKFONT RGB(50,50,50)
#define RENKSATIR01 RGB(255,255,255)
#define RENKSATIR02 RGB(248,248,248)
#define RENKSATIR03 RGB(230, 240, 255)
#define RENKSATIR04 RGB(224, 234, 250)
#define SELECTBG RGB(190,240,60)
#define SELECTFONT RGB(255,255,255)
#define RENKBORDER RGB(255,0,0)
#include <tchar.h>
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
HWND ListViewCustom;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ListViewProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
void FillListView(HWND hwnd);
int BG_ListView_Header (HWND hwnd, LPNMCUSTOMDRAW lpcd);
int BG_ListView_ItemLine (HWND hwnd, LPNMLVCUSTOMDRAW lplvcd);
/* Make the class name into a global variable */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Custom draw Listview "), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
435, /* The programs width */
280, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
{
ListViewCustom = CreateWindowEx(WS_EX_CLIENTEDGE , WC_LISTVIEW, "",
WS_CHILD | LVS_REPORT | WS_VISIBLE,
10, 10, 400, 220, hwnd,
(HMENU) IDC_LISTVIEWCUSTOM, NULL, NULL);
ListView_SetExtendedListViewStyle(ListViewCustom, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
SetWindowSubclass(ListViewCustom, ListViewProc, 0, 0);
FillListView(ListViewCustom);
break;
}
case WM_NOTIFY:
{
switch (((LPNMHDR)lParam)->code) {
case NM_CUSTOMDRAW:
{
HWND hLV = ((LPNMHDR)lParam)->hwndFrom;
// Listview satır (item) çizimi
if(hLV == ListViewCustom) {
return BG_ListView_ItemLine (hLV, (LPNMLVCUSTOMDRAW)lParam);
}
break;
}
}
break;
}
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK ListViewProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
switch (message) {
case WM_NOTIFY:
{
switch (((LPNMHDR)lParam)->code) {
case NM_CUSTOMDRAW:
{
HWND hLV = ((LPNMHDR)lParam)->hwndFrom;
return BG_ListView_Header(hLV, (LPNMCUSTOMDRAW)lParam);
break;
}
}
break;
}
}
return DefSubclassProc(hwnd, message, wParam, lParam);
}
int BG_ListView_Header(HWND hwnd, LPNMCUSTOMDRAW lpcd)
{
HBRUSH hBrush;
char cdizi[30];
HDITEM hdi = { 0 };
hdi.mask = HDI_TEXT;
hdi.pszText = cdizi;
hdi.cchTextMax = 30;
switch (lpcd->dwDrawStage) {
case CDDS_PREPAINT :
return CDRF_NOTIFYITEMDRAW;
case CDDS_ITEMPREPAINT:
SetBkMode(lpcd->hdc, TRANSPARENT);
if (lpcd->dwItemSpec%2) hBrush = CreateSolidBrush(RENKSUTUN02);
else hBrush = CreateSolidBrush(RENKSUTUN01);
SetTextColor(lpcd->hdc, RENKFONT);
SelectObject(lpcd->hdc, hBrush);
FillRect(lpcd->hdc, &lpcd->rc, NULL);
Header_GetItem(hwnd, lpcd->dwItemSpec, &hdi);
DrawText(lpcd->hdc, cdizi, strlen(cdizi), &lpcd->rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
DeleteObject(hBrush);
return CDRF_SKIPDEFAULT;
default:
return CDRF_DODEFAULT;
}
}
int BG_ListView_ItemLine(HWND hwnd, LPNMLVCUSTOMDRAW lplvcd)
{
LVITEM lvi;
HBRUSH hBrushBack = NULL;
TCHAR wcBuffer[256] = {0};
int id;
HDITEM hdi = { 0 };
hdi.mask = HDI_FORMAT;
switch (lplvcd->nmcd.dwDrawStage) {
case CDDS_PREPAINT :
return CDRF_NOTIFYITEMDRAW;
case CDDS_ITEMPREPAINT:
// Tüm item satırına (subitem'lar dahil) aynı işlemi yapmak için
// Seçili satır için
if(ListView_GetItemState(hwnd, lplvcd->nmcd.dwItemSpec, LVIS_SELECTED) & LVIS_SELECTED) {
SetBkMode(lplvcd->nmcd.hdc, TRANSPARENT);
for (id=0;id<Header_GetItemCount(ListView_GetHeader(hwnd)); id++) {
lvi.iItem = lplvcd->nmcd.dwItemSpec;
lvi.iSubItem = id;
lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
lvi.pszText = wcBuffer;
lvi.cchTextMax = sizeof(wcBuffer) - 1;
if(!ListView_GetItem(hwnd, &lvi)) break;
hBrushBack = CreateSolidBrush(SELECTBG);
SetTextColor(lplvcd->nmcd.hdc, SELECTFONT);
ListView_GetSubItemRect(hwnd, lplvcd->nmcd.dwItemSpec, id, LVIR_BOUNDS, &lplvcd->nmcd.rc);
FillRect(lplvcd->nmcd.hdc, &lplvcd->nmcd.rc, hBrushBack);
DeleteObject(hBrushBack);
Header_GetItem(ListView_GetHeader(hwnd), id, &hdi);
lplvcd->nmcd.rc.left += 6;
lplvcd->nmcd.rc.top += 2;
lplvcd->nmcd.rc.right -= 6;
if (hdi.fmt & HDF_RIGHT) DrawText(lplvcd->nmcd.hdc, lvi.pszText, lstrlen(lvi.pszText), &lplvcd->nmcd.rc, DT_RIGHT|DT_VCENTER);
else if (hdi.fmt & HDF_CENTER) DrawText(lplvcd->nmcd.hdc, lvi.pszText, lstrlen(lvi.pszText), &lplvcd->nmcd.rc, DT_CENTER|DT_VCENTER);
else DrawText(lplvcd->nmcd.hdc, lvi.pszText, lstrlen(lvi.pszText), &lplvcd->nmcd.rc, DT_LEFT|DT_VCENTER);
}
return CDRF_SKIPDEFAULT; // Ön tanımlı işlemleri devre dışı bırakır.
}
lplvcd->clrText = RENKFONT;
return CDRF_NOTIFYSUBITEMDRAW; // Her bir subitem'a farklı işlem yapmak gerektiğinde
// return CDRF_NEWFONT; // Tüm item satırına aynı işlemi yapmak gerektiğinde
case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
{
// Sadece report mod için yukarıda CDRF_NOTIFYSUBITEMDRAW geri döndürülünce
// subitem işlemleri yapılır ve CDRF_NEWFONT döndürülür.
COLORREF crTextBk=0, crText=RENKFONT;
if (lplvcd->nmcd.dwItemSpec%2) { // satır indeks değeri
if (lplvcd->iSubItem%2) { // sütun index değeri
crTextBk = RENKSATIR02;
}
else {
crTextBk = RENKSATIR01;
}
}
else {
if (lplvcd->iSubItem%2) { // sütun index değeri
crTextBk = RENKSATIR04;
}
else {
crTextBk = RENKSATIR03;
}
}
lplvcd->clrTextBk = crTextBk;
lplvcd->clrText = crText;
return CDRF_DODEFAULT;
}
default:
return CDRF_DODEFAULT;
}
}
void FillListView(HWND hwnd)
{
LVCOLUMN lvc;
LVITEM lvI;
int idCol=4, idItem=10;
char cdizi[15];
int id1, id2;
// LVCOLUMN structure için ilk değer atama işlemlerini yapar.
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvc.cx = 98; // Sütun genişliği değeri
lvc.pszText = cdizi;
lvc.cchTextMax = sizeof(cdizi);
lvc.fmt = LVCFMT_LEFT; // Sütunları sol tarafa ayarlar
// Sütun ekleme işlemleri
for (id1=0; id1<idCol; id1++) {
lvc.iSubItem = id1;
sprintf(cdizi, "%s%d", "Column", id1+1);
lvc.pszText = cdizi;
// Sütun ekleme işlemi
ListView_InsertColumn(hwnd, id1, &lvc);
}
// LVITEM structure için ilk değer atama işlemlerini yapar.
lvI.mask = LVIF_TEXT | LVIF_STATE;
lvI.stateMask = 0;
lvI.iSubItem = 0;
lvI.state = 0;
// Satır ekleme işlemleri
for (id1=0; id1<idItem; id1++) {
lvI.iItem = id1;
sprintf(cdizi, "%s%d", "Item", id1+1);
lvI.pszText = cdizi;
// İlk sütun için öğe ekleme işlemleri
ListView_InsertItem(hwnd, &lvI);
// Diğer sütunlar için öğe ekleme işlemleri
for (id2=1; id2<idCol; id2++) {
sprintf(cdizi, "%s%d-%d", "SubItem", id1+1, id2+1);
ListView_SetItemText(hwnd, id1, id2, cdizi);
}
}
}
resource.rc
1 24 "manifest.xml"
manifest.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="CompanyName.ProductName.YourApplication" type="win32" />
<description>Your application description here.</description>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" />
</dependentAssembly>
</dependency>
</assembly>
Program derleyip çalıştırdığımızda aşağıdakine benzer bir ekran görüntüsü karşımıza gelecektir: