Graphics Laboratory Korea University Windows Programming Graphics Laboratory Korea University
Microsoft Visual C++ 6.0
File New
New Project
Win32 Application (1/2)
Win32 Application (2/2)
Workspace
New File
Workspace FileView
“main.cpp”
#include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // global variables char *szClassName = "Draw2DClass"; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) { // Registers the window class WNDCLASSEX wcex; wcex.cbSize = sizeof(wcex); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(NULL,IDI_APPLICATION); wcex.hIconSm = NULL; wcex.hCursor = LoadCursor(NULL,IDC_ARROW); wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wcex.lpszMenuName = NULL; wcex.lpszClassName = szClassName; RegisterClassEx(&wcex); // Create the main window HWND hWnd = CreateWindow(szClassName, "Draw in 2D", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL); if (!hWnd) return FALSE; ShowWindow(hWnd, SW_SHOWDEFAULT); UpdateWindow(hWnd); // Main message loop MSG msg; while (GetMessage(&msg, NULL, 0, 0)) TranslateMessage(&msg); DispatchMessage(&msg); } UnregisterClass(szClassName, wcex.hInstance); return msg.wParam;
Window Message Handling /* * WndProc: to process messages for the main window */ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; switch (message) case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0;
Build
Execute
Draw Points /* * WndProc: to process messages for the main window */ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; register int i, j; switch (message) case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... for(i=-5; i<=5; i++) for(j=-5; j<=5; j++) SetPixel(hdc, 400+i, 300+j, RGB(255, 0, 0)); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0;
Result – Draw Points
Mouse Event Handing (1/2) LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; register int i, j; switch (message) case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... for(i=-5; i<=5; i++) for(j=-5; j<=5; j++) SetPixel(hdc, 400+i, 300+j, RGB(255, 0, 0)); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); case WM_LBUTTONDOWN: case WM_LBUTTONUP: case WM_MOUSEMOVE: default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0;
Mouse Event Handing (1/2) int point[2] = { -5, -5 }; LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; register int i, j; switch (message) case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... for(i=-5; i<=5; i++) for(j=-5; j<=5; j++) SetPixel(hdc, point[0]+i, point[1]+j, RGB(255, 0, 0)); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); case WM_LBUTTONDOWN: point[0] = LOWORD(lParam); point[1] = HIWORD(lParam); InvalidateRect(hWnd, NULL, FALSE); default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0;
Result – Mouse Event Handling
Insert Resource
Resources
Menu (1/6)
Menu (2/6)
Menu (3/6)
Menu (4/6)
Menu (5/6)
Menu (6/6)
Save Resources
Include Resources (1/3)
Include Resources (2/3)
Include Resources (3/3)
Result – Menu
Menu Event Handling
Result – Menu Event Handling
Miscellanies Check / Uncheck a item of menu CheckMenuItem(GetMenu(hWnd), ID_PRIM_LINE, MF_CHECKED); CheckMenuItem(GetMenu(hWnd), ID_PRIM_LINE, MF_UNCHECKED); Enable / Disable a item of menu EnableMenuItem(GetMenu(hWnd), ID_PRIM_LINE, MF_ENABLED); EnableMenuItem(GetMenu(hWnd), ID_PRIM_LINE, MF_GRAYED); COLORREF GetPixel( int x, int y ) const; COLORREF SetPixel( int x, int y, COLORREF crColor );
RGB Color Model R G B Color Colors are additive 0.0 0.0 0.0 Black 1.0 Red 0.0 1.0 0.0 Green 0.0 0.0 1.0 Blue 1.0 1.0 0.0 Yellow 1.0 0.0 1.0 Magenta 0.0 1.0 1.0 Cyan 1.0 1.0 1.0 White
Insert Resource
Dialog Box (1/5)
Dialog Box (2/5)
Dialog Box (3/5)
Dialog Box (4/5)
Dialog Box (5/5)
New Menu
Dialog Callback Function (1/2)
Dialog Callback Function (2/2)
Dialog Creation
Result – Dialog Box
Debug