i'm not being succefull on drawing my own listbox, heres the code:
LRESULT CALLBACK ListBoxProcedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DRAWITEM:
LPDRAWITEMSTRUCT Item;
Item = (LPDRAWITEMSTRUCT)lParam;
if (Item->itemState == ODS_SELECTED)
{
FillRect(Item->hDC, &Item->rcItem, CreateSolidBrush(0));
SetTextColor(Item->hDC, 0x0000FF);
}
else
{
SetBkColor(Item->hDC, 0);
FillRect(Item->hDC, &Item->rcItem, (HBRUSH)GetStockObject(BLACK_BRUSH));
SetTextColor(Item->hDC, 0xFFFFFF);
}
LPSTR lpBuff;
SendMessageA(Item->hwndItem , LB_GETTEXT, Item->itemID, (LPARAM)lpBuff);
TextOutA(Item->hDC, Item->rcItem.left, Item->rcItem.top, (lpBuff), strlen(lpBuff)-1);
if (Item->itemState & ODS_FOCUS)
{
DrawFocusRect(Item->hDC, &Item->rcItem);
}
return true;
break;
case WM_MEASUREITEM:
break;
default:
DefWindowProcA(hwnd, msg, wParam, lParam);
}
return 0;
}
I create the ListBox like this:
lbLogs = CreateWindowExA(0, "LISTBOX", "", WS_VISIBLE + WS_CHILD + WS_BORDER + LBS_HASSTRINGS + LBS_NOINTEGRALHEIGHT + WS_TABSTOP, 15, 115, 515, 180, hwnd, (HMENU)1005, hInstance, NULL);
//-----------------------------
SetWindowLong(lbLogs, GWL_WNDPROC, (LONG)&ListBoxProcedure);
Could someone explain to me whats wrong in it? I'm trying to make a listbox that has black bg and red text and when an item gets selected it's text transforms to white.But the listbox just don't add nothing.
You are creating a listbox, then installing your own WNDPROC to process messages sent to the listbox. However, an owner-draw listbox sends WM_MEASUREITEM and WM_DRAWITEM messages to its owner, so you need to handle those messages in the parent window's WNDPROC, not the listbox WNDPROC.
Also, even if subclassing was the correct approach (which it is not in this case), your subclass WNDPROC should pass unprocessed messages to the original listbox WNDPROC, not DefWindowProc(). Bypassing the original listbox WNDPROC will most likely cause trouble.