我想利用.net得到打印机的hdc,可是printDocument类找不到,请问有什么办法能够得到,谢谢!!

解决方案 »

  1.   

    你可以在printdocument的打印事件里获得
    Graphics 
    再通过这个Graphics.GetHdc()
      

  2.   

    那如何验证得到的hdc是对的呢,我就是这么写的,可是在系统中没有反应
      

  3.   

    public class PrintingExample 
    {
        private Font printFont;
        private StreamReader streamToPrint;
        static string filePath;
        public PrintingExample() 
        {
            Printing();
        }    // The PrintPage event is raised for each page to be printed.
        private void pd_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) ;        // Iterate over the file, printing each line.
            while (count < linesPerPage && 
               ((line=streamToPrint.ReadLine()) != null)) 
            {
               yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
               ev.Graphics.DrawString (line, printFont, Brushes.Black, 
                  leftMargin, yPos, new StringFormat());
               count++;
            }        // If more lines exist, print another page.
            if (line != null) 
               ev.HasMorePages = true;
            else 
               ev.HasMorePages = false;
        }    // Print the file.
        public void Printing()
        {
            try 
            {
               streamToPrint = new StreamReader (filePath);
               try 
               {
                  printFont = new Font("Arial", 10);
                  PrintDocument pd = new PrintDocument(); 
                  pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                  // Print the document.
                  pd.Print();
               } 
               finally 
               {
                  streamToPrint.Close() ;
               }
           } 
           catch(Exception ex) 
           { 
               MessageBox.Show(ex.Message);
           }
        }
      
        // This is the main entry point for the application.
        public static void Main(string[] args) 
        {
           string sampleName = Environment.GetCommandLineArgs()[0];
           if(args.Length != 1)
           {
              Console.WriteLine("Usage: " + sampleName +" <file path>");
              return;
           }
           filePath = args[0];
           new PrintingExample();
        }
    }
      

  4.   

    直接用Graphics 输出。不需要HDC.
      

  5.   

    还有,倦怠大哥,我用了printDocument.Print(),我只想触发它的打印事件,得到hdc,单并不想利用它的打印功能,能不能用了printDocument.Print()之后,只触发事件,但不打印??
    我用  ev.HasMorePages = false  但是还是打了,有没有让它停止打印的语句
      

  6.   

    对了,我忘了说啦,我用的是第三方控件打印,但需要传递hdc变量,所以只想得到hdc,别的不要(二次开发)
    我用 
    ev.Graphics.GetHdc();
    中间给那个控件的hdc附值,结果没有效果
    ev.Graphics.ReleaseHdc((hDc);
    但我用VB的hdc属性负值后就可以打印,不知道原因???
      

  7.   

    第三方控件你试试
    调用beginpaint api
     来获得hdc
    HDC BeginPaint(
      HWND hwnd,            // handle to window
      LPPAINTSTRUCT lpPaint // paint information
    );
      

  8.   

    我用VB的控件已经做成了,但是现在不想借助第三方控间得到hdc,只想用c#自己的功能,很不好办呀,还有别的办法吗?
      

  9.   

    我这个不是第三方的
    beginPaint()
    是windows api