Drawing Something To Screen

I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <windows.h>
#include "wtypes.h"
#include <iostream>

const char g_szClassName[] = "testWindowClass";


// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_CLOSE:
		DestroyWindow(hwnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd, msg, wParam, lParam);
	}
	return 0;
}

DWORD WINAPI MyThread(LPVOID lpParameter)
{
	HDC hdc;
	SIZE s;
	s.cx = ::GetSystemMetrics(SM_CXSCREEN);
	s.cy = ::GetSystemMetrics(SM_CYSCREEN);
	int x, y, z, r, g, b;
	int horizontal = 0;
	int vertical = 0;
	RECT desktop;
	// Get a handle to the desktop window
	const HWND hDesktop = GetDesktopWindow();
	// Get the size of screen to the variable desktop
	GetWindowRect(hDesktop, &desktop);
	// The top left corner will have coordinates (0,0)
	// and the bottom right corner will have coordinates
	// (horizontal, vertical)
	horizontal = desktop.right;
	vertical = desktop.bottom;
	HBRUSH hbr, hbrOld;
	x = horizontal / 4; //s.cx * rand() / RAND_MAX;  // position x
	y = vertical / 4; //s.cy * rand() / RAND_MAX;  // position y
	z = 30; // radius
	r = 255; // red color componennt
	g = 200;// green color component
	b = 0;// blue color component
	hbr = ::CreateSolidBrush(RGB(r,g,b));
	hdc = ::GetDCEx(NULL, 0, 0);
	hbrOld = (HBRUSH) ::SelectObject(hdc, hbr);
	::Ellipse(hdc, x - z, y - z, x + z, y + z);
	::SelectObject(hdc, hbrOld);
	::DeleteObject(hbr);
	::ReleaseDC(NULL, hdc);
	::Sleep(20);
	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine, int nCmdShow)
{
	::CreateThread(0, 0, MyThread, 0, 0, 0);
	WNDCLASSEX wc;
	HWND hwnd;
	MSG Msg;

	//Step 1: Registering the Window Class
	wc.cbSize        = sizeof(WNDCLASSEX);
	wc.style         = 0;
	wc.lpfnWndProc   = WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hInstance;
	wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = g_szClassName;
	wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Cannot Register Window!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	// Step 2: Creating the Window
	hwnd = CreateWindowEx(
		WS_EX_CLIENTEDGE,
		g_szClassName,
		"Test Window",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
		NULL, NULL, hInstance, NULL);

	if(hwnd == NULL)
	{
		MessageBox(NULL, "Cannot Create Window!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	// Step 3: The Message Loop
	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return Msg.wParam;
}
It does everything I want it to except, when I minimize then maximize a window it will disappear (even while I have the application running, not that I would want it to stay after I close it down but just to tell you I DO have it running). I have tried removing the 'return 0;' at the end of the function but then I get an error saying that the function needs to return a value. I have also tried removing '::SelectObject(hdc, hbrOld);', '::DeleteObject(hbr);' and '::ReleaseDC(NULL, hdc);'. I got no errors but it still disappeared after I put a window on top of it. How could I get the yellow circle to stay even after I move a window on top of it?
Last edited on
Well, that is easy enough (note that the circle will appear above every other thing on the screen)

1
2
3
4
5
6
7
8
9
//previous code
hbrOld = (HBRUSH) ::SelectObject(hdc, hbr);
while (true)
{
     ::Ellipse(hdc, x - z, y - z, x + z, y + z);
     ::Sleep(10);
}
::SelectObject(hdc, hbrOld);
//other code 
This:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <windows.h>
#include "wtypes.h"
#include <iostream>

const char g_szClassName[] = "testWindowClass";


// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_CLOSE:
		DestroyWindow(hwnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd, msg, wParam, lParam);
	}
	return 0;
}

DWORD WINAPI MyThread(LPVOID lpParameter)
{
	HDC hdc;
	SIZE s;
	s.cx = ::GetSystemMetrics(SM_CXSCREEN);
	s.cy = ::GetSystemMetrics(SM_CYSCREEN);
	int x, y, z, r, g, b;
	int horizontal = 0;
	int vertical = 0;
	RECT desktop;
	// Get a handle to the desktop window
	const HWND hDesktop = GetDesktopWindow();
	// Get the size of screen to the variable desktop
	GetWindowRect(hDesktop, &desktop);
	// The top left corner will have coordinates (0,0)
	// and the bottom right corner will have coordinates
	// (horizontal, vertical)
	horizontal = desktop.right;
	vertical = desktop.bottom;
	HBRUSH hbr, hbrOld;
	x = horizontal / 4; //s.cx * rand() / RAND_MAX;  // position x
	y = vertical / 4; //s.cy * rand() / RAND_MAX;  // position y
	z = 30; // radius
	r = 255; // red color componennt
	g = 200;// green color component
	b = 0;// blue color component
	hbr = ::CreateSolidBrush(RGB(r,g,b));
	hdc = ::GetDCEx(NULL, 0, 0);
	hbrOld = (HBRUSH) ::SelectObject(hdc, hbr);
	while (true)
	{
		::Ellipse(hdc, x - z, y - z, x + z, y + z);
		::Sleep(10);
	}
	::SelectObject(hdc, hbrOld);
	::DeleteObject(hbr);
	::ReleaseDC(NULL, hdc);
	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine, int nCmdShow)
{
	::CreateThread(0, 0, MyThread, 0, 0, 0);
	WNDCLASSEX wc;
	HWND hwnd;
	MSG Msg;

	//Step 1: Registering the Window Class
	wc.cbSize        = sizeof(WNDCLASSEX);
	wc.style         = 0;
	wc.lpfnWndProc   = WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hInstance;
	wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = g_szClassName;
	wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Cannot Register Window!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	// Step 2: Creating the Window
	hwnd = CreateWindowEx(
		WS_EX_CLIENTEDGE,
		g_szClassName,
		"Test Window",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
		NULL, NULL, hInstance, NULL);

	if(hwnd == NULL)
	{
		MessageBox(NULL, "Cannot Create Window!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	// Step 3: The Message Loop
	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return Msg.wParam;
}
Still doesn't work. To me it looked like it would've worked too but still when I drag a window on top of it it disappears.
Last edited on
Aha, I had a brainwave.

It works on Windows 7 but not on Windows XP.

I have Windows Vista, lol.
Topic archived. No new replies allowed.