Search code examples
winformsresizegdi+hwnd

GDI+ Graphics object does not reflect to window resize


I've got a following problem. I'm drawing on a Windows Form's Panel surface, using native GDI+ (drawing algorithm is inside native DLL, called by P/Invoke). Everything is OK, but just until one changes size of the panel. If it is shrinked, it's being rendered properly. However, if it is enlarged, only the portion of Panel's area, that corresponds to its original size is redrawn by GDI+.

Why is it so? Do I really have to create new Graphics object every time, when the Panel resizes?


Solution

  • Answered by Hans Passant in comment:

    You found out why, the Graphics object no longer matches the state of the window. You are assuming that it is expensive to create. It isn't, it only costs couple of dozen microseconds. It is only expensive to keep it around, needlessly using memory. Anyhoo, you need to recreate it when the window state changes.