CBitmapCtrl: Control for Displaying Bitmaps


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

Environment: VC6, Windows 95/98/NT

Its name is CBitmapCtrl and it is a class representing a dialog control that loads bitmaps from files. The fancy thing (that I've not seen in other related postings) is that this control does not clip the bitmap if it's bigger than the control's window - it uses scrollbars for the user to be able to see the whole bitmap.

You can use it very easily:
void SetBitmap(HBITMAP hBmp);
is the member function that loads a bitmap from HBITMAP
void LoadFile(CString filename);
is the member function that loads a bitmap from a file name.

To create the control you need these lines of code:

CRect brect;
where IDC_BITMAP_CTRL is the dialog resource that we use as a placeholder for the control and
m_bmpCtrl is a the CBitmapCtrl member variable.


Download demo project (includes release build) - 131 Kb
Download source - 3 Kb


  • Really awesome

    Posted by hypheni on 10/30/2010 11:14pm

    My application uses this class and all credit goes to the main coder. Thanks for the really helpful post.

  • Grabbing image with cursor and draggin image around

    Posted by Legacy on 11/16/2003 08:00am

    Originally posted by: aL

    If you want modify the demo project to allow an alternate way of scrolling, where you grab the image with the mouse curor and drag it around (like in Adobe Acrobat), you can override the WM_LBUTTONUP, WM_LBUTTONDOWN, and WM_MOUSEMOVE events. In the BitmapCtrl.h header add these members (add them to the protected section at the bottom):

    int m_handScrollMode;
    CPoint m_oldMousePos;
    HCURSOR m_grabCursor;

    At the top of the Create function in BitmapCtrl.cpp add these 2 lines:

    m_handScrollMode = 0;
    m_grabCursor = AfxGetApp()->LoadStandardCursor(IDC_SIZEALL);

    If you preffer a custom cursor while dragging (like the hand-grabbing cursor) make a cursor resource for it and do something like this instead:

    m_grabCursor = LoadCursor(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_MY_CUSTOM_CURSOR));

    Use this code for the 3 mouse handlers:

    void CBitmapCtrl::OnMouseMove(UINT nFlags, CPoint point)

    int newOffset = x_offset + m_oldMousePos.x - point.x;
    if (newOffset >= 0 && newOffset <= image_size.cx - rcClient.Width())
    x_offset = newOffset;

    newOffset = y_offset + m_oldMousePos.y - point.y;
    if (newOffset >= 0 && newOffset <= image_size.cy - rcClient.Height())
    y_offset = newOffset;

    m_oldMousePos = point;

    CWnd::OnMouseMove(nFlags, point);

    void CBitmapCtrl::OnLButtonDown(UINT nFlags, CPoint point)

    m_handScrollMode = 1;
    m_oldMousePos = point;

    CWnd::OnLButtonDown(nFlags, point);

    void CBitmapCtrl::OnLButtonUp(UINT nFlags, CPoint point)
    m_handScrollMode = 0;

    CWnd::OnLButtonUp(nFlags, point);

  • Memory leaks ?

    Posted by Legacy on 02/18/2003 08:00am

    Originally posted by: Pawel Sokolowski

    I just took a glimpse, and I am not sure if image object created by CBitmapCrtl::LoadFile() is deleted after the object is destroyed.
    Am I wrong ?

    Pawel Sokolowski

  • Could this be used to display multiple bitmaps?

    Posted by Legacy on 11/01/2002 08:00am

    Originally posted by: John Lawson

    ...and if so, would there be any major changes necessary?

  • Really good, but how to make it resizeable??

    Posted by Legacy on 06/20/2002 07:00am

    Originally posted by: Christos

    There occure different problems when trying to resize the Bitmapframe, OnSize() is not caught ...
    What to do??

  • Excellent job !

    Posted by Legacy on 06/09/2002 07:00am

    Originally posted by: Hamed Shateri

    It's what I want !

  • Setting up this bitmap as transparent Background ?

    Posted by Legacy on 05/11/2002 07:00am

    Originally posted by: Srinu

    Thanks for good work. I have another
    question i.e. bitmap as background.

    I have dialog based application and whose background should be a bitmap and that too transparent. Now I wanna display 256K color or any bitmap to that dialog is one of the issue. This bitmap is only background transparent as I have other button on that dialog.

    Kindly suggest how to do that or if you have sample code, let me know about it.



  • ButtonCtrl crashes on minimize

    Posted by Legacy on 02/20/2002 08:00am

    Originally posted by: Peter Dobrev

    If I've loaded a bitmap from file (using Bitmapctrl) and I minimize my dialog and then call it back the program crashes.

    Any suggestions of fixing the bug?
    It's really necessary for me, because I'm using this Class in almost all my projects

    Best wishes, Peter!

  • would this work to set bitmap as a dlg background

    Posted by Legacy on 01/12/2002 08:00am

    Originally posted by: gatman


  • If "notify" is activated, scrollbars doesn't work!

    Posted by Legacy on 07/06/2001 07:00am

    Originally posted by: Xavi Morejon

    It works fine (Thank you it's really helpfull) but
    I need to change the mouse cursor when it's over your BitmapCtrl (and todo other things) so I checked the "Notify" option of the CStatic where I
    have inserted your control.
    //In order to use ON_WM_SETCURSOR

    and now the scrollbars doesn't work!

    Does someone know how to solve it??

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date