When Windows Static Control’s ‘SS_BITMAP’ style is set, the control can be used to display ‘HBITMAP’ as a static image. It serves its purpose well. Unfortunately, there are two quirks about this control that made it unusable in one of my projects.
- The static picture is unscrollable: If the bitmap given to the control is too big, the control merely crops it. Static window class is made to not allow user interaction, so setting the scrollbars to visible won’t work either (they will be shown but will be disabled).
- Bitmap resource disposal is tricky: After you assign a bitmap to the control, you are responsible for destroying both the handle that you gave to the control and an internal copy that the control has maintained. I will not go into detail about this but needless to say – it is a major headache and easy target for resource leaking.
Hence, this control. This control solves the above problems by being scrollable automatically if the given bitmap is too big and taking ownership/responsibility of destruction of the bitmap you pass to the control. In addition, the control is wrapped in a neat C++ class.
The source code is available here and is licensed under public domain.
How to use
- Call the static function ‘registerControlWindowClass()’ just once in your application (preferably when it starts).
- Call the static function ‘create()’ to create a new control with the specified parent window. Check that you received a valid pointer. If this pointer is NULL, there was an internal error.
- Call ‘setPosition()’ and ‘setSize()’ to position and size the control on your window.
- Call ‘setBitmapHandle()’ to assign an image. The control will enable scrollbars automatically if the picture is too big. The control owns this handle now so do not delete this bitmap.
- If you called ‘registerControlWindowClass()’, especially from a DLL, it is a good idea to call ‘unregisterControlWindowClass()’ to remove the control’s window class from registration when the DLL is unloaded.
The control consumes bitmap handle to display an image. You can use Windows Imaging Component to load different image formats and convert it to proper bitmap handle using this (dirty and leaky – which you should fix) test code: WIC_FileToHBitmap.cpp (link with Windowscodecs.lib)