본문 바로가기

Windows/Windows API

윈도우 가운데로 오기 (center window)

#include <windows.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
HWND hWndMain;
LPSTR lpszClass="CenterWindow";

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
         ,LPSTR lpszCmdParam,int nCmdShow)
{
    HWND hWnd;
    MSG Message;
    WNDCLASS WndClass;
    g_hInst=hInstance;
    
    WndClass.cbClsExtra=0;
    WndClass.cbWndExtra=0;
    WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    WndClass.hInstance=hInstance;
    WndClass.lpfnWndProc=(WNDPROC)WndProc;
    WndClass.lpszClassName=lpszClass;
    WndClass.lpszMenuName=NULL;
    WndClass.style=CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&WndClass);

    hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
        NULL,(HMENU)NULL,hInstance,NULL);
    ShowWindow(hWnd,nCmdShow);
    hWndMain=hWnd;
    
    while(GetMessage(&Message,0,0,0)) {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }
    return Message.wParam;
}

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) ;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    char Mes[]="화면 중앙에 윈도우가 위치합니다";

    switch(iMessage) {
    case WM_LBUTTONDOWN:
    case WM_CREATE:
        CenterWindow(hWnd);
        return 0;
    case WM_PAINT:
        hdc=BeginPaint(hWnd, &ps);
        TextOut(hdc,50,50,Mes,strlen(Mes));
        EndPaint(hWnd, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}

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

시스템 정보 (GetSystemInfo)  (0) 2013.10.01
실행시간 검사 (check run time)  (0) 2013.10.01
capture  (0) 2013.10.01
비트 블릿 (BitBlt)  (0) 2013.10.01
알파 블렌딩 (alphablending)  (0) 2013.10.01