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

Thread: Basic Graphics Drawing problem using OnPaint

  1. #1
    Join Date
    Mar 2008
    Posts
    21

    Basic Graphics Drawing problem using OnPaint

    Hello Gurus!

    I'm new using VS2012 trying to understand graphics programming. I have an example project given to me that successfully loads a dialog form and draws rectangles, lines, text etc. I made my own copy of that form and the code that goes with it as a learning exercise and tried to use it to create my own graphics. I couldn't get it to work so I stripped it down to just a small program that attempts to draw a simple line. Easy, right? I can't get my simple line to appear. I've compared the code for both but see no fundamental difference and can't understand why my attempts at drawing anything do not appear. I know it executes my drawing command because it will hit a break point set there, but no graphical elements have ever appeared on my form. Note in the code below, 'IDC_Lin_UpLeft' is the ID of a text control located in the upper left of the form. Any ideas? Thanks, Tim


    Here's the relevant parts of my code:

    Code:
    BEGIN_MESSAGE_MAP(CCalLinDialog, CDialog)
        ON_BN_CLICKED(IDCANCEL, &CCalLinDialog::OnBnClickedCancel)
        ON_BN_CLICKED(IDC_ATTN_SEL_ALL, &CCalLinDialog::OnBnClickedAttnSelAll)
        ON_BN_CLICKED(IDC_INIT_GPIB, &CCalLinDialog::OnBnClickedInitGpib)
        ON_WM_PAINT()
    END_MESSAGE_MAP()
    
    void CCalLinDialog:: OnPaint() 
    {
        CPaintDC dc(this); // device context for painting
        drawPlot();
    }
    
    void CCalLinDialog::drawPlot()
    {
    	CWnd* pULWnd = GetDlgItem(IDC_Lin_UpLeft);
    	CDC* pControlDC = pULWnd->GetDC();
    
    	CPen bound(PS_SOLID,5,RGB(150,150,150));
    	pControlDC->SelectObject(&bound);
    	pControlDC->MoveTo(10, 10); 
    	pControlDC->LineTo(200, 200);
    
    	pULWnd->ReleaseDC(pControlDC);
    }
    Last edited by 2kaud; March 28th, 2019 at 11:23 AM. Reason: Added code tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,452

    Re: Basic Graphics Drawing problem using OnPaint

    You have to use the device context that CPaintDC creates for you!
    So do it somehow like:
    Code:
    void CCalLinDialog:: OnPaint() 
    {
        CPaintDC dc(this); // device context for painting
        drawPlot(&dc);
    }
    
    void CCalLinDialog::drawPlot(CPaintDC* dc)
    {
    	CPen bound(PS_SOLID,5,RGB(150,150,150));
    	dc->SelectObject(&bound);
    	dc->MoveTo(10, 10); 
    	dc->LineTo(200, 200);
    }
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2008
    Posts
    21

    Re: Basic Graphics Drawing problem using OnPaint

    That worked, thank you Victor!
    But shouldn't my call to GetDC have given me the handle I need?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,452

    Re: Basic Graphics Drawing problem using OnPaint

    Quote Originally Posted by tboygee View Post
    That worked, thank you Victor!
    But shouldn't my call to GetDC have given me the handle I need?
    You are welcome!
    And, please, have a look at this thread.
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2008
    Posts
    21

    Re: Basic Graphics Drawing problem using OnPaint

    Thanks Victor, I learned a lot form that thread and missed it in my original attempts to search the forum. I'll also add that I've learned a great deal from your many posts in the past and truly appreciate the time you donate and your willingness to share.

    Just as a follow up from my post, I figured out that the reason it would not draw any objects was because the text control I was using as my anchor (IDC_Lin_UpLeft) had its invisible property set to TRUE. I thought I needed it invisible so it wouldn't show on the drawing. With it invisible no error was given - it just refused to show my graphical objects. When I changed it to FALSE everything worked properly.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,452

    Re: Basic Graphics Drawing problem using OnPaint

    Thank you for your feedback!
    And I am glad you could solve your problem!
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)