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

Thread: Line Drawing

  1. #1
    Join Date
    Apr 2005
    Posts
    1,828

    Line Drawing

    I want to draw vertical and horizontal straight lines in WM_PAINT, How can I do it useing CDC?
    Last edited by maverick786us; February 24th, 2010 at 05:25 AM.

  2. #2
    Join Date
    Apr 2009
    Posts
    598

    Re: Line Drawing

    CDC is for MFC if I am not wrong, and WM_PAINT is for programs that don't use MFC, if I am not wrong.

    Here is a solution, not for MFC:
    Code:
    int paint_line(HWND hwnd, HDC hdc, int x0, int y0, int x9, int y9, int thickness, COLORREF rgb)
    {
       HBRUSH hbr; HPEN hpen, hpen_sav;
    
       // int rop2_sav;
       // rop2_sav = SetROP2(hdc, R2_NOTXORPEN);
    
       hpen = CreatePen(PS_SOLID, thickness, rgb);
       hpen_sav = (HPEN) SelectObject(hdc, hpen);
       hbr = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH));
    
       MoveToEx(hdc, x0, y0, NULL);
       LineTo(hdc, x9, y9);
       DeleteObject(SelectObject(hdc, hbr));
       SelectObject(hdc, hpen_sav);
       DeleteObject(hpen);
    
       return 0;
    }

  3. #3
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Line Drawing

    Sorry I ean OnDraw. The way you can draw Rectangle and Ellipse, I want to draw lines

  4. #4
    Join Date
    Feb 2010
    Posts
    30

    Re: Line Drawing

    MoveTo(x,y) moves the pen to position and LineTo(x,y) draws the line from MoveTo position to LineTo position

  5. #5
    Join Date
    Feb 2010
    Posts
    30

    Re: Line Drawing

    this code draws grey lines,horizontal and vertical in center of screen
    Code:
    OnDraw()
    { 
           CRect rect;
    	GetClientRect(rect);
    	CPen penGrey;  // Construct it, then initialize
    		if( penGrey.CreatePen( PS_SOLID, 0, RGB(200,200,200) ) )
    		{
    			// Select it into the device context
    			// Save the old pen at the same time
    			CPen* pOldPen = pDC->SelectObject( &penGrey );
    
    			// Draw with the pen
    			//draws axis
    			pDC->MoveTo(rect.right/2,rect.top);
    			pDC->LineTo(rect.right/2,rect.bottom);
    			pDC->MoveTo(rect.right,rect.bottom/2);	
    			pDC->LineTo(rect.left,rect.bottom/2);
    
    			// Restore the old pen to the device context
    			pDC->SelectObject( pOldPen );
    		}
    		else
    		{
    			MessageBox("Resources are running low\nPlease close one or more                     applications");		
    		}
    }

  6. #6
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Line Drawing

    MSDN knows everything about the CDC object.

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