Take the 2-minute tour ×

I have some legacy MFC apps, and I'd like to use the Cairo drawing engine to add some charts and graphs.

I'm searching for a small example of how to get that to work. Basically, once I've created a PNG or GIF file, how do I get that show up in an MFC CView window?

My google-fu is not finding any good clues.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

From my demo samples,

// cairo_surface_t *surface;
// cairo_t *cr;

// surface = call_win32_surface_create_with_dib_T(CAIRO_FORMAT_ARGB32, 240, 80);
// cr = call_create_T (surface);

// call_surface_write_to_png_T (surface, "hello.png");

HDC src = call_win32_surface_get_dc_T(surface); // <--------
BitBlt(dest, 0, 0, 240, 80, src, 0,0, SRCCOPY); // <--------

Assuming that you already have a surface you can use something like the above sample.
dest is the HDC handle to the window you want to render the cairo surface.

Update: CView::OnDraw()

You should implement the OnDraw() method for your CView (inherited?) class.
You can use the pDC pointer to draw the cairo surface, ie:

pDC->BitBlt(0, 0, 240, 80, src, 0,0, SRCCOPY); // "HDC src" is mentioned above
share|improve this answer
Do you have a link to more samples? I've done years and years of MFC programming, but only with the simple builtins (dialogs, menus, etc). The last time I did charting, I used a library (ChartFX 98, if that tells you how long ago it was), and they handled all the drawing. –  Eric H. Aug 25 '09 at 14:07
@Eric H., see my update. I don't have CView-specific samples. If you still need help on how to use a CView object or about Cairo API, I could find some samples, but not at the moment. –  Nick Dandoulakis Aug 25 '09 at 15:56
add comment

Your Answer


