我在编程序的时候,属性页上显示3个按钮,确定,取消,应用。
我想把"应用"和"取消"隐藏,然后把"确定"按钮移动到边上,大概就是"应用"按钮的位置,我用的是以下方法。
CWnd pWnd = GetDlgItem( ID_APPLY_NOW ); 
     RECT rect;
     pWnd->GetClientRect(&rect);   //得到位置
     pWnd->ShowWindow( FALSE );    //隐藏
     pWnd = GetDlgItem( IDOK ); 
     pWnd->SetWindowText("关闭");  //改文本
     pWnd->MoveWindow(&rect);      //设定位置
     pWnd->GetDlgItem(IDCANCEL);
     pWnd->ShowWindow(FALSE);      //隐藏
可是这样程序运行以后那个按钮就显示在属性页的最左上角了,不是我要的原来"应用"按钮的位置。

解决方案 »

  1.   

    重载属性页
    把上面的代码加到 Oninitdialog 里面
    要在CPropertySheet::Oninitdialog后面处理
    这时各个按钮才在最后的位置
      

  2.   

    获取按钮的句柄,然后就可以象对待窗体一样处理它们了. 下面代码先隐藏掉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;
    }
      

  3.   

    int ids [] = {IDOK, IDCANCEL};//or: ID_APPLY, IDHELP };
    //OK and CANCEL Button move right
    CRect rectBtn;
    int nSpacing = 6; //Button distance
    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);
      

  4.   

    我想知道为什么用我的方法得到的按钮的位置是在左上角。结果是left=0,top=0,right=75,bottom=21.还有,为什么楼上的要先用GetWindowRect()方法,然后再用ScreenToClient()方法,直接用GetClientRect()方法不可以吗?还有两个按钮之间的距离是6是怎么知道到的?那个是固定的数吗?还是自己定义的。
      

  5.   

    GetClientRect()接收客户区坐标,这个函数将CWnd的客户区的客户坐标拷贝到lpRect指向的结构中。客户坐标指定了客户区的左上角和右下角。由于客户坐标是相对于CWnd客户区的左上角的,因此左上角的坐标是(0,0)。 
    GetWindowRect()这个函数将CWnd对象的边界矩形的大小拷贝到lpRect所指向的结构中。大小是用相对于显示器屏幕左上角的屏幕坐标给出的,其中包括了标题条,边框和滚动条的大小,如果有的话。两个范围是不一样的
      

  6.   

    CWnd *pWnd = GetDlgItem( ID_APPLY_NOW ); 
         RECT rect;
         pWnd->GetWindowRect(&rect);   //得到位置
         pWnd->ShowWindow( FALSE );    //隐藏
         pWnd = GetDlgItem( IDOK ); 
         pWnd->SetWindowText("关闭");  //改文本
         pWnd->MoveWindow(&rect);      //设定位置
         pWnd->GetDlgItem(IDCANCEL);
         pWnd->ShowWindow(FALSE);      //隐藏