WPF 深入研究 之 Print 列印

來源:互聯網
上載者:User

本章共計51個樣本,全都在VS2008下.NET3.5測試通過,點擊這裡下載: Printing.rar

1.PrintDialog

This sample illustrates how to create an instance of a simple PrintDialog and then display it. The sample uses both Extensible Application Markup Language (XAML) and procedural code.

這個樣本示範了如何進行一個最簡單的列印工作,為此需要引入兩個dll:ReachFramework.dll和System.Printing。

InvokePrint方法只是顯示了一個PrintDialog列印框,並未進行列印工作:

            PrintDialog pDialog = new PrintDialog();
            pDialog.PageRangeSelection = PageRangeSelection.AllPages;
            pDialog.UserPageRangeEnabled = true;
            pDialog.ShowDialog();

有PrintableAreaHeight和PrintableAreaWidth兩個屬性,分別用來表示可列印範圍的高和寬。

而對PrintDialog的設定,可以儲存在PrintTicket中,下次再開啟PrintDialog,就不必重複進行設定了。

            PrintDialog pDialog = new PrintDialog();
            PrintTicket pt = pDialog.PrintTicket;   

同樣,選擇使用哪一台印表機的設定,存放在PrintQueue中,下次再開啟PrintDialog,也不用再次設定了。

            PrintDialog pDialog = new PrintDialog();
            PrintQueue pq = pDialog.PrintQueue;    

如果要把特定的內容列印輸出,則需要調用PrintDialog的PrintVisual方法:

            if ((bool)pDialog.ShowDialog().GetValueOrDefault())
            {
                DrawingVisual vis = new DrawingVisual();
                DrawingContext dc = vis.RenderOpen();
                dc.DrawLine(new Pen(), new Point(0, 0), new Point(0, 1));
                dc.Close();
                pDialog.PrintVisual(vis, "Hello, world!");
            }

我們能列印的,都是Visual類型的對象,其中UIElement派生於Visual,從而我們可以列印所有Panel、控制項和其它元素,最一般的方法是使用派生於Visual的DrawingVisual類,利用它的RenderOpen方法產生DrawingContext對象,為其繪製圖形,最後使用PrintDialog的PrintVisual方法,輸出圖形和文字。

注意到,pDialog.ShowDialog()返回的是可空類型?bool,為此需要使用GetValueOrDefault將其轉為bool值,對於null值也會轉為false。

2.EnumerateSubsetOfPrintQueues

EnumerateSubsetOfPrintQueues shows how to use the EnumeratedPrintQueueTypes enumeration to get a subset of available print queues.

這個程式示範了如何得到本地和共用的所有印表機列表。為此,需要使用到EnumeratedPrintQueueTypes枚舉中的Local和Shared兩個值,組合成一個數組,

 

            EnumeratedPrintQueueTypes[] enumerationFlags = {EnumeratedPrintQueueTypes.Local,
                                                          EnumeratedPrintQueueTypes.Shared};

作為參數傳遞到查詢方法

GetPrintQueues中:

            LocalPrintServer printServer = new LocalPrintServer();
            PrintQueueCollection printQueuesOnLocalServer = printServer.GetPrintQueues(enumerationFlags);

接著就可以對

PrintQueueCollection進行遍曆了,擷取每一個的PrintQueue名稱和所在位置:

            foreach (PrintQueue printer in printQueuesOnLocalServer)
            {
                Console.WriteLine(""tThe shared printer " + printer.Name + " is located at " + printer.Location + ""n");
            }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.