CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Threaded View

  1. #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.

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