Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
컴 터 래픽 를 위한컴퓨터 그래픽스를 위한윈도우 프로그래밍윈도우 프로그래밍
1 t W k 20091st Week, 2009
시작하기 Visual Studio 2008시작하기 – Visual Studio 2008
새 프로젝트새 프로젝트
■ 파일 새로 만들기 프로젝트■ 파일 새로 만들기 프로젝트■ Visual C++ 프로젝트 Win32 프로젝트
응용 프로그램 설정응용 프로그램 설정
■ 빈 프로젝트■ 빈 프로젝트
오류오류
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
“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
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
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 WNDCLSS
typedef struct _WNDCLASS {UINT style;
typedef struct _WNDCLASS {UINT style; y ;WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HANDLE hInstance; HICON hIcon;
y ;WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HANDLE hInstance; HICON hIcon; ;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
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
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
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
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
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
The Window Procedure (2)The Window Procedure (2)
■ Handling three messages:■ Handling three 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 ltdefault: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)
d t i th i d■ 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)
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( )
연습 문제 (1)연습 문제 (1)
■ Application Window의 배경색 아이콘 커서를■ Application Window의 배경색, 아이콘, 커서를변경시키시오.A li ti Wi d 의 크기를 800 600으로 변■ Application Window의 크기를 800x600으로 변경시키시오.
의 제 을 자신의 학번과 이■ Application Window의 제목을 자신의 학번과 이름으로 변경시키시오.
■ 좌측 마우스 버튼을 클릭했을 때 생성되는Message Box를 다음과 같이 변경시키시오.g
리소스 추가리소스 추가