drawing to a window using GDI not working?

i'm trying to draw a rectangle on top of a window using the GDI.

i have the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <Windows.h>
#include <iostream>

int main()
{

	LPCSTR wndName = "name";

	HWND hwnd = FindWindow(NULL,wndName);

	if (hwnd == NULL)
		std::cout << "didn't find window" << std::endl;

	HDC hdc = GetDC(hwnd);
	
	Rectangle(hdc, 0,0,100,100);
	DeleteDC(hdc);

	
	system("pause");
	return 0;
}


everything works fine until i try to draw on an actual window..

if i specify NULL as the wndName it works and i see a rectangle being draw over my screen just as expected.

but as soon as i try to draw on top of an actual window, i dont see anything?

what am i doing wrong?

Last edited on
oh wait....
it actually works on some programs such as notepad but not games...

i assume thats because the game i was trying to draw on top of was updating its window every time it redraws the game right?

how do i go about drawing on top of it then? so my rectangle doesn't disappear when the window gets redrawn?
You can try to paint outside the client area with the GetWindowDC function.
Topic archived. No new replies allowed.