简体   繁体   English

如何在CPropertySheet中重新排序CPropertyPage

[英]How to reorder the CPropertyPage inside a CPropertySheet

We have a CPropertySheet that have 5 CPropertyPage inside. 我们有一个CPropertySheet,里面有5个CPropertyPage。

Let say we have something like this 假设我们有这样的事情

1 2 3 4 5 1 2 3 4 5

Then, based on some business logic, when the user click refresh, we would like to have 然后,根据一些业务逻辑,当用户单击刷新时,我们希望

1 5 2 3 4 1 5 2 3 4

We don't want to delete all the CPropertyPage and recreate them in the correct order (with AddPage() ), we just want to update the position of the page in the sheet. 我们不想删除所有CPropertyPage并以正确的顺序(使用AddPage() )重新创建它们,我们只想更新页面在表单中的位置。

Is this possible? 这可能吗?

Thanks! 谢谢!

Microsoft's MFC CPropertySheet doesn't have a function to do this, however, if you check the Property Sheet Windows Controls API , you can achieve something similar by removing the page you want to move and then insert it at the desired position. Microsoft的MFC CPropertySheet没有执行此操作的功能,但是,如果选中“ 属性表Windows控件API” ,则可以通过删除要移动的页面,然后将其插入所需的位置来实现类似目的。

To do this, you first need to subclass CPropertySheet and add a function to insert a page to a given order: 为此,您首先需要继承CPropertySheet的子类,并添加一个函数以给定的顺序插入页面:

//In the .h of your subclass

//Inserts the page at a given Index
void InsertPageAt(CPropertyPage* pPageToInsert, int nPos);

//In the .cpp file
void CPropertySheetControleur::InsertPageAt(CPropertyPage* pPage, int nPos)
{
    ASSERT_VALID(this);
    ENSURE_VALID(pPage);
    ASSERT_KINDOF(CPropertyPage, pPage);


    // add page to internal list
    m_pages.InsertAt(nPos, pPage);

    // add page externally
    if (m_hWnd != NULL)
    {
        // determine size of PROPSHEETPAGE array
        PROPSHEETPAGE* ppsp = const_cast<PROPSHEETPAGE*>(m_psh.ppsp);
        int nBytes = 0;
        int nNextBytes;
        for (UINT i = 0; i < m_psh.nPages; i++)
        {
            nNextBytes = nBytes + ppsp->dwSize;
            if ((nNextBytes < nBytes) || (nNextBytes < (int)ppsp->dwSize))
                AfxThrowMemoryException();
            nBytes = nNextBytes;
            (BYTE*&)ppsp += ppsp->dwSize;
        }

        nNextBytes = nBytes + pPage->m_psp.dwSize;
        if ((nNextBytes < nBytes) || (nNextBytes < (int)pPage->m_psp.dwSize))
            AfxThrowMemoryException();

        // build new prop page array
        ppsp = (PROPSHEETPAGE*)realloc((void*)m_psh.ppsp, nNextBytes);
        if (ppsp == NULL)
            AfxThrowMemoryException();
        m_psh.ppsp = ppsp;

        // copy processed PROPSHEETPAGE struct to end
        (BYTE*&)ppsp += nBytes;
        Checked::memcpy_s(ppsp, nNextBytes - nBytes , &pPage->m_psp, pPage->m_psp.dwSize);
        pPage->PreProcessPageTemplate(*ppsp, IsWizard());
        if (!pPage->m_strHeaderTitle.IsEmpty())
        {
            ppsp->pszHeaderTitle = pPage->m_strHeaderTitle;
            ppsp->dwFlags |= PSP_USEHEADERTITLE;
        }
        if (!pPage->m_strHeaderSubTitle.IsEmpty())
        {
            ppsp->pszHeaderSubTitle = pPage->m_strHeaderSubTitle;
            ppsp->dwFlags |= PSP_USEHEADERSUBTITLE;
        }
        HPROPSHEETPAGE hPSP = AfxCreatePropertySheetPage(ppsp);
        if (hPSP == NULL)
            AfxThrowMemoryException();

        if (!SendMessage(PSM_INSERTPAGE, nPos, (LPARAM)hPSP))
        {
            AfxDestroyPropertySheetPage(hPSP);
            AfxThrowMemoryException();
        }
        ++m_psh.nPages;
    }
} 

I based this fonction on the CPropertySheet::AddPage code, but at the end of the function, I substituted the message PSM_ADDPAGE to PSM_INSERTPAGE 我将此功能基于CPropertySheet :: AddPage代码,但在函数结尾,我将消息PSM_ADDPAGE替换PSM_INSERTPAGE

Then, to move a page to a specific position, just remove it, then add it at the desired position. 然后,要将页面移动到特定位置,只需将其删除,然后将其添加到所需位置即可。

pPropertySheet->RemovePage(pPageToAdd);
pPropertySheet->InsertPageAt(pPageToAdd, nIndex);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM