-1

我有一些与 wxpaintDC 绘图相关的代码。

因此,我有一个与一些绘图相关的课程:

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
    {

    }



}

我尝试了各种方法,但我发现只有

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);

可以生效。

它从 (50,60) 到 (190,60) 画一条线

我调试代码,发现代码可以进入

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

也可以输入代码 [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 );

我调试并观察它们的价值,毫无疑问它们具有正确的价值,然后跑过去,

但我真的不明白为什么我的 if{} 块中的绘图代码不生效。

4

1 回答 1

1

你不能创建wxPaintDC两次,你应该在你已经拥有的同一个 DC 上绘制,而不是在if.

于 2013-08-29T12:09:03.233 回答