4

I'm struggling with this,

I want to draw a Bitmap into a PluginWindowWin (Firebreath) using GDI+; for that I have a timer, simulating the wm_paint message for now, and this code inside:

using namespace Gdiplus;
Graphics graphics(hwnd);
graphics.DrawImage(image, 0, 0, 400, 400);

image is a Gdiplus::Image, it works fine, BUT if I create 2 instances of the plugin (two different HWND) it will ONLY draw in one of them.

Is that the expected behavior?, I mean, GDI+ will draw only in one context created from an HWND?

Thanks!

1 Answers1

1

Basically, each (toplevel) window must have its own thread. If you put either window in its own thread, you should be able to draw in parallel by sending the message to both windows/threads having their own message dispatcher.

Edit: Threading with shared GDI- objects is a risky task. Resource management must be thread safe.

Sam
  • 7,778
  • 1
  • 23
  • 49
  • 1
    Thanks, I've already solved the problem. How? Well, I do not use anymore a timer, instead, I use the Refresh event from FireBreath. Why is working inside the event and not inside a timer remains as a complete mystery for me. – Maximiliano Santa Cruz Jan 31 '12 at 15:08
  • The event handler most likely dispatches the refresh event in window associated threads or states, where either context is current. You may print out the thread ID, window and DC handles in the event handler to get the answer. – Sam Jan 31 '12 at 22:17
  • That's right. I made some tests and found out the same thing you are saying. Looks like when the RefresheEvent is fired (the one from FireBreath) the window is ready to be painted (WM_PAINT); BUT if I try to force the painting (by using a timer) then the problem appears. Several methods I tried: GetDC(), Graphics.GetHDC(), and BeginPaint(); all returning different HDCs; the only one that work in different instances of the plugin is the BeginPaint()...EndPaint(). When I have more light about this matter I will edit the Question in case that anyone else have the same problem. Thanks! – Maximiliano Santa Cruz Feb 01 '12 at 03:19