CMFCPropertyGridCtrl添加文本

CMFCPropertyGridCtrl Adding text

本文关键字:文本 添加 CMFCPropertyGridCtrl      更新时间:2023-10-16

在CMFCPropertyGridCtrl中插入文本时遇到问题。基本上,我用以下代码插入值:

void GridBuilder::buildGrid() { 
    int cycles = 0, buisnessRules = 0;
    for(vector<cycle *>::iterator it = this->listCycle.begin(); it!= this->listCycle.end(); ++it) {
        buisnessRules = 0;
        cycles++;
        if(!this->mFilter->assertCycleConstraints((*it)->idthread, (*it)->dataStart, (*it)->dateStop, (*it)->name.c_str(), cycles)) {
            continue;
        }
        CString text;
        text.Format(_T("cycle : %S"), (*it)->name.c_str());
        CMFCPropertyGridProperty* parent = new CMFCPropertyGridProperty(text);
        mGrid->AddProperty(parent);
        time_t timestampBegin = (*it)->dataStart / 1000;
        char bufferBegin[256];
        strftime(bufferBegin, sizeof(bufferBegin), "%A %d %B %Y - %X", localtime(&timestampBegin));
        time_t timestampEnd = (*it)->dateStop / 1000;
        char bufferEnd[256];
        strftime(bufferEnd, sizeof(bufferEnd), "%A %d %B %Y - %X", localtime(&timestampBegin));
        text.Format(_T("%d"), (*it)->idthread);     parent->AddSubItem(new CMFCPropertyGridProperty(_T("Thread Id"), text));
        text.Format(_T("%S"), (*it)->name.c_str()); parent->AddSubItem(new CMFCPropertyGridProperty(_T("Name"),      text));
        text.Format(_T("%S:%d"), bufferBegin, (*it)->dataStart % 1000); parent->AddSubItem(new CMFCPropertyGridProperty(_T("DateStart"), text));
        text.Format(_T("%S:%d"), bufferEnd,   (*it)->dateStop  % 1000); parent->AddSubItem(new CMFCPropertyGridProperty(_T("DateStop"),  text));
        parent->Expand(FALSE);
        for(vector<br *>::iterator itBr = (*it)->listBr.begin(); itBr!=(*it)->listBr.end(); ++itBr) {
            buisnessRules++;
            if(!this->mFilter->assertBuisnessRuleConstraints((*itBr)->dateStart, (*itBr)->dateStop, (*itBr)->name.c_str(), buisnessRules)) {
                continue;
            }
            UINT64 executionTime = (*itBr)->dateStop - (*itBr)->dateStart;
            CString text;
            text.Format(_T("%S (%d u00B5s)"), (*itBr)->name.c_str(), executionTime);
            CMFCPropertyGridProperty* buisnessRule = new CMFCPropertyGridProperty(text);
            parent->AddSubItem(buisnessRule);

            text.Format(_T("%S"), (*itBr)->name.c_str()); buisnessRule->AddSubItem(new CMFCPropertyGridProperty(_T("Name"),      text));
            text.Format(_T("%d u00B5s"), executionTime); buisnessRule->AddSubItem(new CMFCPropertyGridProperty(_T("Execution time"), text));
            buisnessRule->Expand(FALSE);
        }
    }
}

这工作得很好,并显示了我想要的一切。然而,我的向量通常非常大(超过8000个元素),这需要大量时间来渲染。。。

我怀疑每次插入新元素时CMFCPropertyGridCtrl都会重新绘制整个树,这需要相当长的时间。。。


我的问题如下:如何在CMFCPropertyGridCtrl中添加文本而不重新绘制整个树?或者如何优化此代码?谢谢

我就是这样做的:

在开始向网格中添加项目之前,请调用LockWindowUpdate(此处从CWnd-MSDN继承),然后在完成时调用UnlockWindowUpdate(此处为MSDN)和Invalidate

m_Grid->LockWindowUpdate();
// ... populate grid
m_Grid->UnlockWindowUpdate();
m_Grid->Invalidate();