CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2010
    Location
    Udeliketano
    Posts
    44

    Draw Something To Screen

    How would you draw something to your screen? Not in one specific window that you make but, on top of everything you have currently open? Like for example a dot or circle?

  2. #2
    Join Date
    Nov 2007
    Posts
    613

    Re: Draw Something To Screen

    Call GetDCEx with hwnd = NULL. You'll get a HDC for the entire screen. You'll be able to draw anywhere on the screen regardless what windows are there.

  3. #3
    Join Date
    Apr 2010
    Location
    Udeliketano
    Posts
    44

    Re: Draw Something To Screen

    Sorry I am pretty new to win32 could you please elaborate?

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Draw Something To Screen


  5. #5
    Join Date
    Nov 2007
    Posts
    613

    Re: Draw Something To Screen

    Here's a sample that uses a thread.

    Put the following function in any Win32 application:
    Code:
    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;
      HBRUSH hbr, hbrOld;
      while(TRUE)
      {
        x = s.cx * rand() / RAND_MAX; // position x
        y = s.cy * rand() / RAND_MAX; // position y
        z = 100 * rand() / RAND_MAX; // radius
        r = 255 * rand() / RAND_MAX; // red color componennt
        g = 255 * rand() / RAND_MAX;// green color component
        b = 255 * rand() / RAND_MAX;// 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);
      }
    }
    You start the thread using the following code:
    Code:
    ::CreateThread(0, 0, MyThread, 0, 0, 0);
    This line can be antwhere in the Win32 application, just make sure it's in a place where it will be excuted exactly once (you can place it in the InitInstance function).

    It will fill the whole screen with colorfull circles.
    Last edited by srelu; April 23rd, 2010 at 10:58 AM.

  6. #6
    Join Date
    Apr 2010
    Location
    Udeliketano
    Posts
    44

    Re: Draw Something To Screen

    That will draw on my desktop not just one specific window?

  7. #7
    Join Date
    Nov 2007
    Posts
    613

    Re: Draw Something To Screen

    Quote Originally Posted by serpentinez View Post
    That will draw on my desktop not just one specific window?
    I repeat: "It will fill the *W.H.O.L.E. S.C.R.E.E.N.* with colorful circles".

    Instead of asking, why don't you try it ?
    Last edited by srelu; April 23rd, 2010 at 04:41 PM.

  8. #8
    Join Date
    Apr 2010
    Location
    Udeliketano
    Posts
    44

    Re: Draw Something To Screen

    Yay, it works! Thank you soo much!!!
    BTW I wasn't home so I just wanted to make sure! Thanks again!
    EDIT: Okay, now I have this code:
    Code:
    #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;
    }
    But every time I minimize then maximize a window the yellow circle disappears even while I have the application running (not that I would want it to stay after I close it but just to tell you that I am running the application). I have tried getting rid of the 'return 0;' at the end of the function but then I get an error saying that the function needs to return a value. Could anyone help please?
    Last edited by serpentinez; April 25th, 2010 at 09:38 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured