所有者绘制列表框总是返回(LPDRAWITEMSTRUCT) - > itemID = -1

时间:2016-08-22 06:14:49

标签: c++ winapi listbox ownerdrawn

我创建了一个所有者绘制列表框。你知道为什么在 WM_DRAWITEM 消息中,我只收到(LPDRAWITEMSTRUCT)lParam->itemID = -1吗?

我使用VS 2010 C ++ Express和Win 7进行编码。

创建列表框并设置滚动信息:

lbHWND = CreateWindowExW(NULL, WC_LISTBOX, NULL,
    WS_CHILD | WS_BORDER | WS_VISIBLE | LBS_NODATA | 
    LBS_OWNERDRAWFIXED | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT,
    0, 0, 400, 400, tkHWND, (HMENU)IDC_LISTBOX_ENTRY, hInstance, 0);

SCROLLINFO lbSi = { 0 };
lbSi.cbSize = sizeof(SCROLLINFO);
lbSi.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
lbSi.nMin = 0;
lbSi.nMax = 1000;
lbSi.nPage = 20;
lbSi.nPos = 0;
SetScrollInfo(lbHWND, SB_VERT, &lbSi, TRUE);

Winproc传

LRESULT CALLBACK WndProc(HWND phwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_MEASUREITEM:
        {
            MEASUREITEMSTRUCT*  lpmis = (LPMEASUREITEMSTRUCT)lParam;
            switch (lpmis->CtlID)
            {
                case IDC_LISTBOX_ENTRY:
                    lpmis->itemHeight = 20;
                    break;
                default:
                    break;
            }
            return TRUE;
        }
        case WM_DRAWITEM:
        {
            DRAWITEMSTRUCT* lpdis = (LPDRAWITEMSTRUCT)lParam;

            // Check lpdis->itemID, for test purpose only
            FILE *f;
            if (!_wfopen_s(&f, L"E:\\lb.txt", L"ab"))
            {
                fprintf(f,"%d\r\n", lpdis->itemID);
                fclose(f);
            }
            // lpdis->itemID always = -1
            return TRUE;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了答案:对于拥有LBS_NODATA样式的所有者绘制列表框,我必须告诉列表框它拥有的项目数。

SendMessage(lbHWND, LB_RESETCONTENT, 0, 0);    // Remove all items, if have any
SendMessage(lbHWND, LB_SETCOUNT, 1000, 0);     // Set number of items (here is 1000)