Click here to Skip to main content
15,893,904 members
Articles / Programming Languages / C++

Using CodeProject - A Day In the Life of an Application - Part 3 of 5

Rate me:
Please Sign up or sign in to vote.
4.63/5 (20 votes)
27 Jan 2007CPOL14 min read 43.3K   436   17  
The right way to code using CodeProject for occasional support
// SecondaryView.cpp : implementation file
//

#include "stdafx.h"
#include "SDIMultiApp1.h"
#include "SecondaryView.h"


IMPLEMENT_DYNCREATE(CSecondaryView, CScrollView)

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// CSecondaryView message map
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CSecondaryView, CScrollView)
END_MESSAGE_MAP()


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// CSecondaryView constructor/destructor
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
CSecondaryView::CSecondaryView()
{
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
CSecondaryView::~CSecondaryView()
{
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// CSecondaryView overrides
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void CSecondaryView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;

	// for the moment, we'll go ahead and use the default view size generated by 
	// class wizard
	sizeTotal.cx = sizeTotal.cy = 100;
	SetScrollSizes(MM_TEXT, sizeTotal);
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void CSecondaryView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();

	CFont docFont;
	CFont* pOldFont;

	// construct and select our font
	BuildFont(pDC, &docFont, 11, false, false);
	pOldFont = pDC->SelectObject(&docFont);

	// Get a baseline lineheight value. We add 2 pixels to the calculated 
	// lineheight so we have a little white space betwen lines of text.
	CString sText = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	int nLineHeight = pDC->GetTextExtent(sText).cy + 2;
	int nMargin = 10;
	int nMaxLines = 20;
	int nViewHeight = 5;
	int nViewWidth  = 0;

	// draw the lines
	for (int i = 1; i <= nMaxLines; i++)
	{
		// build our text string
		sText.Format("This is line number %02d of %d", i, nMaxLines);
		// display it
		pDC->TextOutA(nMargin, nViewHeight, sText);
		// calculate our new view height (and coincidentally, our next y position)
		nViewHeight += nLineHeight;
		// determine our new view width
		nViewWidth = __max(nViewWidth, pDC->GetTextExtent(sText).cx);
	}
	// release the font resource
	pDC->SelectObject(pOldFont);

	// use our caluclated view height and width to reset the scroolbars.
	SetScrollSizes(MM_TEXT, CSize(nViewWidth, nViewHeight));
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// CSecondaryView diagnostics
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
void CSecondaryView::AssertValid() const
{
	CScrollView::AssertValid();
}

#ifndef _WIN32_WCE
void CSecondaryView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}
#endif
#endif //_DEBUG


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// CSecondaryView programmer-added functions
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

//------------------------------------------------------------------------------
// Added this function to build a suitable font for the INFO text - jms
//------------------------------------------------------------------------------
BOOL CSecondaryView::BuildFont(CDC* pDC, CFont* pFont, int nFontHeight, bool bBold, bool bItalic)
{
	nFontHeight = -MulDiv(nFontHeight, pDC->GetDeviceCaps(LOGPIXELSY), 72);
    CString sFontName = _T("Arial");

	int   nWeight = (bBold) ? FW_BOLD : FW_NORMAL;
	BYTE  nItalic = (bItalic) ? 1 : 0;

	return pFont->CreateFont(nFontHeight, 0, 0, 0, nWeight, nItalic, 0, 0, DEFAULT_CHARSET,
                             OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY,
                             DEFAULT_PITCH | FF_DONTCARE, sFontName);
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// CSecondaryView windows message handlers
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions