:
: Thanks AsmGuru62, made changes now the line rotates but the
: circle'still flickering... Someone suggested that i must create a
: Compatible DC then draw the circle and the line on it!... This will
: avoid flicking, could that be true?
:
Yes. Drawing in memory DC and then using BitBlt() to instantly draw in one shot is the way to make games and other draw-intensive stuff. In addition to this make a change shown in RED below. It reduces flicker too, but then you need to draw what is around the circle yourself.
:
:
: // Rotate.cpp : Defines the entry point for the console application.
: //
:
: #include "stdafx.h"
: #include <windows.h>
: #include <math.h>
:
: #pragma hdrstop
:
: #define IDT_ROTATER 101
:
: //---------------------------------------------------------------------------
: const char *ClsName = "RADAR_01";
: const char *WndName = "Rotate Line";
:
: POINT SetPOINT(LONG radius);
: long ErrorHandler(char errorMessage[]);
: void Win32APIGDICircle (HDC hdc, LONG radius, int R, int G, int B);
: LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,WPARAM wParam, LPARAM lParam);
:
: // Globals
: int angle=0;
: HICON hIcon1;
: POINT ptOld;
: UINT uResult;
: POINT coord, ptCenter = {450,300};
:
: //---------------------------------------------------------------------------
: INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
: {
: MSG Msg;
: HWND hWnd;
: WNDCLASSEX WndClsEx;
:
: // Create the application window
: WndClsEx.cbSize = sizeof(WNDCLASSEX);
: WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
: WndClsEx.lpfnWndProc = WndProcedure;
: WndClsEx.cbClsExtra = 0;
: WndClsEx.cbWndExtra = 0;
: WndClsEx.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(400));
: WndClsEx.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(200));
: WndClsEx.hbrBackground = NULL;
: WndClsEx.lpszMenuName = NULL;
: WndClsEx.lpszClassName = ClsName;
: WndClsEx.hInstance = hInstance;
: WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
:
: // Register the application
: RegisterClassEx(&WndClsEx);
:
: // Create the window object
: hWnd = CreateWindowEx(0,
: ClsName,
: WndName,
: WS_OVERLAPPEDWINDOW,
: CW_USEDEFAULT,
: CW_USEDEFAULT,
: CW_USEDEFAULT,
: CW_USEDEFAULT,
: NULL,
: NULL,
: hInstance,
: NULL);
:
: // Find out if the window was created
: if( !hWnd ) // If the window was not created,
: return FALSE; // stop the application
:
: // Display the window to the user
: ShowWindow(hWnd, nCmdShow);
: UpdateWindow(hWnd);
:
: // Decode and treat the messages
: // as long as the application is running
: while( GetMessage(&Msg, NULL, 0, 0) )
: {
: TranslateMessage(&Msg);
: DispatchMessage(&Msg);
: }
:
: return Msg.wParam;
: }
:
: //---------------------------------------------------------------------------
: LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
: WPARAM wParam, LPARAM lParam)
: {
: PAINTSTRUCT ps;
: HPEN pen, oldPen;
: HDC hMemDC;
: long radius=150;
:
: double xLimit = sqrt ((double) (radius * radius) / 2);
:
: // Handle messagse
: switch(Msg)
: {
: case WM_CREATE:
:
: // Set the timer
: uResult = (UINT)SetTimer(hWnd, IDT_ROTATER, 0.025,(TIMERPROC) NULL);
:
: break;
:
: case WM_PAINT:
: {
: hdc = BeginPaint(hWnd, &ps);
:
: //hdc = CreateCompatibleDC(hWnd);
:
: // Draw a circle
: Win32APIGDICircle (hdc, radius, 0, 255, 128);
:
: // Create pen
: pen = CreatePen(PS_SOLID, 2, RGB(128, 255, 128));
: oldPen = (HPEN)SelectObject(hdc, pen);
:
: // Draw line
: SetPOINT(radius);
: MoveToEx(hdc, ptCenter.x,ptCenter.y, NULL);
: LineTo(hdc, coord.x, coord.y);
:
: // Delete & Release DCs
: SelectObject(hdc, oldPen);
: DeleteObject(pen);
: EndPaint(hWnd, &ps);
: }
: break;
:
: case WM_TIMER:
: angle-=2;
: InvalidateRect (hWnd, NULL, TRUE);
: UpdateWindow(hWnd);
:
: break;
:
: case WM_DESTROY:
: KillTimer(hWnd, IDT_ROTATER);
: PostQuitMessage(WM_QUIT);
: break;
:
: default:
: // Process the left-over messages
: return DefWindowProc(hWnd, Msg, wParam, lParam);
: }
: // If something was not done, let it go
: return 0;
: }
: // Win32API GDI circle:
: void Win32APIGDICircle (HDC hdc, LONG radius,int R, int G, int B)
: {
: /*SelectObject (hdc, GetStockObject (DC_BRUSH));
: SetDCBrushColor(hdc,RGB(R,G,B));*/
:
: HBRUSH hBrush = CreateSolidBrush (RGB (R,G,B));
: HGDIOBJ hOldBrush = SelectObject (hdc, hBrush);
:
: Ellipse (hdc,
: ptCenter.x - radius,
: ptCenter.y + radius,
: ptCenter.x + radius,
: ptCenter.y - radius);
:
: SelectObject (hdc, hOldBrush);
: DeleteObject (hBrush);
: }
:
: /* Calculate the line points */
: POINT SetPOINT(LONG radius)
: {
: const float Deg2Rad = 0.017453292;
: float degInRad;
:
: degInRad = angle*Deg2Rad; // Convert degrees to radians
:
: // Finds the adjacent value
: coord.x = (long)(ptCenter.x + (cos(degInRad)*(float)radius));
:
: // Finds the hypotenuse value
: coord.y = (long)(ptCenter.y - (sin(degInRad)*(float)radius));
:
: return coord;
: }
: :