在控制臺應用程序中實現打印
你是否厭倦了將從控制臺應用程序中選出的文本打印出來?這里將介紹一種方便的工具來幫你實現打
你是否厭倦了將從控制臺應用程序中選出的文本打印出來?這里將介紹一種方便的工具來幫你實現打印。
by Bill Wagner
我在以前的一篇文章中曾介紹到過如何從控制臺命令中捕獲輸出結果并將它放到剪貼板中。我常常需要從一些控制臺命令中將文本輸出結果打印出來,因此我對最后的例子進行了修改以使它支持打印。
.NET打印框架對于使用MFC的人來說并不陌生:你只需建立一個PrintDocument對象并調用其Print()方法就行。PrintDocument對象會調用你建立的事件處理來打印每個頁面。打印完所有頁面之后,打印處理會設置一個標記并結束打印。
在這個新的例子中,我做了三個地方的改動。首先,我修改了存儲方法(storage)以便將被捕獲的輸入保存在一個ArrayList中,輸入的每一行都成為該集合中的一個字符串:
clearcase/" target="_blank" >cc>private void grabStream (TextReader inStream)
{
string str;
while (null != (str = inStream.ReadLine ()))
{
listOStrings.Add (str);
// Pipe it to the output:
System.Console.WriteLine (str);
}
}
|
接下來,我添加了一些命令行選項以使它能夠將數據發送到剪貼板中或缺省的打印機上,或者同時發送到這兩處(見javascript:openWindowRes('VS/2003_02/xml2html.asp?xmlfile=ConsoleApplication/List1.xml&xslfile=../../include/xsl/Mylist.xsl');">列表1)。
最后,我寫了兩個程序來將輸出結果打印出來(見列表2)。PrintData()方法用于建立一個PrintDocument對象并啟動該打印過程。它還負責為打印頁面處理添加事件處理程序。
PrintPage()方法用于打印每個頁面。首先,我計算出符合打印頁面的行數。字體的GetHeight()方法顯示了單獨一行的高度,PrintPageEvent的Marginbounds屬性顯示了每個頁面的打印空間有多大。有了這些信息,我便可以簡單地繪制出頁眉行、頁面的每一行以及頁腳行。
在打印框架中有一個小問題:PageBounds屬性代表的是整個頁面的大小,而不是可打印區域的大小。我曾試圖將頁眉和頁腳限定在打印區域以外,但沒有成功。在有些打印機上,打印區域以下的地方是不能被打印出來的。所以我只能簡單地調整頁邊距并將頁眉和頁腳放在打印區域中。
關于作者:
Bill Wagner是SRT Solutions的Windows技術專家。他是Visual Studio Magazine的撰稿編輯,也是The C# Core Language Little Black Book一書的作者,這是一本C#開發人員的高級參考書。在16年的軟件開發實踐中,Bill在許多項目中都是重要的開發人員。他曾為工程和商務應用程序、桌面和Web環境開發過軟件。他在2D和3D圖象和多媒體軟件方面也很有經驗,包括為The Lion King Animated Storybook開發的視頻回放引擎。他的聯系方式是wwagner@SRTSolutions.com。
過程選項
列表1.該程序用于解析命令行以及存儲所有命令行選項和該類的成員變量(member variable)中的輸入文件。
private int parseArgs (string [] args)
{
for (int index = 0; index < args.Length;
index++)
{
string arg = args[index];
if ('-' == arg[0])
{
switch (arg[1])
{
case 'h':
// Help Me:
PrintHelpMessage ();
return 0;
case 'c':
// Copy to clipboard.
setClipboard = true;
break;
case 'p':
printData = true;
break;
default:
// Ooops.
Console.Error.WriteLine
("Invalid Argument: {0}", arg);
PrintHelpMessage ();
return 2;
}
}
else
this.inFile = arg;
// If there were no options, set the
// clipboard data:
if (!(setClipboard || printData))
setClipboard = true;
}
return 0;
}
| |
Print Page事件處理和Print程序
列表2. Print Page事件處理負責將一個單獨輸出結果頁面發送到打印機中。Print程序啟動打印過程。
private void PrintPage(object sender,
PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics)-2;
// Print the header:
string header = inFile;
if (header == null)
header = "Standard input";
StringFormat fmt = new StringFormat(StringFormatFlags.NoWrap);
fmt.Alignment = StringAlignment.Center;
fmt.LineAlignment = StringAlignment.Center;
Rectangle rcHeader = new Rectangle
(ev.MarginBounds.Left, ev.MarginBounds.Top,
ev.MarginBounds.Width,
(int)(printFont.GetHeight (ev.Graphics)+1));
ev.Graphics.DrawString(header, printFont,
Brushes.Black, rcHeader, fmt);
topMargin += rcHeader.Height;
// Print each line of the file.
while((count < linesPerPage) &&
(currentLineToPrint < listOStrings.Count))
{
line = (string) listOStrings[currentLineToPrint];
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont,
Brushes.Black, leftMargin, yPos,
new StringFormat());
count++;
currentLineToPrint++;
}
// Print the footer:
string footer = "Page " + currentPage.ToString() + " of " +
Math.Ceiling ((double)listOStrings.Count /
(double) linesPerPage);
rcHeader.Y = (int)(topMargin +
(linesPerPage+1)*
printFont.GetHeight(ev.Graphics));
rcHeader.Height = (int)(printFont.GetHeight
(ev.Graphics) + 1);
ev.Graphics.DrawString(footer, printFont,
Brushes.Black, rcHeader, fmt);
currentPage++;
// If more lines exist, print another page.
if(currentLineToPrint < listOStrings.Count)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
private void PrintData ()
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.PrintPage);
// Reset the page number and the current line.
currentLineToPrint = 0;
currentPage = 1;
pd.Print();
}
| |
原文轉自:http://www.anti-gravitydesign.com
- 評論列表(網友評論僅供網友表達個人看法,并不表明本站同意其觀點或證實其描述)
-
国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97
|