CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2010
    Posts
    4

    [MFC] Refreshing Picture Control

    Hello,
    I have a problem. I'm trying to create an application, which shows a rectangle on a grid. The position of the rectangle is subject to coordinates given from GPS simulator. The application is based on a single dialog window which involves a Picture Control. I set a type of that as Owner Draw. Then I created a class CPict, based on CStatic. CPict involves the OnPaint() method. Then I created a control variable of Picture Control. It's a CPict type variable named m_pict. In the class CGPSDlg there is the OnRead() method. To invoke that method, the user have to click a button. My question is: how to refresh the content of the Picture Control in OnRead() method. Here's some code:

    Code:
    void CGPSDlg::OnRead()
    {
            CComPort* pComPort = new CComPort(g_sComPort);
            if (pComPort->Initialize())
            {
                    pComPort->Read(m_sResults);
                    m_sLatitude = m_sResults;
                    m_sLatitude.Delete(0,19);
                    m_sLatitude.Delete(2,50);
                    m_nLatitude = atoi(m_sLatitude);
                    pComPort->Terminate();
            }
            else
            {
                    m_sResults = "Setup failed";
            }
            UpdateData(FALSE);
    }
    Code:
    void CPict::OnPaint()
    {
            CPaintDC dc(this);
            CDC mdc;
            mdc.CreateCompatibleDC(&dc);
            CBitmap mBitmap;
            CBitmap* pOldMap;
            mBitmap.CreateCompatibleBitmap(&dc,360,180);
            pOldMap = mdc.SelectObject(&mBitmap);
            CBrush* pOldBrush;
            CPen* pOldPen;
            CBrush aWhiteBrush;
            aWhiteBrush.CreateSolidBrush(RGB(255,0,0));
            CPen BlackPen(PS_SOLID,1,RGB(255,255,255));
            pOldPen = mdc.SelectObject(&BlackPen);
            pOldBrush = mdc.SelectObject(&aWhiteBrush);
            mdc.SetMapMode(MM_ANISOTROPIC);
            CGPSDlg m_bridge;
            y=m_bridge.m_nLatitude;       
            for(int i=1;i<36;i++){
                    mdc.MoveTo(i*10,0); mdc.LineTo(i*10,180);}
            for(int i=1;i<18;i++){
                    mdc.MoveTo(0,i*10); mdc.LineTo(360,i*10);}
            CString str;
            str = m_bridge.m_sResults;
            mdc.Rectangle(10,y-3,16,y+3);
            mdc.TextOutA(10,10,str);
            dc.BitBlt(0,0,500,360,&mdc,0,0,SRCCOPY);
    }
    Last edited by raphalski; July 28th, 2010 at 05:32 AM.

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

    Re: [MFC] Refreshing Picture Control

    Please use code tags for posting readable code.

    You can use Invalidate to force a window to redraw itself.

    Some notes on your code.

    In the OnRead...
    Code:
    CComPort* pComPort = new CComPort(g_sComPort);
    This is never freed, so you have a memory leak.

    In the OnPaint...
    Code:
    CGPSDlg m_bridge;
    y=m_bridge.m_nLatitude;
    You are creating a new window in the OnPaint. This will slow down your paint, because creating a new window is relatively a intensive job, and the OnPaint can be triggered a lot of times without you knowing it. Also, the class is new, so the m_nLatitude has it's default value, not the value you put in there during the OnRead function.

  3. #3
    Join Date
    Jan 2010
    Posts
    4

    Re: [MFC] Refreshing Picture Control

    Then how to transfer values from OnRead() to update them in OnPaint() ?

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

    Re: [MFC] Refreshing Picture Control

    You can pass the pointer of 1 class to another.

    Something like this...
    Code:
    class A
    {
    public:
    
       int MyVariable;
    
    };
    
    
    
    class B
    {
    public:
    
      A *MyA;
      
      void SomeFunction ()
      {
          MyA->MyVariable = 10;
      }
    
    };
    
    
    
    int main ()
    {
      A varA;
      B varB;
    
      varB.MyA = &varA;                 // set the pointer of varA
      varB.SomeFunction ();             // this function uses the varA-pointer to set MyVariable
    
    };
    Last edited by Skizmo; July 28th, 2010 at 06:29 AM.

Tags for this Thread

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