看流星社区

 找回密码
 注册账号
查看: 2030|回复: 0

MFC控件自适应窗口大小类

[复制链接]

该用户从未签到

发表于 2017-6-1 12:56:46 | 显示全部楼层 |阅读模式
AutoAdapt.h
#pragma once
class CAutoAdapt
{
public:
        CAutoAdapt(void);
        virtual ~CAutoAdapt(void);

public:
        void ReSize(HWND m_hWnd);  
        void InitSize(HWND m_hWnd);
        POINT old;

private:
       
};
[/code]


AutoAdapt.cpp
#include "stdafx.h"
#include "AutoAdapt.h"
/*
MFC控件自适应窗口大小
使用方法:
在目标类中添加此类成员
在目标初始化函数中调用InitSize函数//这个看自己需求
在WM_SIZE消息处理函数中调用ReSize
*/


CAutoAdapt::CAutoAdapt(void)
{

}


CAutoAdapt::~CAutoAdapt(void)
{

}

//此函数也可以不调用,看自己需求,如果是子窗口 比如tableCtrl中的窗口,调用这个函数 就填满tableCtrl大小
//在初始化函数中添加此函数
void CAutoAdapt::InitSize(HWND m_hWnd)
{
        CRect rect;      
        GetClientRect(m_hWnd,&rect);     //取客户区大小  
       
    old.x=rect.right-rect.left;  
    old.y=rect.bottom-rect.top;  
    int cx = GetSystemMetrics(SM_CXFULLSCREEN);  
    int cy = GetSystemMetrics(SM_CYFULLSCREEN);  
    CRect rt;  
    SystemParametersInfo(SPI_GETWORKAREA,0,&rt,0);  
    cy = rt.bottom;  
        MoveWindow(m_hWnd,0, 0, cx, cy,TRUE);   
}

//在WM_SIZE消息处理函数中添加这个函数
void CAutoAdapt::ReSize(HWND m_hWnd)
{
        float fsp[2];  
    POINT Newp; //获取现在对话框的大小  
    CRect recta;      
        GetClientRect(m_hWnd,&recta);     //取客户区大小   
    Newp.x=recta.right-recta.left;  
    Newp.y=recta.bottom-recta.top;  
        //获得倍数
    fsp[0]=(float)Newp.x/old.x;  
    fsp[1]=(float)Newp.y/old.y;  

    CRect Rect;  
    int woc;  //控件标示
    CPoint OldTLPoint,TLPoint; //左上角  
    CPoint OldBRPoint,BRPoint; //右下角  

    HWND  hwndChild=::GetWindow(m_hWnd,GW_CHILD);  //列出所有控件  

    while(hwndChild)      
    {      
        woc=::GetDlgCtrlID(hwndChild);//取得ID  
                HWND hCtrl = GetDlgItem(m_hWnd,woc);
                GetWindowRect(hCtrl,&Rect);

                //处理左上角坐标
                POINT point;
                point = Rect.TopLeft();
                ScreenToClient(m_hWnd,&point);  
      
                OldTLPoint = point;        
        TLPoint.x = long(OldTLPoint.x*fsp[0]);   
        TLPoint.y = long(OldTLPoint.y*fsp[1]);
               
                //处理右下角坐标
                point = Rect.BottomRight();
                ScreenToClient(m_hWnd,&point);  

                OldBRPoint = point;
        BRPoint.x = long(OldBRPoint.x *fsp[0]);   
        BRPoint.y = long(OldBRPoint.y *fsp[1]);  

        Rect.SetRect(TLPoint,BRPoint);   
        
                MoveWindow(hCtrl,TLPoint.x,TLPoint.y,BRPoint.x-TLPoint.x,BRPoint.y-TLPoint.y,TRUE);
        hwndChild=::GetWindow(hwndChild, GW_HWNDNEXT);      
    }  
    old=Newp;
}

[/code]
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-3-19 15:01

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表