How to fill an ellipse

This is a discussion on How to fill an ellipse within the Windows Programming forums, part of the Platform Specific Boards category; Apparently its not the same as to fill a rectangle and i cant find the right function. Thanks in advance! ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747

    How to fill an ellipse

    Apparently its not the same as to fill a rectangle and i cant find the right function.
    Thanks in advance!

    Code:
    #include <windows.h>
    #include <winuser.h>
    
    #define ID_TIMER    1
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    void DrawRectangle (HWND) ;
    void DrawEllipse (HWND) ;
    int cxClient, cyClient ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static TCHAR szAppName[] = TEXT ("Beeper1") ;
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASS     wndclass ;
         
         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = NULL ;
         wndclass.lpszClassName = szAppName ;
         
         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
         
         hwnd = CreateWindow (szAppName, TEXT ("Beeper1 Timer Demo"),
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              NULL, NULL, hInstance, NULL) ;
              
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
              
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
         HBRUSH      hBrush ;
         HDC         hdc ;
         PAINTSTRUCT ps ;
         RECT        rc ;
         
         switch (message)
         {
         case WM_SIZE:
              cxClient = LOWORD (lParam) ;
              cyClient = HIWORD (lParam) ;
              return 0 ;
         
         case WM_CREATE:
              SetTimer (hwnd, ID_TIMER, 2000, NULL) ;
              return 0 ;
    
         case WM_TIMER :
              InvalidateRect (hwnd, NULL, FALSE) ;     
              return 0 ;
              
         case WM_PAINT :
              hdc = BeginPaint (hwnd, &ps) ;
              
              GetClientRect (hwnd, &rc) ;
              hBrush = CreateSolidBrush (RGB(0,0,0)) ;
              FillRect (hdc, &rc, hBrush) ;
              DrawEllipse (hwnd) ;
              EndPaint (hwnd, &ps) ;
              DeleteObject (hBrush) ;
              return 0 ;
              
         case WM_DESTROY :
              KillTimer (hwnd, ID_TIMER) ;
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    void DrawEllipse (HWND hwnd)
    {
         HBRUSH hBrush ;
         HDC    hdc = NULL;
         PAINTSTRUCT ps;
         hdc= BeginPaint(hwnd, &ps);
         
         if (cxClient == 0 || cyClient == 0)
              return ;
         
         Ellipse (hdc,rand () % cxClient, rand () % cyClient,
                         rand () % cxClient, rand () % cyClient) ;
         
         hBrush = CreateSolidBrush (RGB (rand () % 256, rand () % 256, rand () % 256)) ;
         hdc = GetDC (hwnd) ;
         
         FillEllipse (hBrush,Ellipse) ;
         ReleaseDC (hwnd, hdc) ;
         DeleteObject (hBrush) ;
    }
    Compiler MSVC++ 2010 with Code::Blocks.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Ellipse:
    Quote Originally Posted by msdn
    The ellipse is outlined by using the current pen and is filled by using the current brush.
    For example:
    Code:
    HBRUSH hbr=CreateSolidBrush(RGB(255,0,0)); /*red brush*/
    HBRUSH hOld=(HBRUSH)SelectObject(hdc,hbr);
    Ellipse(hdc,10,10,200,200);
    SelectObject(hdc,hOld);
    DeleteObject(hbr);
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747
    Thank you Ken!
    It compiles now but wont draw any ellipse.
    What am i doing wrong?

    Code:
    void DrawEllipse (HWND hwnd)
    {
         HDC    hdc = NULL;     
         if (cxClient == 0 || cyClient == 0)
              return ;
         
         Ellipse (hdc,rand () % cxClient, rand () % cyClient,
                         rand () % cxClient, rand () % cyClient) ;
         
         HBRUSH hbr = CreateSolidBrush (RGB (rand () % 256, rand () % 256, rand () % 256)) ;
         HBRUSH hOld = (HBRUSH) SelectObject (hdc,hbr);
         hdc = GetDC (hwnd) ;
         SelectObject (hdc,hOld) ;
         ReleaseDC (hwnd, hdc) ;
         DeleteObject (hbr) ;
    }
    Compiler MSVC++ 2010 with Code::Blocks.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    The device context is NULL when you call Ellipse and the brush must be selected into the device context prior to calling Ellipse - ie you have the order wrong. You're also using GetDC unnecessarily. Just change your DrawEllipse function so it takes two parameters(HWND and HDC) and send the device context obtain from BeginPaint in your WM_PAINT handler as the second argument:
    Code:
    case WM_PAINT :
      hdc = BeginPaint (hwnd, &ps) ;
      /*...*/
      DrawEllipse (hwnd,hdc) ;
      /*...*/
      return 0 ;
    
    void DrawEllipse (HWND hwnd,HDC hdc) /*<-- dc sent to this function now*/
    {
      /*create and select gdi brush*/
      HBRUSH hbr = CreateSolidBrush (RGB (rand () % 256, rand () % 256, rand () % 256)) ;
      HBRUSH hOld = (HBRUSH) SelectObject (hdc,hbr);
      /*draw ellipse*/
      Ellipse (hdc,rand () % cxClient, rand () % cyClient,
               rand () % cxClient, rand () % cyClient) ;
      /*restore device context's original, default brush object*/
      SelectObject (hdc,hOld) ;
      /*free brush resources back to system*/
      DeleteObject (hbr) ;
    }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747
    Perfect, its working now!

    Thanks again Ken!
    Compiler MSVC++ 2010 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list fill the gaps question..
    By transgalactic2 in forum C Programming
    Replies: 2
    Last Post: 07-06-2009, 12:35 PM
  2. Question On how to Fill icmp_data
    By Antigloss in forum Linux Programming
    Replies: 3
    Last Post: 04-25-2007, 09:42 PM
  3. Replies: 0
    Last Post: 03-23-2006, 11:42 AM
  4. Quick fill question
    By Mr Moe in forum C++ Programming
    Replies: 5
    Last Post: 02-25-2005, 03:26 PM
  5. Replies: 3
    Last Post: 12-22-2004, 06:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21