11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9. Font<br />

CGDIDoc for this command, and the corresponding member function CGDIDoc::OnDialogProgress() is<br />

implemneted as follows:<br />

void CGDIDoc::OnDialogProgress()<br />

{<br />

CProgDlg dlg;<br />

}<br />

dlg.DoModal();<br />

After all these implementations, we can execute command Dialog | Progress to test the percentage bar.<br />

9.4 One-Line Text Editor, Step 1: Displaying a Static String<br />

From now on we are going to implement a very simple one-line text editor: it will display only one line<br />

text that can be edited through using mouse and keyboard. We will implement many features of a standard<br />

editor such as font selection, changing text styles. The sample application in this section does not introduce<br />

any new concept, it is the base of later sections.<br />

Sample 9.4\GDI is a standard SDI application generated by Application Wizard. Because the<br />

horizontal size of the text string may be bigger than that of the client window as the user input more and<br />

more characters, we need to add scroll bars to the application. In order to do this, we can choose<br />

CScrollView as the base class of the client window in the final step of Application Wizard.<br />

The sample does nothing but displaying a static text in the client window. Other features of text editor<br />

will be implemented in later sections.<br />

Two variables m_szText and m_ftDraw along with two member functions are declared in class<br />

CGDIDoc:<br />

class CGDIDoc : public CDocument<br />

{<br />

protected:<br />

CGDIDoc();<br />

DECLARE_DYNCREATE(CGDIDoc)<br />

CString m_szText;<br />

CFont m_ftDraw;<br />

……<br />

public:<br />

CString GetText(){return m_szText;}<br />

CFont *GetFont(){return &m_ftDraw;}<br />

//{{AFX_VIRTUAL(CGDIDoc)<br />

public:<br />

};<br />

Variable m_szText will be used to store the text string, and m_ftDraw will be used to store the font used<br />

for text drawing. Functions CGDIDoc::GetText() and CGDIDoc::GetFont() provide a way of accessing the<br />

two member variables outside class CGDIDoc.<br />

Because we still do not have an interactive input environment, in the constructor, variable m_szText is<br />

initialized to a fixed string:<br />

CGDIDoc::CGDIDoc()<br />

{<br />

m_szText="This is just a test string";<br />

}<br />

In the sample, function CGDIDoc::OnNewDocument() is modified as follows:<br />

BOOL CGDIDoc::OnNewDocument()<br />

{<br />

CClientDC dc(NULL);<br />

LOGFONT lf;<br />

250

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!