20
Windows Programming 1 t W k 2011 1 st Week , 2011 시작하기 Visual Studio 2008 시작하기 Visual Studio 2008

시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

Windows Programming

1 t W k 20111st Week, 2011

시작하기 Visual Studio 2008시작하기 – Visual Studio 2008

Page 2: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

새 프로젝트새 프로젝트

■ 파일 새로 만들기 프로젝트■ 파일 새로 만들기 프로젝트■ Visual C++ 프로젝트 Win32 프로젝트

응용 프로그램 설정응용 프로그램 설정

■ 빈 프로젝트■ 빈 프로젝트

Page 3: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

“Prac01” 솔루션Prac01 솔루션

새 항목 추가새 항목 추가

Page 4: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

C++ 파일C++ 파일

“main cpp”main.cpp

Page 5: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

“main0 cpp” 다운로드main0.cpp 다운로드

솔루션 빌드솔루션 빌드

Page 6: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

오류오류

Unicode vs Multi ByteUnicode vs. Multi-Byte

■ SBCS (Single Byte Character Set)■ SBCS (Single Byte Character Set)■■ ASCIIASCII code: 1 byte character set

■ DBCS (Double Byte Character Set)■ Korean■ Korean

■ MBCS (Multi Byte Character Set)■ MBCS (Multi Byte Character Set)■ SBCS + DBCS: ~ Windows 9x

■ WBCS (Wide Byte Character Set)■■ UnicodeUnicode: Windows 2000 ~■■ UnicodeUnicode: Windows 2000

Page 7: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

프로젝트 속성프로젝트 속성

멀티바이트 문자 집합 사용멀티바이트 문자 집합 사용

Page 8: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

실행실행

■ 솔루션 빌드 F7■ 솔루션 빌드 F7■ 디버깅 시작 F5

디버깅하지 않 시작■ 디버깅하지 않고 시작 Ctrl + F5

■ 디버그 툴

실행 결과실행 결과

Page 9: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

“main cpp” 구조main.cpp 구조#include <windows.h>

HWND MyMWindowHandle = 0;

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, intnShowCmd )

{{// Create the main window

// Main message loopg p

}

LRESULT CALLBACK WndProc( HWND hWnd UINT msg WPARAM wParamLRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )

{

}

Includes, Global Variables, and Prototypes

Page 10: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

Includes, Global Variables, and Prototypes

■ Includes■ Includes

■ Obtaining the structures, types, and function

#include <windows.h>#include <windows.h>

■ Obtaining the structures, types, and function declarations needed for using the basic elements of the Win32 API

■ Global VariablesHWND MyMWindowHandle = 0;HWND MyMWindowHandle = 0;

■ Handle to a window – to refer our main application window

Prototypes■ Prototypes

Our main window’s window procedure

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

■ Our main window s window procedure

WinMain ( ) CreationWinMain ( ) – Creation

Page 11: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

WinMain ( )WinMain ( )

■ The Windows equivalent to the main() in■ The Windows equivalent to the main() in normal C++ programming

int WINAPI WinMain(int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nShowCmd

HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nShowCmd

■ hInstance: Handle to the current application

););

instance■ hPrevInstance: Not used in Win32 programmingl C dLi Th d li t t i■ lpCmdLine: The command line argument string used to run the program

■ nCmdShow: Specifying how the application should■ nCmdShow: Specifying how the application should be displayed

WNDCLASS (1)WNDCLASS (1)

■ Describing our window with WNDCLSS■ Describing our window with WNDCLSStypedef struct _WNDCLASS {

UINT style; WNDPROC lpfnWndProc;

typedef struct _WNDCLASS {UINT style; WNDPROC lpfnWndProc;WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HANDLE hInstance; HICON hIcon; HCURSOR hCursor;

WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HANDLE hInstance; HICON hIcon; HCURSOR hCursor;HCURSOR hCursor; HBRUSH hbrBackground; LPCTSTR lpszMenuName; LPCTSTR lpszClassName;

} WNDCLASS;

HCURSOR hCursor; HBRUSH hbrBackground; LPCTSTR lpszMenuName; LPCTSTR lpszClassName;

} WNDCLASS;

■ style: Specifying the class stylel f W dP P i t t th i d d■ lpfnWndProc: Pointer to the window procedure functioncbClsExtra and cbWndExtra: Extra memory slots■ cbClsExtra and cbWndExtra: Extra memory slots

Page 12: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

WNDCLASS (2)WNDCLASS (2)

■ hInstance: A handle to our application instance■ hInstance: A handle to our application instance■ hIcon: Specifying a handle to an icon■ hCursor: Specifying a handle to a cursor■ hCursor: Specifying a handle to a cursor■ hbrBackground: Specifying the background of the

client area of the windowclient area of the window■ lpszMenuName: Specifying the window’s menu■ lpszClassName: Specifying the name of the■ lpszClassName: Specifying the name of the

window class structure

Class StylesClass Styles

Page 13: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

LoadIcon( )LoadIcon( )

LoadCursor( )LoadCursor( )

Page 14: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

GetStockObject( )GetStockObject( )

Creating the Window (1)Creating the Window (1)

■ Creating a window based on WNDCLASS■ Creating a window based on WNDCLASS description

HWND CreateWindow( LPCTSTR lpClassName,

HWND CreateWindow( LPCTSTR lpClassName, p ,LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth,

p ,LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, ,int nHeight, HWND hWndParent, HMENU hMenu, HANDLE hInstance, PVOID lpParam

,int nHeight, HWND hWndParent, HMENU hMenu, HANDLE hInstance, PVOID lpParam

■ lpClassName: The name of the registered WNDCLASS t t

p);

p);

WNDCLASS structure■ lpWindowName: The name we want to give our

windowwindow

Page 15: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

Creating the Window (2)Creating the Window (2)

■ dwStyle: Defining the style of window■ dwStyle: Defining the style of window■ x: The position at the top-left corner of the window■ y: The position at the top-left corner of the window■ y: The position at the top left corner of the window■ nWidth: The width of the window in pixels■ nHeight: The height of the window in pixels■ nHeight: The height of the window in pixels■ hWndParent: Handle to a window that is to be the

parent of this windowparent of this window■ hMenu: A handle to menu■ hInstance: Handle to the application■ hInstance: Handle to the application■ lpParam: A pointer to user-defined data

WinMain ( ) Message LoopWinMain ( ) – Message Loop

Page 16: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

The Message Loop (1)The Message Loop (1)

■ The message structure that represents a■ The message structure that represents a Windows message

typedef struct tagMSG { typedef struct tagMSG { yp gHWND hwnd; UINT message; WPARAM wParam; LPARAM lParam; DWORD time;

yp gHWND hwnd; UINT message; WPARAM wParam; LPARAM lParam; DWORD time;

■ hWnd: The handle to the window

POINT pt; } MSG;

POINT pt; } MSG;

■ message: A predefined constant value identifying the message (e.g. WM_QUIT)P d lP E t i f ti■ wParam and lParam: Extra information

■ time: The time the message was posted■ pt: The (x y) coordinates of the mouse cursor■ pt: The (x, y) coordinates of the mouse cursor

The Message Loop (2)The Message Loop (2)

■ Entering the message loop■ Entering the message loopwhile( GetMessage( &msg, 0, 0, 0 ) ){

TranslateMessage( &msg );DispatchMessage( &msg );

while( GetMessage( &msg, 0, 0, 0 ) ){

TranslateMessage( &msg );DispatchMessage( &msg );

■ GetMessage()■ Returning true unless a WM QUIT

DispatchMessage( &msg );}

DispatchMessage( &msg );}

■ Returning true unless a WM_QUIT■ Retrieving a message from the message queue■ Filling in members of MSG structure

()■ TranslateMessage()■ Performing some keyboard translation and virtual key

messages to character messages specifically■ DispatchMessage()

■ Dispatching the message off to the appropriate window proc.p

Page 17: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

The Window ProcedureThe Window Procedure

The Window Procedure (1)The Window Procedure (1)

■ Executing in response to a message our■ Executing in response to a message our window receives LRESULT CALLBACK WndProc(

HWND hWnd,UINT uMsg

LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg

CallbackCallback function: Windows will be calling this

UINT uMsg, WPARAM wParam,LPARAM lParam

);

UINT uMsg, WPARAM wParam,LPARAM lParam

);

■■ CallbackCallback function: Windows will be calling this function outside of the code space of the program

■ hWnd: The handle to the window receiving the■ hWnd: The handle to the window receiving the message

■ uMsg: A predefined value that identifies the■ uMsg: A predefined value that identifies the particular message (e.g. WM_QUIT)

■ wParam and lParam: Extra information about the message

Page 18: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

The Window Procedure (2)The Window Procedure (2)

■ Handling these messages:■ Handling these messages:switch( msg ) {

case WM_LBUTTONDOWN:MessageBo ( NULL "H ll W ld" "H ll " MB OK )

switch( msg ) {case WM_LBUTTONDOWN:

MessageBo ( NULL "H ll W ld" "H ll " MB OK )MessageBox( NULL, "Hello, World", "Hello", MB_OK );break;

case WM_KEYDOWN:if( wParam == VK_ESCAPE )

DestroyWindow( MyMWindowHandle );

MessageBox( NULL, "Hello, World", "Hello", MB_OK );break;

case WM_KEYDOWN:if( wParam == VK_ESCAPE )

DestroyWindow( MyMWindowHandle );y ( y );break;

case WM_DESTROY: PostQuitMessage( 0 ); break;

d f lt

y ( y );break;

case WM_DESTROY: PostQuitMessage( 0 ); break;

d f lt

WM LBUTTONDOWN: clicking left mouse button

default:return DefWindowProc( hWnd, msg, wParam, lParam );

}

default:return DefWindowProc( hWnd, msg, wParam, lParam );

}

■ WM_LBUTTONDOWN: clicking left mouse button■ WM_KEYDOWN: pressing a key (c.f. virtual key codevirtual key code)■ WM DESTROY: destroying the window■ WM_DESTROY: destroying the window

The Window Procedure (3)The Window Procedure (3)

■ Default window procedure■ Default window procedure

Win32 API function

return DefWindowProc( hwnd, msg, wParam, lParam );return DefWindowProc( hwnd, msg, wParam, lParam );

■ Win32 API function■ Using default behavior for all other messages (e.g.

minimizing maximizing resizing and closing aminimizing, maximizing, resizing, and closing a window)

Page 19: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

The Message Box FunctionThe Message Box Function

■ Win32 API function■ Win32 API functionint MessageBox(

HWND hWnd,LPCTSTR lpText,

int MessageBox(HWND hWnd,LPCTSTR lpText,

hWnd: Handle of owner window (may specify null)

LPCTSTR lpCaption,UINT uType

);

LPCTSTR lpCaption,UINT uType

);

■ hWnd: Handle of owner window (may specify null)■ lpText: Text to put in the message boxl C ti : Te t fo the title of the message bo■ lpCaption: Text for the title of the message box

■ uType: Style of the message boxMessageBox( NULL, "Hello, World", "Hello", MB_OK );MessageBox( NULL, "Hello, World", "Hello", MB_OK );

MessageBox( )MessageBox( )

Page 20: 시작하기–VisualStudio2008Visual Studio 2008graphics.hallym.ac.kr/teach/2011/cg/src/01prac.pdf · 2011. 7. 13. · Includes, Global Variables, and Prototypes Includes Obtainingthestructures,types,andfunction

연습 문제연습 문제

■ Application Window의 배경색 아이콘 커서를■ Application Window의 배경색, 아이콘, 커서를변경시키시오.A li ti Wi d 의 크기를 800 600으로 변■ Application Window의 크기를 800x600으로 변경시키시오.

의 제 을 자신의 학번과 이■ Application Window의 제목을 자신의 학번과 이름으로 변경시키시오.

■ 좌측 마우스 버튼을 클릭했을 때 생성되는Message Box를 다음과 같이 변경시키시오.g