#include #include "basicDrawing.h" #include "basicDrawingPaintArea.h" pixel pixelObject; PAINTSTRUCT ps; const char className[] = "basicDrawing"; const char APP_TITLE[] = "Basic drawing demo"; const unsigned short int APP_WIDTH = 800; const unsigned short int APP_HEIGHT = 600; BOOL modeNormal = true; BOOL leftMouseDown = false; LRESULT CALLBACK mainEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM lParam); void paint (HWND hwnd, LPARAM params) { int x = LOWORD(params); int y = HIWORD(params); HDC hdc = GetDC(hwnd); if (modeNormal) { pixelObject.draw(hdc, x, y); } else { pixelObject.drawLine(hdc, x, y); } ReleaseDC(hwnd, hdc); } LRESULT CALLBACK mainEventListener (HWND hwnd, UINT event, WPARAM object, LPARAM params) { if (event == WM_LBUTTONDOWN) { leftMouseDown = true; if (modeNormal) { paint(hwnd, params); } else { pixelObject.setLineStart(LOWORD(params), HIWORD(params)); } } else if (event == WM_MOUSEMOVE) { if (modeNormal && leftMouseDown) { paint(hwnd, params); } } else if (event == WM_LBUTTONUP) { leftMouseDown = false; if (!modeNormal) { paint(hwnd, params); } } else if (event == WM_COMMAND) { int objectID = LOWORD(object); if (objectID == mItemFileExit) { DestroyWindow(hwnd); } else if (objectID == mItemModeNormal) { modeNormal = true; } else if (objectID == mItemModeLines) { modeNormal = false; } } else if (event == WM_CLOSE) { DestroyWindow(hwnd); } else if (event == WM_DESTROY) { PostQuitMessage(0); } else { return DefWindowProc(hwnd, event, object, params); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR args, int nCmdShow) { WNDCLASSEX wc; //The class settings HWND hwnd; //Main window handler MSG Msg; //Here messages to the application are saved //Step 1: Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = mainEventListener; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(imgMainIcon)); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(GetStockObject(WHITE_BRUSH)); wc.lpszMenuName = MAKEINTRESOURCE(menuSystem); wc.lpszClassName = className; wc.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(imgMainIcon), IMAGE_ICON, 16, 16, 0); if(!RegisterClassEx(&wc)) //If the class was not registered successfully { MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } pixel pixelObject(); //Create the window hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, className, APP_TITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, APP_WIDTH, APP_HEIGHT, NULL, NULL, hInstance, NULL); if(hwnd == NULL) //If a problem occured with creating the window { MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); //This keeps looping and processing messages when they become available //The while loop ends when a destroy message is sent while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; }