簡體   English   中英

如何在CPropertySheet中重新排序CPropertyPage

[英]How to reorder the CPropertyPage inside a CPropertySheet

我們有一個CPropertySheet,里面有5個CPropertyPage。

假設我們有這樣的事情

1 2 3 4 5

然后,根據一些業務邏輯,當用戶單擊刷新時,我們希望

1 5 2 3 4

我們不想刪除所有CPropertyPage並以正確的順序(使用AddPage() )重新創建它們,我們只想更新頁面在表單中的位置。

這可能嗎?

謝謝!

Microsoft的MFC CPropertySheet沒有執行此操作的功能,但是,如果選中“ 屬性表Windows控件API” ,則可以通過刪除要移動的頁面,然后將其插入所需的位置來實現類似目的。

為此,您首先需要繼承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;
    }
} 

我將此功能基於CPropertySheet :: AddPage代碼,但在函數結尾,我將消息PSM_ADDPAGE替換PSM_INSERTPAGE

然后,要將頁面移動到特定位置,只需將其刪除,然后將其添加到所需位置即可。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM