下面是一段徐景周译的(原作:Mustafa Demirhan)关于propertysheet的文章中的一段代码
首先,要获取按钮的句柄,然后就可以象对待窗体一样处理它们了. 下面代码先隐藏掉Apply和Help铵钮,再把OK和Cancel按移动到右侧。 BOOL CMyPropSheet::OnInitDialog () 
{
    BOOL bResult = CPropertySheet::OnInitDialog();
 
    int ids [] = {IDOK, IDCANCEL};//, ID_APPLY_NOW, IDHELP };
    
    // Hide Apply and Help buttons
    CWnd *pWnd = GetDlgItem (ID_APPLY_NOW);
    pWnd->ShowWindow (FALSE);
    pWnd = GetDlgItem (IDHELP);
    pWnd->ShowWindow (FALSE);
    
    CRect rectBtn;
    int nSpacing = 6;        // space between two buttons...
 
    for( int i =0; i < sizeof(ids)/sizeof(int); i++)
    {
        GetDlgItem (ids [i])->GetWindowRect (rectBtn);
        
        ScreenToClient (&rectBtn);
        int btnWidth = rectBtn.Width();
        rectBtn.left = rectBtn.left + (btnWidth + nSpacing)* 2;
        rectBtn.right = rectBtn.right + (btnWidth + nSpacing)* 2;
 
        GetDlgItem (ids [i])->MoveWindow(rectBtn);
    }
     return bResult;
}编译可以通过,但是在执行时总是报Debug Assertion Failer错误,调试进去之后发现是ASSERT(hWnd != NULL)错误,小弟不知道如何解决。在线等谢谢

解决方案 »

  1.   

    这段代码是没有问题的,是你其他的问题。也需是你在窗口还没有创建出来,就对其上的控件进行了一些操作。。你单步进行调试一下,看是哪句代码出错贴出OnInitDialog 所有代码
      

  2.   

    要隐藏掉这两个按钮,其实不需要这么复杂,直接设置属性即可
    设置CPropertySheet的m_psh.dwFlags以及CPropertyPage的m_psp.dwFlags的风格
    sheet
    |= PSH_NOAPPLYNOW // no "Apply" button
    &= ~PSH_HASHELP // no "Help" buttonpage
    &= ~ PSP_HASHELP // no "Help" button
      

  3.   

    确实是没有创建出窗口就对其进行了操作,但是是在CPropertySheet::OnInitDialog()里面报的错误,不知道怎么先把创建出来,难道OnInitDialog()不是在创建窗口吗?迷惑flyelf多谢你。。你说的方法是可以的但是我还想对其有其他的操作,所以还是要把这个问题搞清楚吧
      

  4.   

    你在OnInitDialog的return TRUE;之前SetTimer(),然后在ontimer里对控件进行操作,同时要在ontimer里killtimer
      

  5.   


    我是这样调用的
    CPropertySheetTest   m_mainfram("test");
              //class CPropertySheetTest   : public CPropertySheet
             m_mainfram.OninitialDlg();而自己的OninitialDlg()就是照抄的那个例子程序
    BOOL CPropertySheetTest::OnInitDialog () 
    {
        BOOL bResult = CPropertySheet::OnInitDialog();
     
        int ids [] = {IDOK, IDCANCEL};//, ID_APPLY_NOW, IDHELP };
        
        // Hide Apply and Help buttons
        CWnd *pWnd = GetDlgItem (ID_APPLY_NOW);
        pWnd->ShowWindow (FALSE);
        pWnd = GetDlgItem (IDHELP);
        pWnd->ShowWindow (FALSE);
        
        CRect rectBtn;
        int nSpacing = 6;        // space between two buttons...
     
        for( int i =0; i < sizeof(ids)/sizeof(int); i++)
        {
            GetDlgItem (ids [i])->GetWindowRect (rectBtn);
            
            ScreenToClient (&rectBtn);
            int btnWidth = rectBtn.Width();
            rectBtn.left = rectBtn.left + (btnWidth + nSpacing)* 2;
            rectBtn.right = rectBtn.right + (btnWidth + nSpacing)* 2;
     
            GetDlgItem (ids [i])->MoveWindow(rectBtn);
        }
         return bResult;
    }
    BOOL CPropertySheet::OnInitDialog()
    {
    // change tab style if scrolling tabs desired (stacked tabs are default)
    if (!m_bStacked)
    {
    HWND hWndTab = (HWND)::GetDlgItem(m_hWnd, AFX_IDC_TAB_CONTROL);
    if (hWndTab != NULL)
    CWnd::ModifyStyle(hWndTab, TCS_MULTILINE, TCS_SINGLELINE, 0);
    } if (!IsWizard())
    {
    // resize the tab control so the layout is less restrictive
    HWND hWnd = (HWND)::GetDlgItem(m_hWnd, AFX_IDC_TAB_CONTROL);
    ASSERT(hWnd != NULL);
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~这里会报错
    CRect rectOld;
    ::GetWindowRect(hWnd, &rectOld);
    ScreenToClient(rectOld);
    CRect rectNew(0, 0, 0, 32);
    ::MapDialogRect(m_hWnd, &rectNew);
    if (rectNew.bottom < rectOld.bottom)
    {
    // move tab control
    int cyDiff = rectOld.Height() - rectNew.bottom;
    ::SetWindowPos(hWnd, NULL, 0, 0, rectOld.Width(), rectNew.bottom,
    SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE); // move buttons by similar amount
    for (int i = 0; i < _countof(_afxPropSheetButtons); i++)
    {
    hWnd = ::GetDlgItem(m_hWnd, _afxPropSheetButtons[i]);
    if (hWnd != NULL)
    {
    ::GetWindowRect(hWnd, &rectOld);
    ScreenToClient(&rectOld);
    ::SetWindowPos(hWnd, NULL,
    rectOld.left, rectOld.top - cyDiff,
    0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
    }
    } // resize property sheet itself similarly
    GetWindowRect(&rectOld);
    SetWindowPos(NULL, 0, 0, rectOld.Width(), rectOld.Height() - cyDiff,
    SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
    }
    } BOOL bResult = (BOOL)Default(); if (m_bModeless && !IsWizard())
    {
    // layout property sheet so button area is not accounted for
    CRect rectWnd;
    GetWindowRect(rectWnd);
    CRect rectButton;
    HWND hWnd = ::GetDlgItem(m_hWnd, IDOK);
    ASSERT(hWnd != NULL);
    ::GetWindowRect(hWnd, rectButton);
    SetWindowPos(NULL, 0, 0,
    rectWnd.Width(), rectButton.top - rectWnd.top,
    SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE); // remove standard buttons for modeless dialogs
    for (int i = 0; i < _countof(_afxPropSheetButtons); i++)
    {
    HWND hWnd = ::GetDlgItem(m_hWnd, _afxPropSheetButtons[i]);
    if (hWnd != NULL)
    {
    ::ShowWindow(hWnd, SW_HIDE);
    ::EnableWindow(hWnd, FALSE);
    }
    }
    } // center the property sheet relative to the parent window
    if (!(GetStyle() & WS_CHILD))
    CenterWindow(); return bResult;
    }