CBitmapCtrl: Control for Displaying Bitmaps
Posted
by John Melas
on September 4th, 2000
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 KbDownload source - 3 Kb

Comments
Really awesome
Posted by hypheni on 10/30/2010 04:14pmMy application uses this class and all credit goes to the main coder. Thanks for the really helpful post.
ReplyGrabbing image with cursor and draggin image around
Posted by Legacy on 11/16/2003 12:00amOriginally 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:00amOriginally 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
ReplyCould this be used to display multiple bitmaps?
Posted by Legacy on 11/01/2002 12:00amOriginally posted by: John Lawson
...and if so, would there be any major changes necessary?
ReplyReally good, but how to make it resizeable??
Posted by Legacy on 06/20/2002 12:00amOriginally posted by: Christos
There occure different problems when trying to resize the Bitmapframe, OnSize() is not caught ...
ReplyWhat to do??
Excellent job !
Posted by Legacy on 06/09/2002 12:00amOriginally posted by: Hamed Shateri
It's what I want !
ReplySetting up this bitmap as transparent Background ?
Posted by Legacy on 05/11/2002 12:00amOriginally 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
ReplyButtonCtrl crashes on minimize
Posted by Legacy on 02/20/2002 12:00amOriginally 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!
Replywould this work to set bitmap as a dlg background
Posted by Legacy on 01/12/2002 12:00amOriginally posted by: gatman
????????
ReplyIf "notify" is activated, scrollbars doesn't work!
Posted by Legacy on 07/06/2001 12:00amOriginally posted by: Xavi Morejon
Reply