VC通用控件自适应屏幕类

此为我程序中的一个类,本用于WinCE,但在桌面系统上也同样适用!

使用方法(在WM_INITDIALOG或WM_CREATE消息中加入):

CWindowAnchor::BeginControlBound(hwnd)

 

手动调整控件位置:

CWindowAnchor::AddControl(hwnd,IDC_STATIC1, & WindowAnchorInfo(WAT_LEFT | WAT_TOP, 2 , 8 , 4 , 10 ));
CWindowAnchor::AddControl(hwnd,IDC_STATIC1,
& WindowAnchorInfo(WAT_LEFT | WAT_TOP | WAT_RIGHT, 2 , 20 , 4 , 10 ));
CWindowAnchor::AddControl(hwnd,IDC_STATIC1,
& WindowAnchorInfo(WAT_LEFT | WAT_TOP, 2 , 8 , 40 , 10 ));


自动调整控件位置(跟据设计时资源文件中控件的大小及位置):

CWindowAnchor::AddControl(hwnd,IDC_STATIC1, & WindowAnchorInfo(WAT_LEFT | WAT_TOP));
CWindowAnchor::AddControl(hwnd,IDC_STATIC1,
& WindowAnchorInfo(WAT_LEFT | WAT_TOP | WAT_RIGHT));

 

响应WM_SIZE消息:

case  WM_SIZE:
    
return  HANDLE_WM_SIZE(hwndDlg,wParam,lParam,CWindowAnchor::OnSize);

 

响应WM_DESTROY消息:

CWindowAnchor::EndControlBound(hwnd);

 

 

代码:

#pragma  once
#include 
< map >

#if  defined (_MSC_VER)
    
#pragma  warning(disable: 4786)
#endif

/* 用于WindowAnchorInfo结构的停靠类型 */
typedef 
enum  WindowAnchorType
{
    WAT_TOP
= 0x0001 ,
    WAT_LEFT
= 0x0002 ,
    WAT_RIGHT
= 0x0004 ,
    WAT_BOTTOM
= 0x0008
};

/* 控件定位描述信息 */
typedef 
struct  WindowAnchorInfo{
    DWORD dwAnchor; 
// WAT_*
    RECT rcOriginalRect;  // 控件的原始边距,如果为空则自动获取(仅适用于WM_INIT中)
    
    WindowAnchorInfo(DWORD pAnchor
= WAT_TOP | WAT_LEFT,LONG pLeft = 0 ,LONG pTop = 0 ,LONG pRight = 0 ,LONG pBottom = 0 )
    {
        dwAnchor
= pAnchor;
        rcOriginalRect.left
= pLeft;
        rcOriginalRect.top
= pTop;
        rcOriginalRect.right
= pRight;
        rcOriginalRect.bottom
= pBottom;
    };
};

typedef std::map
< HWND,WindowAnchorInfo >  ControlHashtable;

typedef 
struct {
    INT nWidth; 
// 对话框宽度
    INT nHeight;  // 对话框高度
    INT nMinHeight;  // 对话框最小高度
    ControlHashtable mapControls;  // 对话框所有子控件
}WindowAnchorDialog;

/*
 * 对话框子控件定位
 * 2009.03.29 By Frank
*/
static   class  CWindowAnchor
{
private :
    
static  BOOL _ReSize(HWND hwndDlg,  const  WindowAnchorDialog  * wad, HWND hwndCtrl,  const  WindowAnchorInfo  * wai);

public :
    
/*
     * 开始调整(此调用中会获取当前对话框的大小,如果在设计后要调整对话框大小,请先调用此方法)
     * hwndDlg:对话框句柄
    
*/
    
static  BOOL BeginControlBound(HWND hwndDlg);

    
/*
     * 结束调整
     * hwndDlg:对话框句柄
    
*/
    
static  BOOL EndControlBound(HWND hwndDlg);

    
/*
     * 添加一个控件到调整列表
     * hWndInsertAfter:HWND_BOTTOM |HWND_NOTOPMOST | HWND_TOP | HWND_TOPMOST |-2不改变 | Is Hwnd
    
*/
    
static  BOOL AddControl(HWND hwndDlg, INT nCtrlID, WindowAnchorInfo  * wai, HWND hWndInsertAfter = (HWND) - 2 );

    
/*
     * 调整一个指定控件的大小
    
*/
    
static  BOOL ReSize(HWND hwndDlg, HWND hwndCtrl);

    
/*
     * 响应WM_SIZE消息
    
*/
    
static  BOOL OnSize(HWND hwndDlg, UINT state,  int  cx,  int  cy);

    
/* 相应WM_VSCROLL消息 */
    
static  BOOL OnVScroll(HWND hwnd, HWND hwndCtl, UINT code,  int  pos);
};

 

下载地址:单击下载

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值