GDI Rectangles

This is a discussion on GDI Rectangles within the Windows Programming forums, part of the Platform Specific Boards category; So I started messing around with GDI, and I created this magnificent piece of art . The green rectangle is ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    43

    GDI Rectangles

    So I started messing around with GDI, and I created this magnificent piece of art. The green rectangle is supposed to be located at 10 pixels from the borders at all sides. Here's my code for the rectangle displaying part:

    Code:
    RECT rc;
    HBRUSH fillColor = CreateSolidBrush ( RGB ( 0, 255, 0 ) );
    
    rc.left   = 10;
    rc.top    = 10;
    rc.right  = 490;
    rc.bottom = 490;
    
    FillRect ( hDc, &rc, fillColor );
    The size of the window is 500x500 pixels. Logically speaking, the upper-left corner of the rectangle should be at 10x10 pixels and the lower-right corner at 490x490 pixels, right? Somehow, that's not what is being displayed... Any help?

    Thanks.
    Last edited by Darklighter; 10-15-2006 at 03:55 PM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>The size of the window is 500x500 pixels.<<

    That includes the borders, caption, any menu etc. Use GetClientRect to get the client area dimensions and InflateRect to adjust the dimensions of your square
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    43
    That worked fine. Thanks!

    This is my code (I found this clearer than InflateRect):

    Code:
    RECT rc;
    HBRUSH fillColor = CreateSolidBrush ( RGB ( 0, 255, 0 ) );
    
    GetClientRect ( hWnd, &rc ); 
    
    rc.left   += 10;
    rc.top    += 10;
    rc.right  -= 10;
    rc.bottom -= 10;
    
    FillRect ( hDc, &rc, fillColor );

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    With InflateRect:
    Code:
    RECT rc;
    HBRUSH fillColor = CreateSolidBrush ( RGB ( 0, 255, 0 ) );
    
    GetClientRect ( hWnd, &rc ); 
    
    InflateRect(&rc,-10,-10);
    
    FillRect ( hDc, &rc, fillColor );
    But whatever you are comfortable using is probably best.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows GDI vs. Java Graphics
    By Perspective in forum Windows Programming
    Replies: 7
    Last Post: 05-07-2007, 11:05 AM
  2. GDI object lifetime and C++ object lifetime
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 06-16-2006, 06:26 AM
  3. Difference Of Rectangles
    By SMurf in forum C Programming
    Replies: 2
    Last Post: 09-14-2005, 02:12 PM
  4. Whats Peoples Problem With the GDI?
    By MicroFiend in forum Game Programming
    Replies: 6
    Last Post: 07-28-2003, 08:52 PM
  5. Double buffering in GDI -- easy?
    By fusikon in forum Game Programming
    Replies: 17
    Last Post: 02-15-2003, 10:03 PM

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