본문 바로가기
프로그래밍/API

API-MessageBox

by 리뷰하는 (게임)프로그래머_리프TV 2010. 4. 19.


윈도우 프로그램들을 보면
정말 자주
흔히
많이
보는 메시지 박스,

당장 살펴 보자!

라잇 놔우..!

#include <windows.h>

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
HINSTANCE g_hInst;
LPCTSTR lpszClass = TEXT("GraphOut");

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

	while( GetMessage( &Message, NULL, 0, 0 ) )
	{
		TranslateMessage( &Message );
		DispatchMessage( &Message );
	}
	return (int)Message.wParam;
}

LRESULT CALLBACK WndProc( HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam )
{
	// 게임 종료에 관한 test를 위한 bool타입 변수
	bool GameOver = false;
	TCHAR str[128];

	switch( iMessage )
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	case WM_LBUTTONDOWN:
		//MessageBox( hWnd, TEXT("마우스 왼쪽 버튼을 눌렀습니다." ),
		//	TEXT("메시지 박스"), MB_OK );	// 마우스 왼쪽 버튼을 누르면 메시지 박스가 뜬다.
		// 게임의 종료를 물어 본다면.?
		if( MessageBox( hWnd, TEXT("게임을 계속 하시겠습니까?") , TEXT("질문"),
			MB_YESNO ) == IDYES )
		{
			// yes일때 들어 오고
			GameOver = true;
		}
		else
		{
			// no 일때 들어 온다.
			GameOver = false;
		}
		wsprintf( str, TEXT("현재 게임 상태 : %d" ), GameOver );
		SetWindowText( hWnd, str );	// 타이틀바에 문자열을 삽입
		return 0;
	}
	return( DefWindowProc( hWnd, iMessage, wParam, lParam));
}​


특별히 설명할 만한 구간은 없다.
MessageBox 함수를 사용, hWnd, Box문자열, Box타이틀 문자열, 스타일 을 받게 되는데,
예제에 보이는 MB_YESNO의 경우 Yes를 누르면 IDYES, 당연 No를 누르면 IDNO 를 return하게 된다.
그렇다고 IDYES == true 는 아니다.
그 외에도 IDOK, IDCANCEL 등 다양하게 존재한다.
마지막으로 wsprintf를 사용해 str에 문자를 넣어 주게 되는데,
SetWindowText를 사용해 제목표시줄에 문자열을 출력하는 것을 알 수 있다.

/출력 화면

'프로그래밍 > API' 카테고리의 다른 글

API-Key입력(2)  (0) 2010.04.21
API-Key입력(1)  (0) 2010.04.21
API-GraphOut  (0) 2010.04.19
API-DrawText  (0) 2010.04.19
API-TextOut  (0) 2010.04.19