How to handle GDI resources
Does anyone know a good document/article about GDI resource handling?
I need to share some resources like icons and bitmaps among classes that can have different lifetime, and I want to understand how I should approach this problem.
For Mutexes and other kernel objects there is a DuplicateHandle function, but GDI is confusing me a little. Also, the way CBitmap returns HBITMAP through const operator HBITMAP, and that like, is a little bit s开发者_如何学Pythoncary.
I would like to avoid creating local bitmaps on every redraw, so some resource caching would be good, but also, I am not sure I can start creating and loading C##### resources while the main message pump hasn't started running.
Seems that I'm using wrong keywords, as I can't find any good, but manageably short documentation.
There is no such documentation, it is all pretty straightforward. It is completely up to you to decide when to call DeleteObject(). And to decide how to balance resource usage of your program against dynamically creating and destroying the object when needed. Only largish bitmaps are really worthy to keep around. Pens and brushes are very cheap, you create and destroy them on-the-fly. Fonts are a corner case, often cached simply for the live of the program since you need so few of them.
There are plenty of ways to manage caching, a shared_ptr<> in C++ provides the standard reference counting pattern for example. But it is very typical to just keep the reference as a member of your window wrapper class. It isn't very common that the same bitmap would be used in multiple windows. Ymmv.
Creating GDI objects doesn't require a message loop.
精彩评论