Click here to Skip to main content
15,936,802 members
Articles / Desktop Programming / MFC

Scroll with Fixed Background

Rate me:
Please Sign up or sign in to vote.
3.53/5 (10 votes)
9 Oct 2008CPOL2 min read 34.7K   1.1K   9  
Scrolling text with background image fixed in MFC


This article explains how to scroll text on a fixed image background in VC++ 6.0. I have searched the internet but could not find any simple article on this. So, I started experimenting on this and finally found a solution. Well, I have tested this on WinXP only.

Image 1

Although the primary aim of this article is to display scrolling text on fixed background, it also covers how to draw a background with a bitmap brush.

Create an MFC Doc/View Project

Create an MFC doc/view project and select CScrollView as the base view class. We have selected CScrollView as base for our view class because it handles all the scrolling stuff. Now add a bitmap resource which you want as window background. Now create a CBitmap variable to load your bitmap resource in your frame window class.

class CMainFrame : public CFrameWnd
	protected: // create from serialization only

	CBitmap m_bmBG;

In the PreCreateWindow() virtual function, change the extended style as follows:

cs.dwExStyle |= WS_EX_COMPOSITED;

WS_EX_COMPOSITED is an Windows extended style and MSDN says that this style makes painting all descendants of a window in bottom-to-top painting order using double-buffering. It will serve our purpose. In OnCreate() handler function, load our bitmap.

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	return 0;

Add WM_ERASEBKGND Windows message handler. Here, we will be creating pattern brush from our bitmap and select it into the device context and fill the client area. The code looks like this:

BOOL CMainFrame::OnEraseBkgnd(CDC* pDC) 
	// TODO: Add your message handler code here and/or call default
	CBrush hbr,*phbrOld;
	CRect rect;

	phbrOld= pDC->SelectObject(&hbr);
	return TRUE;	

Don't forget to add the following line in Mainfrm.h:

#define WS_EX_COMPOSITED 0x02000000L

In the view class handle WM_CREATE message and in the handler, add the following code:

int CTransscrollView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
	if (CScrollView::OnCreate(lpCreateStruct) == -1)
		return -1;
	CClientDC dc(this);
	m_ftTimes.CreatePointFont(500,"Times New Roman",&dc);
	CFont *pOldFont;
	pOldFont = dc.SelectObject(&m_ftTimes);

	m_nLineHt = tm.tmHeight + tm.tmExternalLeading;

	return 0;

We have created 50 point Times New Roman font and selected into our client dc. Calculate the line height and store it. Don't forget to create CFont m_ftTimes and int m_nLineHt class variables. Now handle WM_ERASEBKGND message. Just return TRUE in the message handler. Change the OnInitialUpdate() function, change the code.

void CTransscrollView::OnInitialUpdate()

	CSize sizeTotal;
	// TODO: calculate the total size of this view = 500; = 25 * m_nLineHt;
	SetScrollSizes(MM_TEXT, sizeTotal);	

We are going to display 25 lines. We are creating a view large enough to show these lines.

In OnDraw(), we are going to draw only those lines which will become visible when scrolling. Initially all the lines will be drawn.

void CTransscrollView::OnDraw(CDC* pDC)
	CTransscrollDoc* pDoc = GetDocument();
	// TODO: add draw code for native data here
	CFont *pOldFont;
	int ct;

	int start = GetDeviceScrollPosition().y /m_nLineHt -1;
	if (start < 0) start =0;

	CRect rcClient;


	ct = start + rcClient.Height()/m_nLineHt + 3;

	if( ct > 25) ct=25;
	pOldFont = pDC->SelectObject(&m_ftTimes);
	for(int i=start;i<ct;i++)
		CString s;
		s.Format("I am line %d on a fixed background",i+1);
		pDC->TextOut(0,i * m_nLineHt,s);

As we know on scrolling, ScrollWindow() scrolls the window contents, we need to draw only the lines that come to visibility. So we have calculated the starting line number and the ending line that will be visible. We have drawn only those lines. Note that initially all lines will be drawn.



  • 9th October, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior) Valuelabs
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

-- There are no messages in this forum --