Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Manning - Windows.forms.programming.with.c#.pdf
Скачиваний:
108
Добавлен:
12.02.2016
Размер:
14.98 Mб
Скачать

IMPLEMENT THE PRINTCURRENTIMAGE METHOD (continued)

 

Action

Result

 

 

 

9

Print the file name, caption,

PrintTextString(g, printFont,

 

photographer, and notes

"FileName:", photo.FileName,

 

properties for the photograph

ref printArea);

 

PrintTextString(g, printFont,

 

onto the page.

 

"Caption:", photo.Caption,

 

How-to

ref printArea);

 

PrintTextString(g, printFont,

 

Use the yet-to-be-written

 

"Photographer:", photo.Photographer,

 

PrintTextString method.

ref printArea);

 

 

PrintTextString(g, printFont,

 

 

"Notes:", photo.Notes,

 

 

ref printArea);

 

 

}

 

 

 

The PrintTextString method is implemented by the subsequent steps. Note that our implementation prints the given string across multiple lines if necessary by drawing each word in the text string separately. Also note that the printArea variable is passed by reference. This is required in order to modify the printable area for text strings in the PrintTextString method. Since the RectangleF structure is a value type, it is normally passed by value.

IMPLEMENT THE PRINTTEXTSTRING METHOD

 

Action

Result

 

 

 

10

Add a PrintTextString

protected void PrintTextString(

 

method to the MainForm.cs

Graphics g,

 

code window.

Font printFont,

 

string name,

 

 

 

 

string text,

 

 

ref RectangleF printArea)

 

 

{

 

 

 

11

Create some local variables for

// Establish some useful shortcuts

 

the margins of the printable

float leftMargin = printArea.Left;

 

area.

float rightMargin = printArea.Right;

 

float topMargin = printArea.Top;

 

 

 

 

float bottomMargin = printArea.Bottom;

 

 

 

12

Also determine the height of the

float fontHeight = printFont.GetHeight(g);

 

font and the coordinates where

float xPos = printArea.Left;

 

the text should be drawn.

float yPos = topMargin + fontHeight;

 

 

 

 

 

13

Find the width of a space and

float spaceWidth = g.MeasureString(" ",

 

the name for the text string.

printFont).Width;

 

 

float nameWidth

 

 

= g.MeasureString(name, printFont).Width;

 

 

 

14

If this name does not fit in the

if (!printArea.Contains(xPos + nameWidth,

 

printable area, then abort the

yPos))

 

operation.

{

 

// Does not fit, so abort

 

 

 

 

return;

 

 

}

 

 

 

PRINTING

609

IMPLEMENT THE PRINTTEXTSTRING METHOD (continued)

 

Action

Result

 

 

 

15

Otherwise, draw the name on

g.DrawString(name, printFont,

 

the page and adjust the left

Brushes.Black, new PointF(xPos, yPos));

 

margin to occur after this string.

leftMargin += nameWidth + spaceWidth;

 

xPos = leftMargin;

 

 

 

 

 

16

Divide the text string into

// Draw text, use multi-lines if necessary

 

individual words, and iterate

string[] words

 

over these words.

= text.Split(" \r\t\n\0".ToCharArray());

 

foreach (string word in words)

 

 

 

 

{

 

 

 

17

Determine the width of the next

float wordWidth = g.MeasureString(

 

word.

word, printFont).Width;

 

 

if (wordWidth == 0.0)

 

 

continue;

 

 

 

18

If the size of this word takes it

if (xPos + wordWidth > rightMargin)

 

past the right margin, then

{

 

adjust the drawing coordinates

// Start a new line

 

xPos = leftMargin;

 

to start a new line.

 

yPos += fontHeight;

 

 

if (yPos > bottomMargin)

 

 

{

 

 

// no more page, abort foreach loop

 

 

break;

 

 

}

 

 

}

 

 

 

19

Draw this word at the current

g.DrawString(word, printFont,

 

position, and adjust the x

Brushes.Black,

 

coordinate appropriately.

new PointF(xPos, yPos));

 

xPos += wordWidth;

 

 

 

 

}

 

 

 

20

When finished drawing the text,

// Adjust print area based on drawn text

 

adjust the printable area to

printArea.Y = yPos;

 

exclude the area just drawn.

printArea.Height = bottomMargin - yPos;

 

}

 

 

 

 

 

When you test this code, make sure it works properly when printing with both landscape and portrait orientation. The page setup dialog can be used to alter the orientation, as well as the margins on the page. For further information on printing, or for details on the classes or methods used here, consult the .NET Framework online documentation.

610

CHAPTER 18 ODDS AND ENDS .NET

Соседние файлы в папке c#