Page 1 of 1

about SetUserScale

Posted: Sun Nov 18, 2018 1:40 am
by dqf88
I have drew lines on the background picture with mouse, when zooming the canvas with SetUserScale method, there comes the problem(see the following pictures), how to solve the problem?

Code: Select all

void AAFrame::PrepareDC(wxDC& dc)
{
    dc.SetLogicalOrigin( m_xLogicalOrigin, m_yLogicalOrigin );
    dc.SetAxisOrientation( !m_xAxisReversed, m_yAxisReversed );
    dc.SetUserScale( m_xUserScale, m_yUserScale );
}

void MyCanvas::Draw(wxDC& pdc)
{
    wxDC &dc = pdc ;
    // Adjust scrolled contents for screen drawing operations only.
    if ( wxDynamicCast(&pdc, wxBufferedPaintDC) || wxDynamicCast(&pdc, wxPaintDC) )
    {
        PrepareDC(dc);
    }
    m_owner->PrepareDC(dc);
    dc.Clear();

    if(bg.IsOk())
    {
        dc.DrawBitmap(bg, 10, 10, true);
    }

    if(lines.size()>=1)
    {
        Line l;
        double dist;
        wxVector<Line>::const_iterator i;
        for(i=lines.begin(); i!=lines.end(); i++)
        {
            l=*i;
            if(i==lines.begin())
            {
                dc.SetTextForeground(l.labelcolor);  // 设置字体颜色
                dc.SetFont(l.labelfont); // 设置字体大小,粗细,字体

                wxPen pen(l.linecolor,l.linewidth);
                dc.SetPen(pen);
                dc.DrawLine(l.startPoint,l.endPoint);
                rulerRatio=rulerSize/lines[0].length;
                int x=wxMin(l.startPoint.x,l.endPoint.x)+abs(l.startPoint.x-l.endPoint.x)/2;
                int y=wxMin(l.startPoint.y,l.endPoint.y)+abs(l.startPoint.y-l.endPoint.y)/2;
                wxSize textsize=dc.GetTextExtent(l.label);
                x=x-textsize.GetWidth()/2;
                y=y-textsize.GetHeight();
                wxPoint centerPt=wxPoint(x,y);
                dc.DrawText(l.label,centerPt);
            }
            else
            {
                dc.SetTextForeground(l.labelcolor);  // 设置字体颜色
                dc.SetFont(l.labelfont); // 设置字体大小,粗细,字体

                wxPen pen(l.linecolor,l.linewidth);
                dc.SetPen(pen);
                dc.DrawLine(l.startPoint,l.endPoint);
                dist=rulerRatio*l.length;
                int x=wxMin(l.startPoint.x,l.endPoint.x)+abs(l.startPoint.x-l.endPoint.x)/2;
                int y=wxMin(l.startPoint.y,l.endPoint.y)+abs(l.startPoint.y-l.endPoint.y)/2;
                wxSize textsize=dc.GetTextExtent(l.label);
                x=x-textsize.GetWidth()/2;
                y=y-textsize.GetHeight();
                wxPoint centerPt=wxPoint(x,y);
                dc.DrawText(l.label,centerPt);
            }
        }
    }
    if ( wxDynamicCast(&pdc, wxBufferedPaintDC) ||wxDynamicCast(&pdc, wxPaintDC) )
    {
        wxCoord x0, y0;
        dc.GetDeviceOrigin(&x0, &y0);
        m_sizeX = dc.LogicalToDeviceX(dc.MaxX()) - x0+1;
        m_sizeY = dc.LogicalToDeviceY(dc.MaxY()) - y0+1;
    }
}

void MyCanvas::OnPaint(wxPaintEvent &WXUNUSED(event))
{
    wxBufferedPaintDC bpdc(this);
    Draw(bpdc);
}

Re: about SetUserScale

Posted: Sun Nov 18, 2018 10:33 am
by doublemax
What exactly is the problem? Are the lines wrong? Or the background image?

What scale values are used for the images?

Re: about SetUserScale

Posted: Mon Nov 19, 2018 12:24 am
by dqf88
when zoom the canvas, the lines deviate from its original position

Re: about SetUserScale

Posted: Mon Nov 19, 2018 9:59 am
by doublemax
dqf88 wrote:when zoom the canvas, the lines deviate from its original position
You didn't answer my questions.

What confuses me, is that the background image looks the same in both images. The upper image looks like the lines were correct, if the image was scaled down.

Re: about SetUserScale

Posted: Mon Nov 19, 2018 11:46 am
by dqf88
firstly, i draw line on the canvas, then i zoom the canvas, the problem comes as the following pictures.

before zooming the canvas
1.png
after zooming the canvas
2.png