Thread: MFC question drawing a LINE

  1. #1
    Registered User noririco's Avatar
    Join Date
    Nov 2013
    Posts
    90

    MFC question drawing a LINE

    Hi, I need to draw a line (dynamicly).

    now so far I use CBrush and Cpen to draw Rectangles and stuff, but someone told me I should use
    HBRUSH
    HGDIOBJ

    why is that? what is so special bout drawing a line.?

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hello, I don't know MFC but seeing some net samples I think it will be similar except by calling the object class (and I suppose more other differences of course), but I think you'll find it usefull.

    To draw a line using LineTo you have to set previously a pen or a brush; from the reference of LineTo function:

    Code:
    ...
    The line is drawn by using the current pen and, if the pen is a geometric pen, the current brush.
    ...
    So if you want to select a pen or a brush you use SelectObject(HDC , HGDIOBJ ) where HGDIOBJ can be several types (bitmap, brush, pen, region and font); so if you have a pen you simply set it to the hdc.

    In the other hand you have to create a pen (or a brush, or a font) or you have the option to get it from the default set of stock objects through the GetStockObject. This function returns a HGDIOBJ althought you can request several object types.

    Code:
    HPEN hpen = (HPEN)GetStockObject(BLACK_PEN);
    HPEN holdpen = (HPEN)SelectObject(hdc, hpen);
    
    or
    HBRUSH hbrush = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
    HBRUSH holdbrush = (HBRUSH)SelectObject(hdc, hbrush);
    This works because HGDIOBJ is defined as a void pointer (void *), so you can typecast to other object types.

    I don't know MFC, but in the 'standard' application procedures you have to catch the WM_PAINT in order to draw (ok, not only the WM_PAINT message is the only way to draw), so on the application load you create the brush (or the pen) or you acquire it from the stock objects, on the paint message you use it and on the unload you destroy it (if created). On the paint when you select an object the function returns the current object of the same type (which you are about to replace. So once you finished to do the drawing operations you have to restore the old objects.

    A very simple lines of code to show what I was trying to explain :

    Code:
    /**
    * global variables
    */
    HBRUSH hRedBrush;
    
    
    /**
    * on loading application
    */
    hRedBrush = CreateSolidBrush(0x000000ff);
    
    
    /**
    * on paint
    */
    PAINTSTRUCT ps;
    HDC hdc;
    HBRUSH hPrevBrush;
    
    hdc = BeginPaint(hwnd, &ps);
    
    //select the brush for the line
    hPrevBrush = (HBRUSH)SelectObject(hdc, hRedBrush);
    
    //place line start line and draw it
    MoveToEx(hdc, 0, 0, 0);
    LineTo(hdc, 10, 10);
    
    //restore brush
    SelectObject(hdc, hPrevBrush);
    
    EndPaint(hwnd, &ps);
    
    
    /**
    * on unload application
    */
    DeleteObject(hRedBrush);
    As I comment I don't know MFC, but I seen some examples on the net about MFC and the functions are the same, except they are inse the object class (like CBrush::CreateSolidBrush()).

    Hope that helps

    Regards
    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing a line
    By Homunculus in forum Windows Programming
    Replies: 6
    Last Post: 03-19-2006, 04:32 PM
  2. Drawing a line
    By lambs4 in forum Game Programming
    Replies: 0
    Last Post: 02-15-2003, 12:42 PM
  3. Need help with drawing a line
    By dv007 in forum C Programming
    Replies: 3
    Last Post: 06-25-2002, 01:10 PM
  4. Question About Line Drawing with Messaging
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 06-06-2002, 11:47 PM
  5. Drawing a line or somethink like that...
    By Unregistered in forum Game Programming
    Replies: 3
    Last Post: 05-06-2002, 12:22 PM