March 28, 2024, 02:50:26 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Ownerdrawn listbox question

Started by Haim, September 22, 2008, 06:31:34 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Haim

Hello,
I am trying to figure out how to program an owner drawn listbox. 
I need some help on the implementationof  the OnDrawitem method. Is it be a method of the listbox (meaning it should be subclassed) or   is it be a method of the parent window/dialog.
I tried both but did not succeed to get items drawn. It seems that OnDrawitem is never called. (I did add the ALBS_OWNERDRAWFIXED style).

Thanks for any help on this.

Haim



sapero

Haim, you need to subclass the window.
#include "windows.inc"

#define IDC_LISTBOX 1

class CDlg:CDialog
{
declare virtual OnInitDialog();
declare virtual OnClose();
declare virtual OnDrawItem(int nID, DRAWITEMSTRUCT lpdis),int;
WNDPROC m_wndproc;
}

sub main()
{
CDlg d1;
d1.Create(0,0,300,202,0x80CA0880,0,"ALBS_OWNERDRAWFIXED",0);
d1.AddControl(CTLISTBOX,"",39,14,230,153,0x50800140|ALBS_OWNERDRAWFIXED,0x200,IDC_LISTBOX);

d1.DoModal();
return 0;
}

CDlg::OnClose()
{
CloseDialog(1);
}

CDlg::OnInitDialog()
{
// subclass the dialog
SetProp(m_hwnd, "THIS", this);
m_wndproc = SetWindowLong(m_hwnd, GWL_WNDPROC, &SubclassedDialogProc);

CListBox *lb = GetControl(IDC_LISTBOX);
lb->AddString("first item");
lb->AddString("second item");
lb->AddString("third item");
lb->AddString("click here");
}

CDlg::OnDrawItem(int nID, DRAWITEMSTRUCT lpdis),int
{
string text;
CListBox *lb  = GetControl(nID);
if ((lpdis.CtlType == ODT_LISTBOX) && (lpdis.itemID != -1))
{
HGDIOBJ brush = GetSysColorBrush(IIF(lpdis.itemState & ODS_SELECTED, COLOR_HIGHLIGHT, COLOR_WINDOW));
FillRect(lpdis.hDC, &lpdis.rcItem, brush);

HGDIOBJ pen = SelectObject(lpdis.hDC, CreatePen(PS_SOLID, 1, 0x000000));
brush = SelectObject(lpdis.hDC, GetStockObject(NULL_BRUSH));

::Ellipse(lpdis.hDC, lpdis.rcItem.left, lpdis.rcItem.top, lpdis.rcItem.right, lpdis.rcItem.bottom);

SetBkMode(lpdis.hDC, TRANSPARENT);
text = lb->GetItemText(lpdis.itemID);
DrawText(lpdis.hDC, text, len(text), &lpdis.rcItem, DT_CENTER | DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX);

DeleteObject(SelectObject(lpdis.hDC, pen));
SelectObject(lpdis.hDC, brush);
return true;
}
return false;
}

sub SubclassedDialogProc(HWND hwnd, UINT uMsg,WPARAM wParam,LPARAM lParam),int
{
CDlg *dialog = GetProp(hwnd, "THIS");
if (uMsg == WM_DRAWITEM)
{
return dialog->OnDrawItem(wParam, *(DRAWITEMSTRUCT)lParam);
}
return CallWindowProc(dialog->m_wndproc, hwnd, uMsg, wParam, lParam);
}

Haim