introduction to windows programminggraphics.hallym.ac.kr/teach/2007/gp1/src/08prac.pdf ·...

11
Game Programming I Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1 Introduction to Windows Programming 5 th Week, 2007 Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1 Sample Program – “hello.cpp”

Upload: others

Post on 12-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Windows Programminggraphics.hallym.ac.kr/teach/2007/gp1/src/08prac.pdf · 2008-09-16 · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Introduction to Windows

Game Programming I

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Introduction to Windows Programming

5th Week, 2007

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Sample Program – “hello.cpp”

Page 2: Introduction to Windows Programminggraphics.hallym.ac.kr/teach/2007/gp1/src/08prac.pdf · 2008-09-16 · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Introduction to Windows

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Microsoft Visual Studio .Net

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

File New Project…

Visual C++ Win32Win32 Project

Page 3: Introduction to Windows Programminggraphics.hallym.ac.kr/teach/2007/gp1/src/08prac.pdf · 2008-09-16 · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Introduction to Windows

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Application Settings

Empty project

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

‘Hello’ Solution

Page 4: Introduction to Windows Programminggraphics.hallym.ac.kr/teach/2007/gp1/src/08prac.pdf · 2008-09-16 · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Introduction to Windows

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (1)

Copying the source code

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (2)

Project Add Existing Item

Page 5: Introduction to Windows Programminggraphics.hallym.ac.kr/teach/2007/gp1/src/08prac.pdf · 2008-09-16 · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Introduction to Windows

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Adding the Source Code (3)

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Result

Page 6: Introduction to Windows Programminggraphics.hallym.ac.kr/teach/2007/gp1/src/08prac.pdf · 2008-09-16 · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Introduction to Windows

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

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

}return Run();

}

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

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;

}

Page 7: Introduction to Windows Programminggraphics.hallym.ac.kr/teach/2007/gp1/src/08prac.pdf · 2008-09-16 · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Introduction to Windows

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Hello World Windows Application (3)

MainWindowHandle = ::CreateWindow("Hello","Hello",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0, instanceHandle,0);

if(MainWindowHandle == 0){

::MessageBox(0, "CreateWindow - Failed", 0, 0);return false;

}

::ShowWindow(MainWindowHandle, show);::UpdateWindow(MainWindowHandle);

return true;}

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

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

Page 8: Introduction to Windows Programminggraphics.hallym.ac.kr/teach/2007/gp1/src/08prac.pdf · 2008-09-16 · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Introduction to Windows

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

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

case WM_KEYDOWN:if( wParam == VK_ESCAPE )

::DestroyWindow(MainWindowHandle);return 0;

case WM_DESTROY: ::PostQuitMessage(0); return 0;

}return ::DefWindowProc(windowHandle, msg, wParam, lParam);

}

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

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

Page 9: Introduction to Windows Programminggraphics.hallym.ac.kr/teach/2007/gp1/src/08prac.pdf · 2008-09-16 · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Introduction to Windows

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

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;

}

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Hint (2)

MainWindowHandle = ::CreateWindow("Hello","Hello",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0, instanceHandle,0);

if(MainWindowHandle == 0){

::MessageBox(0, "CreateWindow - Failed", 0, 0);return false;

}

::ShowWindow(MainWindowHandle, show);::UpdateWindow(MainWindowHandle);

return true;}

Page 10: Introduction to Windows Programminggraphics.hallym.ac.kr/teach/2007/gp1/src/08prac.pdf · 2008-09-16 · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Introduction to Windows

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

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

case WM_KEYDOWN:if( wParam == VK_ESCAPE )

::DestroyWindow(MainWindowHandle);return 0;

case WM_DESTROY: ::PostQuitMessage(0); return 0;

}return ::DefWindowProc(windowHandle, msg, wParam, lParam);

}

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

Result – Exercise

Page 11: Introduction to Windows Programminggraphics.hallym.ac.kr/teach/2007/gp1/src/08prac.pdf · 2008-09-16 · Game Programming I Sun-Jeong Kim sunkim/teach/2007/gp1 Introduction to Windows

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

연습 문제 (1)

메인윈도우를변경하시오.배경색

아이콘

커서

타이틀

크기

대화상자를변경하시오.타이틀, 문장, 버튼의종류

다른대화상자를생성하시오.마우스우측버튼을클릭했을때

Enter 키를눌렀을때

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2007/gp1

연습 문제 (2)

제출방법2007. 4. 5 수업시간전까지

Exercise에대한프로그램을작성 (예: “학번.cpp”) + 실행결과 (본강의자료 10쪽또는 20쪽과같은출력이미지, 예: “학번.jpg”)

2개파일을압축한 “학번.zip”을웹에 upload 할것