Search code examples
c++wxwidgets

i get a strange thing in wxPaintDC drawing


I have a some code relating wxpaintDC drawing.

I have thus a class relating some drawing:

IMPLEMENT_CLASS(MapView, wxWindow)

BEGIN_EVENT_TABLE(MapView, wxWindow)
EVT_PAINT(MapView::OnPaint)
END_EVENT_TABLE() 

void MapView::OnPaint(wxPaintEvent & event)
{

    wxPaintDC dc(this);
    wxRect m_Rect = this->GetRect(); //get the rect

    wxCoord x1 = 50, y1 = 60;
    wxCoord x2 = 190, y2 = 60;

    dc.DrawLine(x1, y1, x2, y2);


    //draw line
    //draw point
    //draw polygon

    if( m_ly->lineSVector.size()>0 )
    {
        wxString tmp = wxString::Format(_T("%d"), m_ly->lineSVector.size());

        for( int i =0 ; i < m_ly->lineSVector.size(); i++)
        {
            wxArrayString strArrTmp = m_ly->lineSVector.at(i);

            wxPoint *pts = new wxPoint;

            wxPoint p1;
            wxPoint p2;

            wxPaintDC dc_k(this);
            wxRect m_Rect = this->GetRect();

            wxCoord xx1 = 40, yy1 = 50;
            wxCoord xx2 = 180, yy2 = 70;
            dc_k.DrawLine( xx1, yy1,xx2,yy2 );

            for( int j = 0; j < strArrTmp.size(); j++ )
            {

                wxString strPt = strArrTmp.Item(j);

                int p = strPt.Find(" ");

                wxString strX = strPt.substr(0,p);
                wxString strY = strPt.substr(p+1,strPt.Length()-p);

                double a = atof(strX);
                double b = atof(strY);

                a = a - m_ly->m_rect->GetLeft();
                b = b - m_ly->m_rect->GetBottom();

                a = m_Rect.GetLeft() + ((double)m_Rect.GetWidth() / (double)m_ly->m_rect->GetWidth()) * (double)a;
                b = m_Rect.GetBottom() + ((double)m_Rect.GetHeight() / (double)m_ly->m_rect->GetHeight())* (double)b;

                (pts + j)->x = a;
                (pts + j)->y = b;
                if(j==0)
                {
                    //xx1 = a;
                    //yy1 = b;
                }
                else if(j==1)
                {
                    //xx2 = a;
                    //yy2 = b;
                }       
            }
            //dc.DrawLines(strArrTmp.size(), pts);
            //dc.DrawLine( p1, p2 );

    /*      dc.DrawLine( xx1, yy1,xx2,yy2 );*/
        } 
        //wxMessageBox( tmp );
    }
    else
    {

    }



}

I try various method, but i find that only the

wxPaintDC dc(this);
wxRect m_Rect = this->GetRect(); //get the rect

wxCoord x1 = 50, y1 = 60;
wxCoord x2 = 190, y2 = 60;

dc.DrawLine(x1, y1, x2, y2);

can take effect.

it draw a line from (50,60) to (190,60)

I debug the code and find that the code can enter into

if( m_ly->lineSVector.size()>0 )
    {....
....
}

Also it can enter to the code [code] wxPaintDC dc_k(this); wxRect m_Rect = this->GetRect();

        wxCoord xx1 = 40, yy1 = 50;
        wxCoord xx2 = 180, yy2 = 70;
        dc_k.DrawLine( xx1, yy1,xx2,yy2 );

i debug and watch their value no doubt they have right value, and run to them,

but i am really can not understand why my drawing code in my if{} block not take effect.


Solution

  • You must not create wxPaintDC twice, you should draw on the same DC you already have instead of recreating another one inside the if.