CBitmapCtrl: Control for Displaying Bitmaps

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;
GetDlgItem(IDC_BITMAP_CTRL)->GetWindowRect(brect);
ScreenToClient(brect);
m_bmpCtrl.Create(WS_CHILD|WS_VISIBLE,brect,this);
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.

Downloads

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


Comments

  • Really awesome

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

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

    Reply
  • Grabbing image with cursor and draggin image around

    Posted by Legacy on 11/16/2003 12: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)
    {
    if(m_handScrollMode)
    {
    SetCursor(m_grabCursor);

    int newOffset = x_offset + m_oldMousePos.x - point.x;
    if (newOffset >= 0 && newOffset <= image_size.cx - rcClient.Width())
    {
    x_offset = newOffset;
    SetScrollPos(SB_HORZ,x_offset,TRUE);
    }

    newOffset = y_offset + m_oldMousePos.y - point.y;
    if (newOffset >= 0 && newOffset <= image_size.cy - rcClient.Height())
    {
    y_offset = newOffset;
    SetScrollPos(SB_VERT,y_offset,TRUE);
    }

    DrawBitmap();
    m_oldMousePos = point;
    }

    CWnd::OnMouseMove(nFlags, point);
    }

    void CBitmapCtrl::OnLButtonDown(UINT nFlags, CPoint point)
    {
    SetCapture();

    m_handScrollMode = 1;
    m_oldMousePos = point;
    SetCursor(m_grabCursor);

    CWnd::OnLButtonDown(nFlags, point);
    }

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

    CWnd::OnLButtonUp(nFlags, point);
    }


    Reply
  • Memory leaks ?

    Posted by Legacy on 02/18/2003 12: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

    Reply
  • Could this be used to display multiple bitmaps?

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

    Originally posted by: John Lawson

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

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

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

    Originally posted by: Christos

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

    Reply
  • Excellent job !

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

    Originally posted by: Hamed Shateri

    It's what I want !

    Reply
  • Setting up this bitmap as transparent Background ?

    Posted by Legacy on 05/11/2002 12: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.

    Thanks

    Srinu

    Reply
  • ButtonCtrl crashes on minimize

    Posted by Legacy on 02/20/2002 12: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!

    Reply
  • would this work to set bitmap as a dlg background

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

    Originally posted by: gatman

    ????????

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

    Posted by Legacy on 07/06/2001 12: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??

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

Top White Papers and Webcasts

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds