MFC对话框最大化时控件也随比例最大化或者还原

先在对话框类里添加成员变量
float m_nDlgWidth;
float m_nDlgHeight;
float m_nWidth;
float m_nHeight;
float m_Multiple_width;
float m_Multiple_height;
bool Change_flag;

记得在构造函数中初始化change_flag为FALSE,否则执行错误。

1. 在OnInitDialog中 计算出当前对话框的大小与最大化后大小.

CRect rect;
::GetWindowRect(m_hWnd,rect);
ScreenToClient(rect);
m_nDlgWidth = rect.right - rect.left;
m_nDlgHeight = rect.bottom - rect.top;

//计算分辨率
m_nWidth = GetSystemMetrics(SM_CXSCREEN);
m_nHeight = GetSystemMetrics(SM_CYSCREEN);

//计算放大倍数
m_Multiple_width = m_nWidth / m_nDlgWidth;
m_Multiple_height = m_nHeight / m_nDlgHeight;
Change_flag = TRUE;  //判断onsize执行时oninitdlg是否已经执行了

2. 给你对话框添加 OnSize消息:

void CShellBottonDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);

// TODO: 在此处添加消息处理程序代码
if (Change_flag)
{
ReSize(IDC_BUTTON1);
ReSize(IDC_BUTTON2);
ReSize(IDC_BUTTON3);
ReSize(IDC_BUTTON4);
ReSize(IDC_BUTTON5);
ReSize(IDC_BUTTON6);
ReSize(IDC_BUTTON7);
ReSize(IDC_BUTTON8);
ReSize(IDC_STATIC1);
ReSize(IDC_STATIC2);
ReSize(IDC_STATIC3);
ReSize(IDC_STATIC4);
ReSize(IDC_STATIC5);
ReSize(IDC_SEND_STATUS);
ReSize(IDC_COMBO_FPGA);

//恢复放大倍数,并保存
m_Multiple_width = 1.0 / m_Multiple_width;
m_Multiple_height = 1.0 / m_Multiple_height;
}
}

3.刷新控件:添加ReSize函数,根据比例计算控件缩放的大小,然后MoveWindow 到新矩形上

void CShellBottonDlg::ReSize(int nID)
{
CRect Rect;
GetDlgItem(nID)->GetWindowRect(Rect);
ScreenToClient(Rect);
//计算控件左上角点
CPoint OldTLPoint,TLPoint;
OldTLPoint = Rect.TopLeft();
TLPoint.x = long(OldTLPoint.x * m_Multiple_width);
TLPoint.y = long(OldTLPoint.y * m_Multiple_height);

//计算控件右下角点
CPoint OldBRPoint,BRPoint;
OldBRPoint = Rect.BottomRight();
BRPoint.x = long(OldBRPoint.x * m_Multiple_width);
BRPoint.y = long(OldBRPoint.y * m_Multiple_height);

//移动控件到新矩形
Rect.SetRect(TLPoint,BRPoint);
GetDlgItem(nID)->MoveWindow(Rect,TRUE);
}

这样,你就可以随心所遇的放大与缩小你的对话框了,控件也跟着比例缩小放大


  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值