MFC Text Output Example

#include <afxwin.h>
class CSimpleApp : public CWinApp
{
public:
BOOL InitInstance();
};
class CMainFrame : public CFrameWnd
{
public:
CMainFrame();
DECLARE_MESSAGE_MAP()
afx_msg void OnPaint();
};


BOOL CSimpleApp::InitInstance(){
m_pMainWnd = new CMainFrame();
m_pMainWnd->ShowWindow(m_nCmdShow);
return TRUE;
}

CMainFrame::CMainFrame()
{
Create(NULL, "MFC Text Output");
}
BEGIN_MESSAGE_MAP(CMainFrame,CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
CSimpleApp MFCApp1;


afx_msg void CMainFrame::OnPaint()
{
CPaintDC dc(this);
TEXTMETRIC tm;
SIZE textWidth;
int xpos=0;//holds x position of text
int ypos=0;//holds y position of test
int hDCLast=dc.SaveDC();//save current font textmetric
dc.TextOut(xpos,ypos,"stock font");

dc.GetTextMetrics(&tm);
ypos=ypos+tm.tmHeight;//new y coordinate based on heigth of existing font

//selects new stock font and outputs text
CFont newfont;
newfont.CreateStockObject(ANSI_FIXED_FONT);
dc.SelectObject(newfont);
dc.TextOut(xpos,ypos,TEXT("ANSI_FIXED_FONT"));
ypos=ypos+tm.tmHeight;

//select new font colour and outputs text
COLORREF newtextcolour=COLORREF RGB(255, 0, 0);
dc.SetTextColor(newtextcolour);
dc.TextOut(xpos,ypos,TEXT("ANSI_FIXED_FONT, colour red"));
ypos=ypos+tm.tmHeight;

//sets new background color and outputs text
dc.SetBkColor(RGB(0, 0, 255));
dc.TextOut(xpos,ypos,TEXT("ANSI_FIXED_FONT, colour red, blue background"));
ypos=ypos+tm.tmHeight;

//sets new background mode and outputs text
dc.SetBkMode(TRANSPARENT);
dc.TextOut(xpos,ypos,TEXT("ANSI_FIXED_FONT, colour red, transparent back ground"));
ypos=ypos+tm.tmHeight;

//creates new font and outputs text
CFont newfontarial;
newfontarial.CreateFont(23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Ariel");
dc.SelectObject(newfontarial);
dc.GetTextMetrics(&tm);
dc.TextOut(xpos,ypos,TEXT("ANSI_FIXED_FONT, colour red, transparent background, arial 20"));
ypos=ypos+tm.tmHeight;
newfontarial.DeleteObject();

//restores original stock font and outputs text
dc.RestoreDC(hDCLast);
dc.GetTextMetrics(&tm);
dc.TextOut(0,ypos,"stock font restored using SaveDC/RestoreDC");
ypos=ypos+tm.tmHeight;

//creates 3 new fonts
CFont newfonttimes,newfontcourier,newfontariel;
newfonttimes.CreateFont(30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
newfontcourier.CreateFont(20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Courier New");
newfontariel.CreateFont(15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Ariel");

//selects 1st new font and outputs text
dc.SelectObject(newfonttimes);
dc.GetTextMetrics(&tm);
int baseline=tm.tmAscent;
textWidth=dc.GetTextExtent("times new roman 30 statement 1");
dc.TextOut(xpos,ypos,TEXT("times new roman 30 statement 1"));
xpos=xpos+textWidth.cx;

//selects 2nd new font and outputs text
dc.SelectObject(newfontcourier);
dc.GetTextMetrics(&tm);
int baselinecourier=baseline-tm.tmAscent;//calculates text baseline
textWidth=dc.GetTextExtent("courier 20 statement 2");//calculates width of text
dc.TextOut(xpos,ypos+baselinecourier,TEXT("courier 20 statement 2"));
xpos=xpos+textWidth.cx;

//selects 3rd new font and outputs text
dc.SelectObject(newfontariel);
dc.GetTextMetrics(&tm);
int baselineariel=baseline-tm.tmAscent;
dc.TextOut(xpos,ypos+baselineariel,TEXT("ariel 10 statement 3"));

newfonttimes.DeleteObject();
newfontcourier.DeleteObject();
newfontariel.DeleteObject();
}