自动调整列宽以适合对话框中的内容

时间:2016-09-26 09:22:56

标签: c++ visual-c++ mfc

Screenshot of wizard dialog

我想自动调整列宽,以匹配最长的内容宽度。我已经做了一个函数(ResizeColumnWidth)来完成这项工作。但是,在添加比标题更长的内容后,该功能似乎无法正常工作。单击“上一步”或“下一步”按钮并再次返回此对话框后,它将自动调整列宽。在字段中添加内容后调用resizing函数后,我是否错过了任何步骤?或者我是否在错误的地方调用了该功能?

这是我的代码:

void CWzdEventDependencies::ResizeColumnWidths()
{
    SetRedraw(FALSE);

    m_lstDependencies.SetColumnWidth(0, 100);
    int nHeaderWidth = m_lstDependencies.GetColumnWidth(0);
    m_lstDependencies.SetColumnWidth(0, LVSCW_AUTOSIZE);
    int nColumnWidth = m_lstDependencies.GetColumnWidth(0);
    m_lstDependencies.SetColumnWidth(0, max(nColumnWidth, nHeaderWidth));

    m_lstDependencies.SetColumnWidth(1, 95);

    m_lstDependencies.SetColumnWidth(2, LVSCW_AUTOSIZE_USEHEADER);
    nHeaderWidth = m_lstDependencies.GetColumnWidth(2);
    m_lstDependencies.SetColumnWidth(2, LVSCW_AUTOSIZE);
    nColumnWidth = m_lstDependencies.GetColumnWidth(2);
    m_lstDependencies.SetColumnWidth(2, max(nColumnWidth, nHeaderWidth));
    m_lstDependencies.SortColumn(0);
    SetRedraw(TRUE);

//  m_lstDependencies.SetColumnWidth(0, 50);

}

    enter code here

void CWzdEventDependencies::InsertDependencies()
{
    m_lstDependencies.DeleteAllItems();
    CorrelationData::FieldsDependenciesType::const_iterator It = m_pFieldsDependencies->begin();
    int nDependency = 0;
    for(; It != m_pFieldsDependencies->end(); ++It, ++nDependency)
    {
        InsertDependency(*It);
    }
}

int CWzdEventDependencies::InsertDependency(const CorrelationEventFieldDependencies* pDependency)
{
    AString strField1    = pDependency->GetField1();
    AString strOperation = pDependency->GetOperation();
    AString strField2    = pDependency->GetField2();

    LVITEM lvItem = {0};

    lvItem.mask         = LVIF_TEXT|LVIF_PARAM;
    lvItem.iItem        = m_lstDependencies.GetItemCount();
    lvItem.pszText      = strField1.LockBuffer();
    lvItem.lParam       = (LPARAM) pDependency;

    lvItem.iItem = m_lstDependencies.InsertItem(&lvItem, m_pCmbFields);
    strField1.UnlockBuffer();
    ASSERT(lvItem.iItem != -1);

    lvItem.pszText      = strOperation.LockBuffer();
    lvItem.iSubItem = 1;
    m_lstDependencies.SetItem(&lvItem, m_pCmbSign);
    strOperation.UnlockBuffer();

    lvItem.pszText      = strField2.LockBuffer();
    lvItem.iSubItem = 2;
    m_lstDependencies.SetItem(&lvItem, m_pCmbFields);
    strField2.UnlockBuffer();

    ResizeColumnWidths();
    UpdateWindow();

    return lvItem.iItem;
}

0 个答案:

没有答案