Introduction
This code helps you to load a bitmap from a resource or from a file and show
it in a MDI View. It enables scrolling and also scrolls the bitmap accordingly.
Steps to follow
Follow the following seven easy steps :-
Step 1
Create a MDI Application by selecting Multiple Documents Interface in Step 1 of the
app-wizard process.
Step 2
In the 6th step, select the View class and in the combo box called base class
, select CScrollView and then click Finish.
Step 3
Create the following variables in the View class (yourview.h file)
CDC m_MemDC;
CBitmap m_bmpView;
int m_nBmpWidth,m_nBmpHeight;
Step 4
In the View class , select the OnInitialUpdate function and
change the code like this.
void CMDIView::OnInitialUpdate()
{
CDC *pDC = this->GetDC();
m_MemDC.CreateCompatibleDC(pDC);
m_bmpView.LoadBitmap(IDB_BMP_BKGND);
m_MemDC.SelectObject(&m_bmpView);
BITMAP Bitmap;
m_bmpView.GetBitmap(&Bitmap);
m_nBmpHeight = Bitmap.bmHeight;
m_nBmpWidth = Bitmap.bmWidth;
CSize size(m_nBmpWidth,m_nBmpHeight);
SetScrollSizes(MM_TEXT,size);
this->ReleaseDC(pDC);
}
Step 5
Select the OnDraw function and change the code inside like this
void CMDIView::OnDraw(CDC* pDC)
{
pDC->BitBlt(0,0,m_nBmpWidth,m_nBmpHeight,
&m_MemDC,0,0,SRCCOPY);
SetScrollSizes(MM_TEXT, CSize(m_nBmpWidth,m_nBmpHeight));
CChildFrame *pParentFrame =
(CChildFrame *)this->GetParentFrame();
pParentFrame->RecalcLayout();
ResizeParentToFit();
}
Step 6
To prevent flickering of the bitmap when its being scrolled, override the
View's OnEraseBkgnd and the ChildFrame's
OnEraseBkgnd like this.
BOOL CMDIView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
BOOL CChildFrame::OnEraseBkgnd(CDC* pDC)
{
return FALSE;
}
Step 7
Compile your project and run it. That's it. Now, you have a scrolling window
which displays a bitmap too. All luck and have a great time.
| You must Sign In to use this message board. |
|
|
 |
|
 |
I replaced this unknown type with CMainFrm and now it works. I neither had CChildFrm.h or cpp files nor did VC++ (6) know about ChildFrame. Still, it seems to be working.
Anyone able to clear up this mystery for me?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
If you followed all of the steps correctly and are using MSVC++ 6.0, then the only missing steps are:
(1) Include the following line of code in your View.cpp #include "ChildFrm.h"
(2) Right Click ProjectName resources in resource view: (a) Insert (b) Select Bitmap, (c) Import
(3) Right Click IDB_BITMAP1: Properties Change ID: to IDB_BMP_BKGND
The program then worked as described.
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
 | Thanks  Ajay_Chopra | 3:15 17 Jun '04 |
|
 |
I wanted to implement similar stuff in Single Document Interface. I could easily implement it after going through your code. Thanks
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Why are the following calls done in OnDraw()? SetScrollSizes(MM_TEXT, CSize((int)m_nBmpWidth, (int)m_nBmpHeight)); CChildFrame *pParentFrame = (CChildFrame *)this->GetParentFrame(); pParentFrame->RecalcLayout(); ResizeParentToFit();
Couldn't the SetScrollSizes() call be done only when the document size changes (i.e. only when a new image is loaded)?
Couldn't the last 2 calls be done just when the window is resized e.g. in OnSize()?
Regards, C Newell
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
This is an interesting and succinct article.
You mention "// Here IDB_BMP_BKGND is a bitmap included in the resource // Note : You can also load a bitmap from a file in the // hard disk instead of using the bmp in the resource."
This seems to imply LoadBitmap(LPCTSTR) can take a disk filename as an argument, but that gave me an exception.
What is the easiest way to load a bitmap from a disk file?
Regards Colin Newell
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
There is a function LoadImage( ) but it works only with "bmp". Can somebody tell me how to work with "jpeg".
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi all..
I would like to use this function too..to load image from local drive but not from resource.. I'm using VC++ 6.0, under bitmap function, I didn't found LoadImage() function..
except using LoadImage(), got other function I can use it to load image from local drive..??
Please advise.. Thank you.. Shizu..
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thanks a lot. That was exactly what I was searching for. It compiled with no probs after including "ChildFrm.h". 
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
 | thanks  Anonymous | 20:36 3 Sep '02 |
|
|
 |
|
|