/* Filename: Simple.cpp Programmers: Brian LaSitis, Br. David Carlson Date: March 4, 2003 This Win32 program illustrates simple graphics by drawing one line diagram in a window. There is no interaction with the user. Although much of the Windows code in the program is not understandable to beginning students, the WM_PAINT section in the WndProc function is fairly easy to follow. Students who have a basic understanding of calling class functions can read this section of code and modify it to draw something of their choosing. Tested with: Microsoft Visual C++ 6.0 Microsoft Visual C++ .NET To compile this program in Visual C++ 6.0, create a new project of type "Win32 Application" (not Win32 Console application) and then make sure that "empty project" is selected. Do not select Console application. You can then create a new C++ source file for this project and paste in this code. To compile this program in Visual C++ .NET, create a Visual C++ project of type "Win32 project" (not Console application) and under Application settings be sure that Windows application and Empty project are checked. You can then create a new C++ file for this project and paste in this code. */ #include // Function prototype: LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); // For a Win32 app, there is a WinMain function, not a main function. Ignore the gory Windows details. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, INT iCmdShow) { TCHAR szAppName[] = "WinApp"; TCHAR szWndName[] = "Simple Drawing"; HWND hWnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (! RegisterClass(&wndclass)) { MessageBox(NULL, "Could not create window.", "Error", 0); return 0; } hWnd = CreateWindow(szAppName, szWndName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 580, 400, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, iCmdShow); UpdateWindow(hWnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } // This function processes the various messages for the window. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CREATE: return 0; case WM_PAINT: HDC hDC; PAINTSTRUCT ps; HPEN hOldPen, hNewPen; hDC = BeginPaint(hWnd, &ps); hNewPen = CreatePen(PS_SOLID, 1, RGB(0, 180, 180)); // aqua color (mixes green and blue) hOldPen = (HPEN) SelectObject(hDC, hNewPen); //*** Students: try changing the section between this comment line and the one marked below. *** // Print a message string: TextOut(hDC, 50, 50, "Line drawing", 12); // Draw a rectangle: MoveToEx(hDC, 100, 200, NULL); LineTo(hDC, 300, 200); LineTo(hDC, 300, 300); LineTo(hDC, 100, 300); LineTo(hDC, 100, 200); // And top it with a triangle: LineTo(hDC, 200, 100); LineTo(hDC, 300, 200); //*** Students: this is the end of the section that you might try to change. *** EndPaint(hWnd, &ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, message, wParam, lParam); }