#include "stdafx.h" #include "main_program.h" #include "virtpoint.h" #include "virtcircle.h" #include "virtsquare.h" #include "resource.h" #include //jedine okno CMainWin theMainWnd; bool CMainWin::InitInstance(HINSTANCE hInstance) { //registrace okna WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_OBJEKTY); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = (LPCTSTR)IDC_OBJEKTY; wcex.lpszClassName = "ObjektyMyClass"; wcex.hIconSm = LoadIcon(hInstance, (LPCTSTR)IDI_SMALL); RegisterClassEx(&wcex); m_hInstance = hInstance; m_hWnd = CreateWindow(wcex.lpszClassName, "Objekty v C++", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (m_hWnd == NULL) return false; ShowWindow(m_hWnd, SW_SHOWMAXIMIZED); UpdateWindow(m_hWnd); return true; } int CMainWin::Run() { MSG msg; ::SetTimer(m_hWnd, 1,50, NULL); // Main message loop: while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } ::KillTimer(m_hWnd, 1); return (int) msg.wParam; } void CMainWin::ExitInstance() { } void CMainWin::AddItem(CVirtPoint* pPoint) { OBJ_ITEM* p = new OBJ_ITEM; p->pObj = pPoint; p->pNext = m_pList; m_pList = p; m_nItems++; char szBuf[256]; sprintf(szBuf, "Objekty v C++, počet objektů: %d", m_nItems); ::SetWindowText(m_hWnd, szBuf); } void CMainWin::OnAddPoint() { RECT rect; ::GetClientRect(m_hWnd, &rect); AddItem(new CVirtPoint(rect.right*rand() / RAND_MAX, rect.bottom*rand() / RAND_MAX)); } void CMainWin::OnAddCircle() { RECT rect; ::GetClientRect(m_hWnd, &rect); AddItem(new CVirtCircle(rect.right*rand() / RAND_MAX, rect.bottom*rand() / RAND_MAX, 50*rand() / RAND_MAX)); } void CMainWin::OnAddSquare() { RECT rect; ::GetClientRect(m_hWnd, &rect); AddItem(new CVirtSquare(rect.right*rand() / RAND_MAX, rect.bottom*rand() / RAND_MAX, 50*rand() / RAND_MAX)); } //posun objektu - casovac void CMainWin::OnTimer() { RECT rect; ::GetClientRect(m_hWnd, &rect); CMainWin::OBJ_ITEM* p = m_pList; while (p != NULL) { p->pObj->Move(&rect); p = p->pNext; } //vynutime prekresleni ::InvalidateRect(m_hWnd, &rect, TRUE); } //vykresleni objektu void CMainWin::OnDraw(HDC hdc) { CMainWin::OBJ_ITEM* p = m_pList; while (p != NULL) { p->pObj->Draw(hdc); p = p->pNext; } } // // FUNCTION: WndProc(HWND, unsigned, WORD, LONG) // // PURPOSE: Processes messages for the main window. // // WM_COMMAND - process the application menu // WM_PAINT - Paint the main window // WM_DESTROY - post a quit message and return // // /*static*/ LRESULT CALLBACK CMainWin::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { return theMainWnd.OnWndProc(hWnd, message, wParam, lParam); } LRESULT CALLBACK CMainWin::OnWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_TIMER: OnTimer(); break; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) { case IDM_ADDPOINT: OnAddPoint(); break; case ID_ADDCIRCLE: OnAddCircle(); break; case ID_ADDSQUARE: OnAddSquare(); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); OnDraw(hdc); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }