5,422,921 members and growing! (16,754 online)
Email Password   helpLost your password?
Desktop Development » Document / View » General     Intermediate

Usage of the CScrollView class to add scrolling functionality to your application

By VGirish

Step by step usage of the CScrollView class to add scrolling to your application
VC6, VC7, C++Windows, Win2K, WinXP, Visual Studio, MFC, Dev

Posted: 28 Jun 2002
Updated: 28 Jun 2002
Views: 57,155
Bookmarked: 17 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
30 votes for this Article.
Popularity: 5.93 Rating: 4.02 out of 5
2 votes, 10.0%
1
2 votes, 10.0%
2
2 votes, 10.0%
3
6 votes, 30.0%
4
8 votes, 40.0%
5

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);    
    // 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.


    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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

VGirish


www.geocities.com/contactgirish/AboutMe.html
Occupation: Web Developer
Location: India India

Other popular Document / View articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralChildFrame?????memberFurries3:28 25 Nov '04  
GeneralRe: ChildFrame?????memberPeteOlcott6:22 19 Aug '07  
GeneralThanksmemberAjay_Chopra3:15 17 Jun '04  
GeneralAre these calls all neededmemberCJN22:50 8 Oct '03  
GeneralHow to load bitmap from disk filememberCJN17:02 8 Oct '03  
GeneralRe: How to load bitmap from disk filesussAnonymous3:25 14 Oct '04  
GeneralTop of the classsussAndy Kuiper3:05 30 May '03  
GeneralthankssussAnonymous20:36 3 Sep '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Jun 2002
Editor: Nishant Sivakumar
Copyright 2002 by VGirish
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project