Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7765923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:17:05+00:00 2026-06-01T15:17:05+00:00

Unless I make static CPaintDC dc(this); the line won’t draw? But this is not

  • 0

Unless I make “static CPaintDC dc(this);” the line won’t draw? But this is not good as it will eventually error, also the graphics wont’ keep on the screen.

Not sure what I am doing wrong

Note: I have a Timer that calls to this every 100ms(x and y are incremented)
thx

void CGraphicsDlg::OnPaint() 
{
    CString s;
    CPaintDC dc(this);// device context for painting

    if (IsIconic())
    {
        SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else if(x==0)
    {
        s.Format("%d", x);
        edXa->SetWindowText(s);

        dc.MoveTo(20,400);
    }
    else if (x>0){
        s.Format("%d", x);
        edXb->SetWindowText(s);

        dc.LineTo(20 + x, 40);  // doesn't draw unless I make "static CPaintDC dc(this);" <- which will error out 
    }
    CDialog::OnPaint();
}

void CGraphicsDlg::OnTimer(UINT nIDEvent)
{
    if(nIDEvent==1){
        srand( (unsigned)time( NULL ) );

        //y = rand() % 100;
        y++;
        x++;

        OnPaint();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:17:06+00:00Added an answer on June 1, 2026 at 3:17 pm

    LineTo draws a line from one point to another, using the selected pen. You need to use MoveTo to define the start of the line, and you need to select a pen into the DC.

    The larger problem is how you’re trying to use the DC. It isn’t meant to be permanent; you’re supposed to acquire it, draw everything to it, then shut it down. When you try to make CPaintDC static, Windows will eventually shut it down and every attempt to use it thereafter will return an error.

    The proper way is to save any coordinates that you need for all of the drawing you need to do. Use a combination of MoveTo and LineTo to draw individual line segments, and every time you reenter OnPaint you need to start over.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This will sound silly, but trust me it is for a good (i.e. over-engineered)
I know that read() is a blocking call unless I make the socket non-blocking.
Unless I'm mistaken, creating a function in Python works like this: def my_func(param1, param2):
I hope you will help me understand this Copy Constructor I took more then
I am trying to make a jQuery get() AJAX call, but the response is
First of all this is NOT an exact duplicate of Initialize final variable before
I know this topic has been discussed and killed over and over again, but
I have this simple method of deleting list of files. But for some cases
sorry if this question is stupid, but I can't wrap my head around Java
I'm trying to make a registration page but PHP is telling me that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.