wxGraphicsContext vs wxDC

227 views
Skip to first unread message

Gerald Brandt

unread,
Dec 9, 2017, 10:13:35 AM12/9/17
to wx-u...@googlegroups.com
Hi,

I have some pretty straight forward code that writes text. I decided
today to do a quick conversion from a wxPaintDC to a wxGraphicsContext.
This looks pretty straight forward to me, but nothing get displayed.

Any ideas?

Works:

    wxPaintDC dc(this) ;

    wxString str ;
    POSITION_T len = mDocument.GetTextSize() ;
    for(POSITION_T loop = 0; loop < len; loop++)
    {
        CHAR_T ch = mDocument.GetChar(loop) ;
        if(ch >= STYLE_END_OF_STYLES)
        {
            str.append(1, (char)ch) ;

        }
        else if(ch == HARD_RETURN)
        {
            str.append(1, (char)ch) ;
        }
    }

    dc.DrawText(str, 1, 1) ;


Doesn't work:

    wxPaintDC dc(this) ;

    wxGraphicsContext *gc = wxGraphicsContext::Create( dc );
    if (gc)
    {
        gc->SetPen( *wxBLACK_PEN );
        gc->SetBrush( *wxBLACK_BRUSH) ;

        wxString str ;
        POSITION_T len = mDocument.GetTextSize() ;
        for(POSITION_T loop = 0; loop < len; loop++)
        {
            CHAR_T ch = mDocument.GetChar(loop) ;
            if(ch >= STYLE_END_OF_STYLES)
            {
                str.append(1, (char)ch) ;

            }
            else if(ch == HARD_RETURN)
            {
                str.append(1, (char)ch) ;
            }
        }

        gc->DrawText(str, 10.0, 10.0) ;

        delete gc ;
    }

Gerald

Vadim Zeitlin

unread,
Dec 9, 2017, 10:22:55 AM12/9/17
to wx-u...@googlegroups.com
On Sat, 9 Dec 2017 09:13:31 -0600 Gerald Brandt wrote:

GB> I have some pretty straight forward code that writes text. I decided
GB> today to do a quick conversion from a wxPaintDC to a wxGraphicsContext.
GB> This looks pretty straight forward to me, but nothing get displayed.
GB>
GB> Any ideas?

Did you check that wxGraphicsContext creation succeeded? It should have
had, of course, but if it fails, this would explain why nothing is drawn.

You could also try a more minimal modification first, by replacing

wxPaintDC dc(this) ;

line with

wxPaintDC dcPaint(this);
wxGDCC dc(dcPaint);

and not changing anything else and seeing if this works.

Finally, it's always a good idea to test a sample when you run into some
problem, so I'd also build samples/drawing and check that using wxGC works
in it. If it doesn't, it definitely means some problem with your library
build...

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

Gerald Brandt

unread,
Dec 9, 2017, 10:38:03 AM12/9/17
to wx-u...@googlegroups.com
Hi,

Yup, verified gc creation works. I use gcdc elsewhere in the
application, and that works as well.

Gerald

Gerald Brandt

unread,
Dec 9, 2017, 10:45:50 AM12/9/17
to wx-u...@googlegroups.com


On 2017-12-09 09:22 AM, Vadim Zeitlin wrote:
Figures. Ask the question then find the answer.

When creating a wxGraphicsContext from a wxDC (wxPaintDC), the GC does
not inherit the DC's font. I had to set a font into the GC.

Thanks,
Gerald

Reply all
Reply to author
Forward
0 new messages