15.02.2015 Views

C# 4 and .NET 4

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

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

OC34 ❘ ChaPTer 48 GrAphics with Gdi+<br />

your selected font is. It is the same for all lines of text so there is no point storing the height separately for<br />

each one; you store it once, in the Form1.lineHeight field. As for the position, well, in this case, the x<br />

coordinate is just equal to the margin, <strong>and</strong> the y coordinate is easily calculated here:<br />

margin + lineHeight*(however many lines are above this one)<br />

If you had been trying to display <strong>and</strong> manipulate, say, individual words instead of complete lines, then<br />

the x position of each word would have to be calculated using the widths of all the previous words on that<br />

line of text, but the intent is to keep it simple here, which is why you are treating each line of text as one<br />

single item.<br />

Let’s turn to the main menu now. This part of the application is more the realm of Windows Forms (see<br />

Chapter 39) than of GDI+. Add the menu options using the design view in Visual Studio 2010, but rename<br />

them menuFile, menuFileOpen, <strong>and</strong> menuFileExit. Next, add event h<strong>and</strong>lers for the File Open <strong>and</strong> File<br />

Exit menu options using the Visual Studio 2010 properties window. The event h<strong>and</strong>lers have their Visual<br />

Studio 2010-generated names of menuFileOpen_Click() <strong>and</strong> menuFileExit_Click().<br />

Add some extra initialization code in the Form1() constructor:<br />

public Form1()<br />

{<br />

InitializeComponent();<br />

CreateFonts();<br />

fileOpenDialog.FileOk += delegate { LoadFile(fileOpenDialog.FileName); };<br />

fileOpenDialog.Filter =<br />

"Text files (*.txt)|*.txt|<strong>C#</strong> source files (*.cs)|*.cs";<br />

}<br />

code download CapsEditor.sln<br />

You add the event h<strong>and</strong>ler here for instances when the user clicks OK in the File Open dialog box. You have<br />

also set the filter for the Open File dialog box, so that you can load text files only. The example in this<br />

case only uses .txt files, in addition to the <strong>C#</strong> source files, so you can use the application to examine the<br />

source code for the samples.<br />

CreateFonts() is a helper method that sorts out the fonts you intend to use:<br />

private void CreateFonts()<br />

{<br />

mainFont = new Font("Arial", 10);<br />

lineHeight = (uint)mainFont.Height;<br />

emptyDocumentFont = new Font("Verdana", 13, FontStyle.Bold);<br />

}<br />

The actual definitions of the h<strong>and</strong>lers are pretty st<strong>and</strong>ard:<br />

protected void menuFileOpen_Click(object sender, EventArgs e)<br />

{<br />

fileOpenDialog.ShowDialog();<br />

}<br />

protected void menuFileExit_Click(object sender, EventArgs e)<br />

{<br />

Close();<br />

}<br />

Next, take a look at the LoadFile() method. It h<strong>and</strong>les the opening <strong>and</strong> reading of a file (as well as<br />

ensuring a Paint event is raised to force a repaint with the new file):<br />

private void LoadFile(string FileName)<br />

{<br />

StreamReader sr = new StreamReader(FileName);<br />

string nextLine;<br />

documentLines.Clear();<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!