Custom scrollable picture control to replace Windows Static Control in ‘SS_BITMAP’ mode


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.

  1. 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).
  2. 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.

Picture Control Preview
Picture Control Preview

How to use

  1. Call the static function ‘registerControlWindowClass()’ just once in your application (preferably when it starts).
  2. 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.
  3. Call ‘setPosition()’ and ‘setSize()’ to position and size the control on your window.
  4. 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.
  5. 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.

Bonus stuff

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)

Advertisements

Leave a reply here, thanks!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s