본문 바로가기

Windows/Windows API

CenterWindow

firstzim님의 블로그 | 멋진보이
http://blog.naver.com/firstzim/120018394345
windows api 로 프로그램을 할때...



아래 함수를 가지고 이용하면, 편하다.



winapi 책 같은 데 보면, 기본적으로 윈도우를 띄우고, 다이얼로그 박스 하나 정도 띄운뒤...

아래 함수들을 이용하면, 편하다.



CenterWindow( hDlg ) 같이 그냥 쓰기만 하면된다.



아래의 두 함수의 차이점은 첫번째 함수는 화면 전체를 기준으로 가운데로 맞추어 주는 것이고,



두번째는 부모 윈도우의 중간에 맞추어 주는 것이다.



즉, 첫번째 함수는 전체 프로그램 띄울때 쓰면 좋고, 두번째 함수는 menu를 통해서 어떤 다이얼로그 박스를 띄울때, 그 프로그램의 가운데 나오게 하고 싶을때 쓰면 좋다.



// 선택한 윈도우를 중심에 맞춘다.
void CenterWindow(HWND hWnd)
{
RECT wrt ;
LONG iX, iY, iWidth, iHeight ;

GetWindowRect(hWnd,&wrt) ;
iWidth = wrt.right - wrt.left ;
iHeight = wrt.bottom - wrt.top ;
iX = LONG((GetSystemMetrics(SM_CXSCREEN) - iWidth) / 2) ;
iY = LONG((GetSystemMetrics(SM_CYSCREEN) - iHeight) / 2) ;
MoveWindow(hWnd,iX,iY,iWidth,iHeight,TRUE) ;
}

// 선택한 윈도우를 부모 윈도우의 중심에 맞춘다.
void CenterClient(HWND hWnd)
{
RECT wrt;
LONG iX, iY, iWidth, iHeight, winX, winY ;
GetWindowRect(hWnd,&wrt);
winX = wrt.right;
winY = wrt.bottom;

iWidth = wrt.right - wrt.left ;
iHeight = wrt.bottom - wrt.top ;

iX = LONG(iWidth/ 2) ;
iY = LONG(iHeight/ 2) ;

MoveWindow(hWnd,winX-iX,winY-iY,iWidth,iHeight,TRUE) ;
}

'Windows > Windows API' 카테고리의 다른 글

local ip address (wince)  (0) 2013.10.01
camel CPU info  (0) 2013.10.01
DrawTransparentBitmap  (0) 2013.10.01
system tray  (0) 2013.10.01
Message Cracker Wizard  (0) 2013.10.01