瑞峰的博客

导航

隐藏很讨厌的“取消”打印对话框,并通过异步回调,取得异步操作的返回结果

隐藏很讨厌的“取消”打印对话框,并通过异步回调,取得异步操作的返回结果

以打印为例:
using System.Drawing.Printing;
首先:声明委托
 delegate void PrintInBackgroundDelegate();
 private TextPrintDocument printDoc = new TextPrintDocument();

 private void toPrint()
 {
  //连续打印时,加入打印控制器,隐藏很讨厌的“取消”打印对话框。
  PrintController printController = new StandardPrintController();
  printDoc.PrintController = printController;

  printDoc.PrintPage+=new PrintPageEventHandler(printDoc_PrintPage);

  PrintInBackgroundDelegate d = new

PrintInBackgroundDelegate(PrintInBackground);
  d.BeginInvoke(new AsyncCallback(PrintInBackgroundComplete), null);
 }

 private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
 {
  e.Graphics.DrawImage(pictureBox1.Image,1,1);
 }

        private void PrintInBackground()
        {
            try
            {
                printDoc.Print();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, Application.ProductName, MessageBoxButtons.OK,

MessageBoxIcon.Error);
            }
        }

        private void PrintInBackgroundComplete(IAsyncResult r)
        {
     //messagebox.show("异步打印操作已经处理完毕");
 }

 

posted on 2006-04-29 17:53  峰子  阅读(794)  评论(3)    收藏  举报