Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在WM_DRAWITEM中绘制多列ListView_C++_Listview_Winapi_Win32gui - Fatal编程技术网

C++ 如何在WM_DRAWITEM中绘制多列ListView

C++ 如何在WM_DRAWITEM中绘制多列ListView,c++,listview,winapi,win32gui,C++,Listview,Winapi,Win32gui,我有一个3列的列表视图(由于这些部分可以工作,所以我没有包括下面的列和项插入代码): 现在我尝试使用WM_DRAWITEM根据第一列中的文本更改背景颜色: case WM_DRAWITEM: { LPDRAWITEMSTRUCT pDIS = (LPDRAWITEMSTRUCT)(lParam); HDC hDC = pDIS->hDC; RECT rc = pDIS->rcItem; // initi

我有一个3列的列表视图(由于这些部分可以工作,所以我没有包括下面的列和项插入代码):

现在我尝试使用
WM_DRAWITEM
根据第一列中的文本更改背景颜色:

    case  WM_DRAWITEM:
    {
        LPDRAWITEMSTRUCT pDIS = (LPDRAWITEMSTRUCT)(lParam);
        HDC hDC = pDIS->hDC;
        RECT rc = pDIS->rcItem;
        // initialize brushes
        HBRUSH bgRed = CreateSolidBrush (RGB(255,0,0));
        HBRUSH bg = (HBRUSH)(GetStockObject(WHITE_BRUSH));
        // declare strings for the sub-items' text
        TCHAR  text_col1[256];
        TCHAR  text_col2[256];
        TCHAR  text_col3[256];
        // get the text from sub-items
        ListView_GetItemText( pDIS -> hwndItem , pDIS -> itemID , 0 ,text_col1, 256);
        ListView_GetItemText( pDIS -> hwndItem , pDIS -> itemID , 1 ,text_col2, 256);
        ListView_GetItemText( pDIS -> hwndItem , pDIS -> itemID , 2 ,text_col3, 256);
        // fill the row
        if(strcmp(text_col1,"Random_Name") == 0)
        {
            FillRect(hDC,&rc,bgRed);
        }
        else
        {
            FillRect(hDC,&rc,bg);
        }
        // How to draw text of 2nd and 3rd columns within columns' boundaries?
        DrawText(hDC, text_col1, strlen(text_col1), &rc, DT_SINGLELINE|DT_LEFT);
    }
    return 0;
如何绘制第二列和第三列的文本?我可以使用其他字符串调用
DrawText()
,但由于它们都绘制在同一个矩形上,因此它们不符合列边界,而是根据我在上一个参数中指定的格式标志在矩形上自由移动。我如何克服这个问题

如果有一种方法可以在不使用
WM_DRAWITEM
的情况下获得相同的效果,或者将每列定义为单独的矩形或任何其他解决方案,我很乐意听到。

您可以使用宏检索子项的矩形

void ListView\u GetSubItemRect(
hwnd,
项目,
iSubItem,
代码,
中华人民共和国
);

Unrelated,通过调用
DeleteObject(bgRed)防止泄漏当你不再需要刷子时。@Barmak Shemirani,谢谢,注意到。我不应该删除
bg
,因为它是一个股票对象,对吗?是的,
bg
很好,不要尝试删除它。使用
NM\u CUSTOMDRAW
对listview来说可能更容易。
    case  WM_DRAWITEM:
    {
        LPDRAWITEMSTRUCT pDIS = (LPDRAWITEMSTRUCT)(lParam);
        HDC hDC = pDIS->hDC;
        RECT rc = pDIS->rcItem;
        // initialize brushes
        HBRUSH bgRed = CreateSolidBrush (RGB(255,0,0));
        HBRUSH bg = (HBRUSH)(GetStockObject(WHITE_BRUSH));
        // declare strings for the sub-items' text
        TCHAR  text_col1[256];
        TCHAR  text_col2[256];
        TCHAR  text_col3[256];
        // get the text from sub-items
        ListView_GetItemText( pDIS -> hwndItem , pDIS -> itemID , 0 ,text_col1, 256);
        ListView_GetItemText( pDIS -> hwndItem , pDIS -> itemID , 1 ,text_col2, 256);
        ListView_GetItemText( pDIS -> hwndItem , pDIS -> itemID , 2 ,text_col3, 256);
        // fill the row
        if(strcmp(text_col1,"Random_Name") == 0)
        {
            FillRect(hDC,&rc,bgRed);
        }
        else
        {
            FillRect(hDC,&rc,bg);
        }
        // How to draw text of 2nd and 3rd columns within columns' boundaries?
        DrawText(hDC, text_col1, strlen(text_col1), &rc, DT_SINGLELINE|DT_LEFT);
    }
    return 0;