I’m having a bit of a hard time figuring out, how to remove a drawn ellipse after it has been drawn somewhere else. I need a circle to follow my mouse all the time and this is all the program should do. I get the mousepositions and draw my circle but how can I remove the last one?
#include <Windows.h>
#include <iostream>
void drawRect(int a1, int a2){
HDC screenDC = ::GetDC(0);
//Draw circle at mouse position
::Ellipse(screenDC, a1, a2+5, a1+9, a2+14);
::ReleaseDC(0, screenDC);
//::InvalidateRect(0, NULL, TRUE); //<- I tried that but then everything flickers
//Also, the refresh rate is not fast enough... still some circles left
}
int main(void)
{
int a1;
int a2;
bool exit=false;
while (exit!=true)
{
POINT cursorPos;
GetCursorPos(&cursorPos);
float x = 0;
x = cursorPos.x;
float y = 0;
y = cursorPos.y;
a1=(int)cursorPos.x;
a2=(int)cursorPos.y;
drawRect(a1, a2);
}
}
You’d better use transparent window above all over the screen. This will be much easier. Windows is not designed to be acting the way you’ve just described. For optimizing the speed you have two ways:
CreateCompatibleDC. In this way you can first prepare your image and then quickly draw it instead of your window’s DC.Also note, that you should implement a hook over WM_MOUSEMOVE messages in order to receive them. The program with the loop will eat 99% of processor time for nothing. Look MSDN for mouse hooks.
Ok, this will be WinAPI. Hope, you know how to write a WinAPI application basic stuff like message cycle and other. In any case you can use Visual Studio template for WinAPI applications. I’ll do so.
First, remove the uninteresting code concerning About dialog and the staff (you can skip it, if you don’t know what to do). Next, you should create your window:
Update the
MyRegisterClassfunction. Replacewcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_…);
with
Update the
InitInstancefunction. ReplacehWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
with
Add following lines of code just after the hWnd is checked for consistency:
Replace
with
Now, implement the drawing in
WM_PAINTsection ofWndProc.hdc = BeginPaint(hWnd, &ps);
POINT ptNew;
GetCursorPos(&ptNew);
HBRUSH hbr = CreateSolidBrush(RGB(255, 255, 255));
HBRUSH hold = (HBRUSH)SelectObject(hdc, hbr);
Ellipse(hdc, ptNew.x + 15, ptNew.y + 15, ptNew.x + 30, ptNew.y + 30);
SelectObject(hdc, hold);
DeleteObject(hbr);
ptOld = ptNew;
EndPaint(hWnd, &ps);
Will continue with hooking tomorrow. Today is too late. Or, look at this article manually.