C++ Schleife, GDI, wo liegt der Fehler?
-
Hallo,
verstehe einfach nicht, wo mein Fehler liegt? Ich möchte verschiedene Ellipsen zeichnen. Mit WAVEPOINTER::AddCurve fügt man einem Array eine Ellipse hinzu, die dann mit WAVEPOINTER::Draw in einer Schleife gezeichnet wird. Aber das funktioniert nicht?! Er zeichnet immer nur die allererste Ellipse.
//wavepointer.h #include <Windows.h> #include <GdiPlus.h> class WAVEPOINTER{ private: INT32 iPosX; INT32 iPosY; INT32 iMidPointRadius; // Radius des Mittelpunktes INT32 iCurves; INT32 iRadius[100]; Gdiplus::Color cLineColor[100]; public: WAVEPOINTER(INT32 iRadius, INT32 iPosX, INT32 iPosY, INT32 iMidPointRadius, Gdiplus::Color cLineColor); void AddCurve(INT32 iRadius, Gdiplus::Color cLineColor); void Draw(HDC hDC); };
// wavepointer.cpp #include "stdafx.h" WAVEPOINTER::WAVEPOINTER(INT32 iRadius, INT32 iPosX, INT32 iPosY, INT32 iMidPointRadius, Gdiplus::Color cLineColor){ this->iRadius[0] = iRadius; this->iPosX = iPosX; this->iPosY = iPosY; this->cLineColor[0] = cLineColor; this->iMidPointRadius = iMidPointRadius; this->iCurves = 1; }; void WAVEPOINTER::Draw(HDC hDC){ Graphics gGraphics(hDC); for (INT32 i = 0; i < iCurves; i++) { Pen pPen(cLineColor[i]); SolidBrush sbBrush(cLineColor[i]); // Außenlinie gGraphics.DrawEllipse(&pPen,iPosX,iPosY,iRadius[i],iRadius[i]); // Mittelpunkt gGraphics.FillEllipse(&sbBrush,(INT32) (iPosX + iRadius[i]/2 - iMidPointRadius/2), (INT32) (iPosY + iRadius[i]/2 - iMidPointRadius/2),iMidPointRadius,iMidPointRadius); } }; void WAVEPOINTER::AddCurve(INT32 iRadius, Gdiplus::Color cLineColor){ this->iRadius[iCurves] = iRadius; this->cLineColor[iCurves] = cLineColor; iCurves++; };
// main.cpp #pragma comment(lib, "gdiplus.lib") #include "stdafx.h" [b]WAVEPOINTER wpWavePointer(400,20,20,7,Color(0,0,255));[/b] //--------------------------------------------------------------------------- HWND hWnd; LPCTSTR ClsName = L"GDIFund"; LPCTSTR WindowCaption = L"GDI Fundamentals"; LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); //--------------------------------------------------------------------------- INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); MSG Msg; WNDCLASSEX WndClsEx; WndClsEx.cbSize = sizeof(WNDCLASSEX); WndClsEx.style = CS_HREDRAW | CS_VREDRAW; WndClsEx.lpfnWndProc = WndProc; WndClsEx.cbClsExtra = NULL; WndClsEx.cbWndExtra = NULL; WndClsEx.hInstance = hInstance; WndClsEx.hIcon = LoadIcon(hInstance, IDI_APPLICATION); WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW); WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); WndClsEx.lpszMenuName = NULL; WndClsEx.lpszClassName = ClsName; WndClsEx.hIconSm = LoadIcon(hInstance, IDI_APPLICATION); RegisterClassEx(&WndClsEx); hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, ClsName, WindowCaption, WS_OVERLAPPEDWINDOW, 100, 120, 640, 480, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); [b]wpWavePointer.AddCurve(100,Color(255,0,0));[/b] while( GetMessage(&Msg, NULL, 0, 0) ) { TranslateMessage(&Msg); DispatchMessage(&Msg); } GdiplusShutdown(gdiplusToken); return 0; } //--------------------------------------------------------------------------- LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { HDC hDC; PAINTSTRUCT ps; switch(Msg) { case WM_DESTROY: PostQuitMessage(WM_QUIT); break; case WM_PAINT: hDC = BeginPaint(hWnd, &ps); [b]wpWavePointer.Draw(hDC);[/b] EndPaint(hWnd,&ps); break; default: return DefWindowProc(hWnd, Msg, wParam, lParam); } return 0; }
Danke,
Thilo
-
Irgendwie sind die Arrays wieder zurückgesetzt, wenn Draw() aufgerufen wird...
-
Oh oh ich habs. AddCurve muss natürlich vor ShowWindow(). Danke ^^