본문 바로가기

3.구현/VC++

[MFC] EasySize.h 매크로 풀어논 것

#ifndef __EASYSIZE_H
#define __EASYSIZE_H
#define ES_BORDER 0xffffffff<?xml:namespace prefix = o />
#define ES_KEEPSIZE 0xfffffffe
#define ES_HCENTER 0x00000001
#define ES_VCENTER 0x00000002
#define DECLARE_EASYSIZE \ //class내에 멤버함수를 추가하는 기능
void __ES__RepositionControls(BOOL bInit);\
void __ES__CalcBottomRight(CWnd *pThis, BOOL bBottom, int &bottomright, int &topleft,
UINT id, UINT br, int es_br, CRect &rect, int clientbottomright);

#define INIT_EASYSIZE // 모든 컨트롤의 위치와 크기 정보를 획득하고 다시 적용함(__ES__RepositionControls())
__ES__RepositionControls(TRUE);
__ES__RepositionControls(FALSE);

#define UPDATE_EASYSIZE // 모든 컨트롤의 위치,크기 정보를 적용
if(GetWindow(GW_CHILD)!=NULL) __ES__RepositionControls(FALSE)

#define EASYSIZE_MINSIZE(mx,my,s,r) // mx, my 최소 위도우 크기를 지정해는 함수
if(r->right-r->left < mx) {
    if((s == WMSZ_BOTTOMLEFT)||(s == WMSZ_LEFT)||(s == WMSZ_TOPLEFT)) // 왼쪽 방향에서 줄이는 경우
        r->left = r->right-mx; 
    else
        r->right = r->left+mx; 
}

if(r->bottom-r->top < my) { 
    if((s == WMSZ_TOP)||(s == WMSZ_TOPLEFT)||(s == WMSZ_TOPRIGHT))  // 오른쪽 방향에서 줄이는 경우
        r->top = r->bottom-my; 
    else 
        r->bottom = r->top+my; 
}

#define BEGIN_EASYSIZE_MAP(class) \ // 컨트롤 정의 위한 헤더부분
// 해당클래스의 멤버함수 __ES__CalcBottomRight 정의
void class::__ES__CalcBottomRight( CWnd *pThis, BOOL bBottom, int &bottomright, int &topleft,
                        UINT id, UINT br, int es_br, CRect &rect, int clientbottomright) {\
    if(br==ES_BORDER) 
        bottomright = clientbottomright-es_br;\
    else if(br==ES_KEEPSIZE) 
        bottomright = topleft+es_br;\
    else { 
        CRect rect2;\
        pThis->GetDlgItem(br)->GetWindowRect(rect2);
        pThis->ScreenToClient(rect2);\
        bottomright = (bBottom?rect2.top:rect2.left) - es_br;
    }
}\
// 해당 클래스의 멤버함수 __ES__RepositionControls 정의
// 각 컨트롤이 크기와 위치가 여기에서 조절된다.
void class::__ES__RepositionControls(BOOL bInit) 
{ 
    CRect rect,rect2,client; 
    GetClientRect(client);
    #define END_EASYSIZE_MAP // 컨트롤 정의 위한 끝부분
    Invalidate(); // 새로 그려줌 WM_PAINT
    UpdateWindow(); // 윈도우 갱신
}

#define EASYSIZE(id,l,t,r,b,o) \
static int id##_es_l, id##_es_t, id##_es_r, id##_es_b;
if(bInit) { // 다이얼로그로 부터 정보 가져옴
    GetDlgItem(id)->GetWindowRect(rect); 
    ScreenToClient(rect);\
    //Left 계산
    if(o & ES_HCENTER) // 정렬 옵션이 가로 가운데 인경우
        id##_es_l = rect.Width()/2; 
    else {\ // 가로 가운데가 아니면
        if(l==ES_BORDER) // 가로가 크기가 늘여나는 경우
            id##_es_l = rect.left; 
        else if(l==ES_KEEPSIZE) // 가로 크기가 고정
            id##_es_l = rect.Width(); 
        else {\ // 다른 값이면 기본값 설정
            GetDlgItem(l)->GetWindowRect(rect2); 
            ScreenToClient(rect2);\
            id##_es_l = rect.left-rect2.right;
        }
    }\

    //Top 계산
    if(o & ES_VCENTER) // 정렬 옵션이 세로 가운데인 경우
    id##_es_t = rect.Height()/2; 
    else {\ // 정렬 옵션이 세로 가운데가 아니면
        if(t==ES_BORDER)  // 세로 가변 크기이면
            id##_es_t = rect.top; 
        else if(t==ES_KEEPSIZE) // 세로 고정 크기면
            id##_es_t = rect.Height(); 
        else {\ // 기타 세로 기본설정값
            GetDlgItem(t)->GetWindowRect(rect2); 
            ScreenToClient(rect2);\
            id##_es_t = rect.top-rect2.bottom;
        }
    }\

    // Right 계산
    if(o & ES_HCENTER) 
        id##_es_r = rect.Width(); 
    else { 
        if(r==ES_BORDER) 
            id##_es_r = client.right-rect.right; 
        else if(r==ES_KEEPSIZE) 
            id##_es_r = rect.Width(); 
        else {\
            GetDlgItem(r)->GetWindowRect(rect2); 
            ScreenToClient(rect2);\
            id##_es_r = rect2.left-rect.right;
        }
    }\

    // Bottom 계산
    if(o & ES_VCENTER) 
        id##_es_b = rect.Height(); 
    else  { 
        if(b==ES_BORDER) 
            id##_es_b = client.bottom-rect.bottom; 
        else if(b==ES_KEEPSIZE) 
            id##_es_b = rect.Height(); 
        else {\
            GetDlgItem(b)->GetWindowRect(rect2); 
            ScreenToClient(rect2);\
            id##_es_b = rect2.top-rect.bottom;
        }
    }\
} else {\ // bInit가 FALSE로 다이얼로그로 값 갱신시킴
    int left,top,right,bottom; 
    BOOL bR = FALSE,bB = FALSE;\

    if(o & ES_HCENTER) { // 가로 가운데 정렬
        int _a,_b;\
        // Left계산
        if(l==ES_BORDER) 
            _a = client.left; 
        else { 
            GetDlgItem(l)->GetWindowRect(rect2); 
            ScreenToClient(rect2); 
            _a = rect2.right; 
        }\
        // Right 계산
        if(r==ES_BORDER) 
            _b = client.right; 
        else { 
            GetDlgItem(r)->GetWindowRect(rect2); 
            ScreenToClient(rect2); 
            _b = rect2.left; 
        }\

        left = _a+((_b-_a)/2-id##_es_l); 
        right = left + id##_es_r;
    } else {\ // 가로 가운데 정렬이 아니면
        if(l==ES_BORDER) 
            left = id##_es_l;\
        else if(l==ES_KEEPSIZE) { 
            __ES__CalcBottomRight(this,FALSE,right,left,id,r,id##_es_r,rect,client.right); 
            left = right-id##_es_l;\
        } else { 
            GetDlgItem(l)->GetWindowRect(rect2); 
            ScreenToClient(rect2); 
            left = rect2.right + id##_es_l; 
        }\

        if(l != ES_KEEPSIZE) 
            __ES__CalcBottomRight(this,FALSE,right,left,id,r,id##_es_r,rect,client.right);
    }\

    if(o & ES_VCENTER) { // 세로 가운데 정렬
        int _a,_b;\
        if(t==ES_BORDER) 
            _a = client.top; 
        else { 
            GetDlgItem(t)->GetWindowRect(rect2); 
            ScreenToClient(rect2); 
            _a = rect2.bottom; 
        }\
        if(b==ES_BORDER) 
            _b = client.bottom; 
        else { 
            GetDlgItem(b)->GetWindowRect(rect2); 
            ScreenToClient(rect2); 
            _b = rect2.top; 
        }\
        top = _a+((_b-_a)/2-id##_es_t); 
        bottom = top + id##_es_b;
    } else {\ // 세로 가운데 정렬 아니면
        if(t==ES_BORDER) 
            top = id##_es_t;\
        else if(t==ES_KEEPSIZE) { 
            __ES__CalcBottomRight(this,TRUE,bottom,top,id,b,id##_es_b,rect,client.bottom); 
            top = bottom-id##_es_t;\
        } else { 
            GetDlgItem(t)->GetWindowRect(rect2); 
            ScreenToClient(rect2); 
            top = rect2.bottom + id##_es_t; 
        }\
        if(t != ES_KEEPSIZE) 
            __ES__CalcBottomRight(this,TRUE,bottom,top,id,b,id##_es_b,rect,client.bottom);
    }\
    GetDlgItem(id)->MoveWindow(left,top,right-left,bottom-top,FALSE);\
}
반응형