about SetUserScale

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
dqf88
Experienced Solver
Experienced Solver
Posts: 55
Joined: Fri Aug 10, 2012 9:59 am

about SetUserScale

Post 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);
}
Attachments
缩放之后
缩放之后
缩放之前
缩放之前
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: about SetUserScale

Post by doublemax »

What exactly is the problem? Are the lines wrong? Or the background image?

What scale values are used for the images?
Use the source, Luke!
dqf88
Experienced Solver
Experienced Solver
Posts: 55
Joined: Fri Aug 10, 2012 9:59 am

Re: about SetUserScale

Post by dqf88 »

when zoom the canvas, the lines deviate from its original position
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: about SetUserScale

Post 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.
Use the source, Luke!
dqf88
Experienced Solver
Experienced Solver
Posts: 55
Joined: Fri Aug 10, 2012 9:59 am

Re: about SetUserScale

Post 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
Post Reply