MFC自定义控件绘制

时间:2016-01-21 10:33:58

标签: c++ mfc

我有一些问题 我制作了自定义控制器内部单选按钮 但每次都不清楚画画(眨眼或重叠)。

所以,问题是这个 你是如何绘制customctrl内部控制器的? 画使用CDC?还是内部控制器重绘()? (我用了redraw())

第二个是listctrl经常覆盖的内部单选按钮 我怎样才能每次在listctrl上方绘制内部单选按钮。

这是我的代码:
vfcxxx:xxx相同

void CCheckListCtrl::DrawItem(_In_ LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    int cnt = lpDrawItemStruct->itemID;
    static int rownumber = 0;

    VfcLong tmpnum = -1;
    LONG prev_left = lpDrawItemStruct->rcItem.left;

    LV_COLUMN column_data;
    memset(&column_data, 0, sizeof(LV_COLUMN));
    column_data.mask = LVCF_WIDTH | LVCF_FMT;

    for(int column_index = 0; GetColumn(column_index, &column_data); column_index++)
    {
        RECT tempRC;
        {
            tempRC.top  = lpDrawItemStruct->rcItem.top;
            tempRC.bottom = lpDrawItemStruct->rcItem.bottom;
            tempRC.left  = (prev_left);
            tempRC.right = (prev_left += column_data.cx);
        }

        if (column_index > 1) break;

        LV_ITEM tempLI;
        wchar_t buffer[256];
        {
            tempLI.mask   = LVIF_TEXT | LVIF_PARAM;
            tempLI.iItem  = lpDrawItemStruct->itemID;
            tempLI.iSubItem  = column_index;
            tempLI.pszText  = buffer;
            tempLI.cchTextMax = sizeof(buffer);
            VERIFY(GetItem(&tempLI));
        }

        CString tmpChk = buffer;
        CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

        if( (tmpnum = isInsert(defaultGroup, cnt)) != -1)
        {
            pDC->FillSolidRect(&tempRC, RGB(255, 255, 255));
            pDC->SetTextColor(RGB(0, 0, 0));
            tempRC.left += 2;
            pDC->DrawText(buffer, wcslen(buffer), &tempRC, DT_LEFT);
        }
        else if( (tmpnum = isInsert(radioGroup, cnt)) != -1)
        {
            radioCell cell = radioGroup[tmpnum];
            pDC->FillSolidRect(&tempRC, RGB(cell.color[0], cell.color[1], cell.color[2]));
            pDC->SetTextColor(RGB(125, 125, 125));
            tempRC.left += 2;
            pDC->DrawText(buffer, wcslen(buffer), &tempRC, DT_LEFT);
        }
        else 
        {
            pDC->FillSolidRect(&tempRC, RGB(255, 255, 255));
            pDC->SetTextColor(RGB(0, 0, 0));
            tempRC.left += 2;
            pDC->DrawText(buffer, wcslen(buffer), &tempRC, DT_LEFT);
        }
    }
    setVisibleRowNumber();
    setVisibleAttrRadioButton(prev_left - column_data.cx/2);
}

VfcVoid CCheckListCtrl::setVisibleRowNumber()
{
    visibleMinRow = GetScrollPos(SB_VERT);
    visibleMaxRow = visibleMinRow + 12;
}

VfcVoid CCheckListCtrl::setVisibleAttrRadioButton(VfcLong left)
{
    VfcLong row;
    radioCell cell;
    for(int i = 0; i < radioGroup.size(); i++)
    {
        row = radioGroup[i].row;
        cell = radioGroup[i];

        if( row >= visibleMinRow && row <= visibleMaxRow)
        {
            cell.btn->SetWindowPos(NULL, left - 6, (row - visibleMinRow + 1) * 19 + 4, 0, 0, SWP_NOSIZE);
            cell.btn->ShowWindow(SW_SHOW);
            cell.btn->RedrawWindow();
        }
        else
        {
            cell.btn->ShowWindow(SW_HIDE);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不要在控件内使用控件。永远不会减少因窗口重叠而导致的所有闪烁。

自己画出按钮。 CListCtrl也有自己的复选框样式。 这是one sample

即使向CListCtrl添加多个复选框,也已经been answered