Game Programming I Introduction to Windows Programming 5 th Week, 2007 Sample Program hello.cpp
Microsoft Visual Studio.Net File New Project Visual C++ Win32 Win32 Project
Application Settings Empty project Hello Solution
Adding the Source Code (1) Copying the source code Adding the Source Code (2) Project Add Existing Item
Adding the Source Code (3) Result
Hello World Windows Application (1) #include <windows.h> HWND MainWindowHandle = 0; bool InitWindowsApp(HINSTANCE instancehandle, int show); int Run(); LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam); int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, PSTR pcmdline, int nshowcmd) if(!initwindowsapp(hinstance, nshowcmd)) ::MessageBox(0, "Init - Failed", "Error", MB_OK); return Run(); Hello World Windows Application (2) bool InitWindowsApp(HINSTANCE instancehandle, int show) WNDCLASS wc; wc.style = CS_HREDRAW CS_VREDRAW; wc.lpfnwndproc = WndProc; wc.cbclsextra = 0; wc.cbwndextra = 0; wc.hinstance = instancehandle; wc.hicon = ::LoadIcon(0, IDI_APPLICATION); wc.hcursor = ::LoadCursor(0, IDC_ARROW); wc.hbrbackground = static_cast<hbrush>(::getstockobject(white_brush)); wc.lpszmenuname = 0; wc.lpszclassname = "Hello"; if(!::registerclass(&wc)) ::MessageBox(0, "RegisterClass - Failed", 0, 0); return false;
Hello World Windows Application (3) MainWindowHandle = ::CreateWindow( "Hello", "Hello", WS_OVERLAPPEDWINDOW, 0, 0, instancehandle, 0); if(mainwindowhandle == 0) ::MessageBox(0, "CreateWindow - Failed", 0, 0); return false; ::ShowWindow(MainWindowHandle, show); ::UpdateWindow(MainWindowHandle); return true; Hello World Windows Application (4) int Run() MSG msg; ::ZeroMemory(&msg, sizeof(msg)); while(::getmessage(&msg, 0, 0, 0) ) ::TranslateMessage(&msg); ::DispatchMessage(&msg); return msg.wparam;
Hello World Windows Application (5) LRESULT CALLBACK WndProc(HWND windowhandle, UINT msg, WPARAM wparam, LPARAM lparam) switch( msg ) case WM_LBUTTONDOWN: ::MessageBox(0, "Hello, World", "Hello", MB_OK); case WM_KEYDOWN: if( wparam == VK_ESCAPE ) ::DestroyWindow(MainWindowHandle); case WM_DESTROY: ::PostQuitMessage(0); return ::DefWindowProc(windowHandle, msg, wparam, lparam); Exercise Change the main window: the background color the icon the cursor the title the size Change the message box: the title, sentence, and buttons Create another message box when the right button of a mouse is clicked the enter key is pressed
Hint (1) bool InitWindowsApp(HINSTANCE instancehandle, int show) WNDCLASS wc; wc.style = CS_HREDRAW CS_VREDRAW; wc.lpfnwndproc = WndProc; wc.cbclsextra = 0; wc.cbwndextra = 0; wc.hinstance = instancehandle; wc.hicon = ::LoadIcon(0, IDI_APPLICATION); wc.hcursor = ::LoadCursor(0, IDC_ARROW); wc.hbrbackground = static_cast<hbrush>(::getstockobject(white_brush)); wc.lpszmenuname = 0; wc.lpszclassname = "Hello"; if(!::registerclass(&wc)) ::MessageBox(0, "RegisterClass - Failed", 0, 0); return false; Hint (2) MainWindowHandle = ::CreateWindow( "Hello", "Hello", WS_OVERLAPPEDWINDOW, 0, 0, instancehandle, 0); if(mainwindowhandle == 0) ::MessageBox(0, "CreateWindow - Failed", 0, 0); return false; ::ShowWindow(MainWindowHandle, show); ::UpdateWindow(MainWindowHandle); return true;
Hint (3) LRESULT CALLBACK WndProc(HWND windowhandle, UINT msg, WPARAM wparam, LPARAM lparam) switch( msg ) case WM_LBUTTONDOWN: ::MessageBox(0, "Hello, World", "Hello", MB_OK); case WM_KEYDOWN: if( wparam == VK_ESCAPE ) ::DestroyWindow(MainWindowHandle); case WM_DESTROY: ::PostQuitMessage(0); return ::DefWindowProc(windowHandle, msg, wparam, lparam); Result Exercise
연습문제 (1) 메인윈도우를변경하시오. 배경색 아이콘 커서 타이틀 크기 대화상자를변경하시오. 타이틀, 문장, 버튼의종류 다른대화상자를생성하시오. 마우스우측버튼을클릭했을때 Enter 키를눌렀을때 연습문제 (2) 제출방법 2007. 4. 5 수업시간전까지 Exercise에대한프로그램을작성 ( 예 : 학번.cpp ) + 실행결과 ( 본강의자료 10쪽또는20쪽과같은출력이미지, 예 : 학번.jpg ) 2개파일을압축한 학번.zip 을웹에upload 할것