VC中最大化及还原对话框实现

在使用VC编写基于MFC的对话框程序时,经常要将窗口在最大化和默认窗体之间进行切换,默认情况下点击最大化按钮后,对话框会全屏,但是里面的组件却没有按相同比例放大。以下给出实现的相关代码。

 

       //最大化,获取当前对话框大小

 CRect rectDlg;

      ::GetWindowRect(m_hWnd,rectDlg); //该函数返回指定窗口的边框矩形的尺寸。

//以相对于屏幕坐标左上角的屏幕坐标给出

                                      // m_hWnd是当前对话框句柄,在头文件定义

       m_intDlgWidth = rectDlg.right - rectDlg.left;

       m_intDlgHeight = rectDlg.bottom - rectDlg.top;

       //SM_CXSCREEN,SM_CYSCREEN 以像素为单位计算的屏幕尺寸。

m_nWidth = GetSystemMetrics(SM_CXSCREEN);

       m_nHeight = GetSystemMetrics(SM_CYSCREEN);

       m_floatMultipleWidth = float(m_nWidth)/float(m_intDlgWidth);

       m_floatMultipleHeight = float(m_nHeight)/float(m_intDlgHeight);

       int i;

       for (i = 1000;i <= 1004; ++i)

       {

              //将当前对话框中的控件尺寸和按比例放大后的尺寸保存起来。

//(1000~1004为控件ID号,下同。)

SaveSize(i); 

       }

       for (i = 1007;i <= 1008; ++i)

       {

              SaveSize(i);

       }

       for (i = 1010;i <= 1022; ++i)

       {

              SaveSize(i);

       }

       for (i = 1024;i <= 1030; ++i)

       {

              SaveSize(i);

       }

       for (i = 1035;i <= 1052; ++i)

       {

              SaveSize(i);

       }

       for (i = 1055;i <= 1058; ++i)

       {

              SaveSize(i);

       }

       for (i = 1060;i <= 1076; ++i)

       {

              SaveSize(i);

       }

       for (i = 1078;i <= 1098; ++i)

       {

              SaveSize(i);

       }

       for (i = 1103;i <= 1104; ++i)

       {

              SaveSize(i);

       }

       for (i =1112;i<= 1114; ++i)

       {

              SaveSize(i);

       }

       SaveSize(IDCANCEL);

// m_sizeInitFinish头文件中定义的BOOL变量,说明是否保存完对话框初始尺寸

m_sizeInitFinish = TRUE;

 

以上这段代码可以在对话框源程序的初始化部分(::OnInitDialog())添加。

 

SaveSize(int nID)函数的实现如下:

BOOL CXXXXDlg::SaveSize(int nID)

{

CRect rect;

GetDlgItem(nID)->GetWindowRect(rect);

ScreenToClient(rect);//将屏幕坐标转换成用户坐标保存在rect中。

CPoint oldTLPoint,TLPoint;

oldTLPoint = rect.TopLeft();//默认对话框尺寸的左上角坐标

//最大化对话框左上角坐标

TLPoint.x = long(oldTLPoint.x * m_floatMultipleWidth);

TLPoint.y = long(oldTLPoint.y * m_floatMultipleHeight);

CPoint oldBRPoint,BRPoint;

oldBRPoint = rect.BottomRight();//默认对话框尺寸的右下角坐标

//最大化对话框左上角坐标

BRPoint.x = long(oldBRPoint.x * m_floatMultipleWidth);

BRPoint.y = long(oldBRPoint.y * m_floatMultipleHeight);

//下述代码作用是判断如果控件是CComboBox类型时,由于考虑到下拉框长//度,所以对纵坐标进行加长处理

if (IDC_COMBO_DS_PORT == nID)

{

        BRPoint.y = BRPoint.y + 60;

        oldBRPoint.y = oldBRPoint.y + 60;

}

if (IDC_COMBO_ML_PROT == nID)

{

        BRPoint.y = BRPoint.y + 60;

        oldBRPoint.y = oldBRPoint.y +60;

}

if (IDC_COMBO_SERIAL_NO == nID)

{

        BRPoint.y = BRPoint.y + 60;

        oldBRPoint.y = oldBRPoint.y +60;

}

if (IDC_COMBO_DATA_BITS== nID)

{

        BRPoint.y = BRPoint.y + 60;

        oldBRPoint.y = oldBRPoint.y + 60;

}

if (IDC_COMBO_STOP_BITS == nID)

{

        BRPoint.y = BRPoint.y + 60;

        oldBRPoint.y = oldBRPoint.y + 60;

}

if (IDC_COMBO_BAUD_RATE== nID)

{

        BRPoint.y = BRPoint.y + 60;

        oldBRPoint.y = oldBRPoint.y + 60;

}

if (IDC_COMBO_CHECK_BITS== nID)

{

        BRPoint.y = BRPoint.y + 60;

        oldBRPoint.y = oldBRPoint.y + 60;

}

//vector<CPoint> m_oldTLPoints;在头文件进行定义

m_oldTLPoints.push_back(oldTLPoint);

m_oldBRPoints.push_back(oldBRPoint);

m_newTLPoints.push_back(TLPoint);

m_newBRPoints.push_back(BRPoint);

return TRUE;

}

以下进行实现窗口的最大化(还原)触发事件:

void CXXXXXDlg::OnSize(UINT nType, int cx, int cy)

{

       CDialog::OnSize(nType, cx, cy);

       // TODO: 在此处添加消息处理程序代码

       if (m_sizeInitFinish)   //判断是否将尺寸保存起来了

       {    

//int m_maxFlag;头文件定义,判断是要进行最大化窗口,还是要将窗口还原。

              m_maxFlag = m_maxFlag ^ 1;

              int i,j = 0;

              for (i = 1000;i <= 1004; ++i)

              {

                     ReSize(i,j);//此函数的实现后续。

                     ++j;

              }

              for (i = 1007;i <= 1008; ++i)

              {

                     ReSize(i,j);

                     ++j;

              }

              for (i = 1010;i <= 1022; ++i)

              {

                     ReSize(i,j);

                     ++j;

              }

              for (i = 1024;i <= 1030; ++i)

              {

                     ReSize(i,j);

                     ++j;

              }

              for (i = 1035;i <= 1052; ++i)

              {

                     ReSize(i,j);

                     ++j;

              }

              for (i = 1055;i <= 1058; ++i)

              {

                     ReSize(i,j);

                     ++j;

              }

         for (i = 1060;i <= 1076; ++i)

        {

                     ReSize(i,j);

                     ++j;

        }

        for (i = 1078;i <= 1098; ++i)

        {

                     ReSize(i,j);

                     ++j;

        }

        for (i = 1103;i <= 1104; ++i)

        {

                     ReSize(i,j);

                     ++j;

      }

        for (i =1112;i<= 1114; ++i)

        {

                     ReSize(i,j);

                     ++j;

        }

              ReSize(IDCANCEL,j);

       }

}

 

 

ReSize(int nID,int j)函数的实现如下:

void CXXXXXDlg::ReSize(int nID,int j)

{

       CRect rect;

       CPoint TLPoint;

       CPoint BRPoint;

       if (m_maxFlag)

       {

              rect.SetRect(m_newTLPoints[j],m_newBRPoints[j]);     

       }

       else

       {

              rect.SetRect(m_oldTLPoints[j],m_oldBRPoints[j]);

       }

       GetDlgItem(nID)->MoveWindow(rect,TRUE);

       //以下代码是为一些自己实现的一些动态组件尺寸改变所编写的,根据个人

//情况可以去掉。

if (IDC_STATIC_CJ_AN_RESULT == nID)

       {

              vector<int> vwidth(16,(rect.right - rect.left)/16);

              rect.right = rect.left + ((rect.right - rect.left) & (~0xf));

              rect.top = rect.bottom - 40;

              list2->MoveWindow(rect,TRUE); 

             

              rect.bottom = rect.bottom - 40;

              rect.top = rect.bottom - 40;

              list1->MoveWindow(rect,TRUE); 

             

              list1->ChangeColumnWidth(vwidth);

              list2->ChangeColumnWidth(vwidth);

       }

       if (IDC_STATIC_CJ_BL_RESULT == nID)

       {

              vector<int> vwidth(16,(rect.right - rect.left)/16);

              rect.right = rect.left + ((rect.right - rect.left) & (~0xf));

 

              rect.top = rect.bottom - 40;

              list3->MoveWindow(rect,TRUE);

              list3->ChangeColumnWidth(vwidth);

       }

       if (IDC_STATIC_CJ_TH_RESULT == nID)

       {

              vector<int> vwidth(16,(rect.right - rect.left)/16);

              rect.right = rect.left + ((rect.right - rect.left) & (~0xf));

 

              rect.top = rect.bottom - 40;

              list4->MoveWindow(rect,TRUE);

              list4->ChangeColumnWidth(vwidth);

 

       }

}

你可能感兴趣的:(VC中最大化及还原对话框实现)