我有一些与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-&gt; GetRect();
wxCoord xx1 = 40, yy1 = 50;
wxCoord xx2 = 180, yy2 = 70;
dc_k.DrawLine( xx1, yy1,xx2,yy2 );
我调试并观察他们的价值,毫无疑问他们有正确的价值,然后跑向他们,
但我真的不明白为什么我的if {}块中的绘图代码没有生效。
答案 0 :(得分:1)
你不能创建wxPaintDC
两次,你应该使用你已经拥有的同一DC,而不是在if
内重新创建另一个。