NM_CUSOMDRAW!

I'm trying to change color of single row in ListView but I just can't figure out how to do this... here's my code:
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#define _WIN32_IE 0x0400

#include <windows.h>
#include <commctrl.h>
#include <windowsx.h>

#include "listview.h"

ListView lv;  



LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[ ] = "WinApp";
HINSTANCE hi;
int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    hi=hThisInstance;

    HWND hwnd;
    MSG messages;
    WNDCLASSEX wincl;

    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof (WNDCLASSEX);

    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    if (!RegisterClassEx (&wincl))
        return 0;


    hwnd = CreateWindowEx (0, szClassName, "Langas",  WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
           CW_USEDEFAULT, 360, 360, HWND_DESKTOP,NULL, hThisInstance, NULL );
    ShowWindow (hwnd, nCmdShow);
    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
    return messages.wParam;
}



LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_CREATE:
        {
            InitCommonControls();
            RECT rc={20,20,320,320};

            lv.Create(hwnd,hi,WS_TABSTOP | WS_BORDER | LVS_SHOWSELALWAYS | LVS_SINGLESEL | LVS_REPORT | LVS_SORTASCENDING,rc,1);
            lv.AddColumn("1",100);
            lv.AddColumn("2",100);

            lv.InsertItem("stulpelis1");
            lv.SetItem("stulpelis2",0,1);

            lv.AddStyle(LVS_EX_FULLROWSELECT);
        }
            break;


        case WM_NOTIFY:
        {
          LPNMLISTVIEW  pnm    = (LPNMLISTVIEW)lParam;

           switch (pnm->hdr.code)
             {
               case NM_CUSTOMDRAW:
                  LPNMLVCUSTOMDRAW  lplvcd; lplvcd = (LPNMLVCUSTOMDRAW)lParam;

                 switch(lplvcd->nmcd.dwDrawStage)
                   {
                      case CDDS_ITEMPREPAINT:
                     lplvcd->clrText=RGB(255,255,200); //change color of item here?
                     return CDRF_NEWFONT; // should I return something
                     break;

                   }
               break;

              }
        }
            break;
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
    return 0;
}






listview.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

#ifndef LISTVIEW_H
#define LISTVIEW_H

#include <commctrl.h>

class ListView
{
HWND hwnd;
int id;
int columns;
unsigned int style;
public:

ListView(){ id=0; columns=0; style=0; hwnd=0;}

// Create a ListView
HWND Create(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,const RECT& rc,const int id);
// Destroiy ListView
void Destroy();

// Add a column at specified pos
int AddColumn(const char *str,int width,int pos);
// Add a column at the end
int AddColumn(const char *str,int width);

// Inserts a new item
int InsertItem(const char *str);
// Set item data
int SetItem(const char*str,int row,int col);

// Add style
unsigned int AddStyle(unsigned int _style);
// Remove style
unsigned int RemoveStyle(unsigned int _style);
};

unsigned int ListView::AddStyle(unsigned int _style)
{
style|=_style;
ListView_SetExtendedListViewStyle(hwnd,style);
return style;
}


unsigned int ListView::RemoveStyle(unsigned int _style)
{
style&=(~_style);
ListView_SetExtendedListViewStyle(hwnd,style);
return style;
}


int ListView::SetItem(const char*str,int row,int col)
{
LVITEM tmp={LVIF_TEXT, row,col, 0,0, (char*)str,0, 0,0 };
ListView_SetItem(hwnd, &tmp);

}

int ListView::InsertItem(const char *str)
{
LVITEM tmp={LVIF_TEXT, 0,0, 0,0, (char*)str,0, 0,0 };
ListView_InsertItem(hwnd, &tmp);

}


int ListView::AddColumn(const char *str,int width,int pos)
{
int ColumnMask = LVCF_TEXT|LVCF_FMT|LVCF_SUBITEM|LVCF_WIDTH;
LVCOLUMN  lvc={ ColumnMask, 0, width, (char*)str, 0, 0 };

ListView_InsertColumn(hwnd, pos, &lvc);
return pos;

}

int ListView::AddColumn(const char *str,int width)
{

int ColumnMask = LVCF_TEXT|LVCF_FMT|LVCF_SUBITEM|LVCF_WIDTH;
LVCOLUMN  lvc={ ColumnMask, 0, width, (char*)str, 0, 0 };

ListView_InsertColumn(hwnd, ++columns, &lvc);
return columns;
}



HWND ListView::Create(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,const RECT& rc,const int id)
{
dwStyle|=WS_CHILD|WS_VISIBLE;
hwnd=CreateWindowEx(0,                  //extended styles
                      WC_LISTVIEW,        //control 'class' name
                      0,                  //control caption
                      dwStyle,            //wnd style
                      rc.left,            //position: left
                      rc.top,             //position: top
                      rc.right,           //width
                      rc.bottom,          //height
                      hParent,            //parent window handle
                      //control's ID
                      reinterpret_cast<HMENU>(static_cast<INT_PTR>(id)),
                      hInst,              //instance
                      0);                 //user defined info
return hwnd;
}

void ListView::Destroy()
{
DestroyWindow(hwnd);
}


#endif

All my problems start at line 85.
I just can't find WinApi code that shows how to use NM_CIUSTOMDRAW properly ( I googled for it and everything is coded in MFC or .net )

I found something here:
http://msdn.microsoft.com/en-us/library/bb761817(v=VS.85).aspx#CustomDraw_ChangingFontsAndColors
but that helped me just a little.

Any ideas how can I make this code work?

Thanks for help.
Last edited on
Topic archived. No new replies allowed.