CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

往并口打印不好使,高手请进!今天必须解决!

楼主gasover(无尽)2003-09-03 18:56:20 在 .NET技术 / C# 提问

using   System;  
  using   System.Drawing;  
  using   System.Collections;  
  using   System.ComponentModel;  
  using   System.Windows.Forms;  
  using   System.Data;  
  using   System.Text;  
  using   System.Runtime.InteropServices;  
  namespace   sy  
  {  
  ///   <summary>  
  ///   Form1   的摘要说明。  
  ///   </summary>  
  public   class   Form1   :   System.Windows.Forms.Form  
  {  
  private   System.Drawing.Printing.PrintDocument   printBill;  
  private   System.Windows.Forms.Button   button1;  
  private   System.Windows.Forms.Button   button2;  
  ///   <summary>  
  ///   必需的设计器变量。  
  ///   </summary>  
  private   System.ComponentModel.Container   components   =   null;  
   
  public   Form1()  
  {  
  //  
  //   Windows   窗体设计器支持所必需的  
  //  
  InitializeComponent();  
   
  //  
  //   TODO:   在   InitializeComponent   调用后添加任何构造函数代码  
  //  
  }  
   
  ///   <summary>  
  ///   清理所有正在使用的资源。  
  ///   </summary>  
  protected   override   void   Dispose(   bool   disposing   )  
  {  
  if(   disposing   )  
  {  
  if   (components   !=   null)    
  {  
  components.Dispose();  
  }  
  }  
  base.Dispose(   disposing   );  
  }  
   
  #region   Windows   Form   Designer   generated   code  
  ///   <summary>  
  ///   设计器支持所需的方法   -   不要使用代码编辑器修改  
  ///   此方法的内容。  
  ///   </summary>  
  private   void   InitializeComponent()  
  {  
  this.printBill   =   new   System.Drawing.Printing.PrintDocument();  
  this.button1   =   new   System.Windows.Forms.Button();  
  this.button2   =   new   System.Windows.Forms.Button();  
  this.SuspendLayout();  
  //    
  //   printBill  
  //    
  this.printBill.PrintPage   +=   new   System.Drawing.Printing.PrintPageEventHandler(this.printBill_PrintPage);  
  //    
  //   button1  
  //    
  this.button1.Location   =   new   System.Drawing.Point(112,   96);  
  this.button1.Name   =   "button1";  
  this.button1.TabIndex   =   0;  
  this.button1.Text   =   "button1";  
  this.button1.Click   +=   new   System.EventHandler(this.button1_Click);  
  //    
  //   button2  
  //    
  this.button2.Location   =   new   System.Drawing.Point(144,   160);  
  this.button2.Name   =   "button2";  
  this.button2.TabIndex   =   1;  
  this.button2.Text   =   "button2";  
  this.button2.Click   +=   new   System.EventHandler(this.button2_Click);  
  //    
  //   Form1  
  //    
  this.AutoScaleBaseSize   =   new   System.Drawing.Size(6,   14);  
  this.ClientSize   =   new   System.Drawing.Size(292,   273);  
  this.Controls.AddRange(new   System.Windows.Forms.Control[]   {  
      this.button2,  
      this.button1});  
  this.Name   =   "Form1";  
  this.Text   =   "Form1";  
  this.ResumeLayout(false);  
   
  }  
  #endregion  
   
  ///   <summary>  
  ///   应用程序的主入口点。  
  ///   </summary>  
  [STAThread]  
  static   void   Main()    
  {  
  Application.Run(new   Form1());  
  }  
  [StructLayout(   LayoutKind.Sequential)]  
  public   struct   DOCINFO  
  {  
  [MarshalAs(UnmanagedType.LPWStr)]public   string   pDocName;  
  [MarshalAs(UnmanagedType.LPWStr)]public   string   pOutputFile;  
  [MarshalAs(UnmanagedType.LPWStr)]public   string   pDataType;  
  }  
   
  public   class   PrintDirect  
  {  
  [   DllImport(   "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,  
      CallingConvention=CallingConvention.StdCall   )]  
  public   static   extern   long   OpenPrinter(string   pPrinterName,ref   IntPtr   phPrinter,  
  int   pDefault);  
  [   DllImport(   "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,  
      CallingConvention=CallingConvention.StdCall   )]  
  public   static   extern   long   StartDocPrinter(IntPtr   hPrinter,   int   Level,  
  ref   DOCINFO   pDocInfo);  
   
  [   DllImport("winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,  
      CallingConvention=CallingConvention.StdCall)]  
  public   static   extern   long   StartPagePrinter(IntPtr   hPrinter);  
  [   DllImport(   "winspool.drv",CharSet=CharSet.Ansi,ExactSpelling=true,  
      CallingConvention=CallingConvention.StdCall)]  
  public   static   extern   long   WritePrinter(IntPtr   hPrinter,string   data,  
  int   buf,ref   int   pcWritten);  
   
  [   DllImport(   "winspool.drv"   ,CharSet=CharSet.Unicode,ExactSpelling=true,  
      CallingConvention=CallingConvention.StdCall)]  
  public   static   extern   long   EndPagePrinter(IntPtr   hPrinter);  
   
  [   DllImport(   "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,  
      CallingConvention=CallingConvention.StdCall)]  
  public   static   extern   long   EndDocPrinter(IntPtr   hPrinter);  
   
  [   DllImport("winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,  
      CallingConvention=CallingConvention.StdCall   )]  
  public   static   extern   long   ClosePrinter(IntPtr   hPrinter);  
  }  
   
  private   void   printBill_PrintPage(object   sender,   System.Drawing.Printing.PrintPageEventArgs   e)  
  {  
  //Print   thePrint=new   Print   ();  
  try  
  {  
  //     说明     thePrint.IntFontSize     为打印的字体大小       默认为   9  
  //     thePrint.IntXspace         用来整体移动一下数据的       列     位置     默认为   0  
  //     thePrint.IntYspace         用来整体移动一下数据的       行     位置     默认为   0  
  // thePrint.thePrintDetail   ();  
  //批准文号  
  e.Graphics.DrawString   ("1221",new   Font   ("Arial",9),Brushes.Black,0,10   );  
  //批准编号  
  e.Graphics.DrawString   ("12212",new   Font   ("Arial",9),Brushes.Black,4,10);  
  //批准日期  
  e.Graphics.DrawString   ("2003-01-01",new   Font   ("Arial",9),Brushes.Black,8,12);  
  //路段名  
  e.Graphics.DrawString   ("21",new   Font   ("Arial",9),Brushes.Black,12,18);  
  //金额  
  e.Graphics.DrawString   ("12",new   Font   ("Arial",9),Brushes.Black,16,28);  
  //车道       工号       票号  
  e.Graphics.DrawString   ("121",new   Font   ("Arial",9),Brushes.Black,24,40);  
  e.Graphics.DrawString   ("1221",new   Font   ("Arial",9),Brushes.Black,30,46);  
  e.Graphics.DrawString   ("12123213",new   Font   ("Arial",9),Brushes.Black,56,50);  
  //日期         时间  
  ///e.Graphics.DrawString   (""+DateTime.Now.ToShortDateString   (),new   Font   ("Arial",9),Brushes.Black,40,60   );  
  // e.Graphics.DrawString   (""+DateTime.Now.ToShortTimeString   (),new   Font   ("Arial",9),Brushes.Black,60.70);  
  }  
  catch(System.Exception   ex)  
  {  
  //MessageBox.Show(ex.ToString(),"打印错误");  
  }  
   
  //------------------  
  }  
   
  private   void   button1_Click(object   sender,   System.EventArgs   e)  
  {  
  this.printBill.Print();  
  }  
  什么反映也没有,不知道什么意思 问题点数:50、回复次数:2Top

1 楼gasover(无尽)回复于 2003-09-03 18:57:32 得分 0

private   void   button2_Click(object   sender,   System.EventArgs   e)  
  {  
  System.IntPtr   lhPrinter=new   System.IntPtr();  
   
  DOCINFO   di   =   new   DOCINFO();  
  int   pcWritten=0;  
  string   st1;  
   
  //   text   to   print   with   a   form   feed   character  
  st1="This   is   an   example   of   printing   directly   to   a   printer\f";  
  di.pDocName="my   test   document";  
  di.pDataType="RAW";  
   
  //   the   \x1b   means   an   ascii   escape   character  
  st1="\x1b*c600a6b0P\f";  
  //lhPrinter   contains   the   handle   for   the   printer   opened  
  //If   lhPrinter   is   0   then   an   error   has   occured  
  long   j=PrintDirect.OpenPrinter("\\\\192.168.1.4\\Star",ref   lhPrinter,0);  
  PrintDirect.StartDocPrinter(lhPrinter,1,ref   di);  
  PrintDirect.StartPagePrinter(lhPrinter);  
  try  
  {  
  //   Moves   the   cursor   900   dots   (3   inches   at   300   dpi)   in   from   the   left   margin,   and  
  //   600   dots   (2   inches   at   300   dpi)   down   from   the   top   margin.  
  st1="\x1b*p900x600Y";  
  PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref   pcWritten);  
   
  //   Using   the   print   model   commands   for   rectangle   dimensions,   "600a"   specifies   a   rectangle  
  //   with   a   horizontal   size   or   width   of   600   dots,   and   "6b"   specifies   a   vertical  
  //   size   or   height   of   6   dots.   The   0P   selects   the   solid   black   rectangular   area   fill.  
  st1="\x1b*c600a6b0P";  
  PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref   pcWritten);  
   
  //   Specifies   a   rectangle   with   width   of   6   dots,   height   of   600   dots,   and   a  
  //   fill   pattern   of   solid   black.  
  st1="\x1b*c6a600b0P";  
  PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref   pcWritten);  
  //   Moves   the   current   cursor   position   to   900   dots,   from   the   left   margin   and  
  //   1200   dots   down   from   the   top   margin.  
  st1="\x1b*p900x1200Y";  
  PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref   pcWritten);  
  //   Specifies   a   rectangle   with   a   width   of   606   dots,   a   height   of   6   dots   and   a  
   
  //   fill   pattern   of   solid   black.  
  st1="\x1b*c606a6b0P";  
  PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref   pcWritten);  
   
  //   Moves   the   current   cursor   position   to   1500   dots   from   the   left   margin   and  
  //   600   dots   down   from   the   top   margin.  
  st1="\x1b*p1500x600Y";  
  PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref   pcWritten);  
   
  //   Specifies   a   rectangle   with   a   width   of   6   dots,   a   height   of   600   dots   and   a  
  //   fill   pattern   of   solid   black.  
  st1="\x1b*c6a600b0P";  
  PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref   pcWritten);  
  //   Send   a   form   feed   character   to   the   printer  
  st1="\f";  
  PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref   pcWritten);  
  }  
  catch   (Exception   e1)  
  {  
  MessageBox.Show("打印机器");  
  }  
   
  PrintDirect.EndPagePrinter(lhPrinter);  
  PrintDirect.EndDocPrinter(lhPrinter);  
  PrintDirect.ClosePrinter(lhPrinter);  
   
  }  
  btn2的代码是有用的,  
  可什么反映也没有Top

2 楼raulredondo()回复于 2003-09-03 20:42:22 得分 50

代码实在太多了,头晕Top

相关问题

  • 串口打印
  • 如何往并口送数据,如打印机编程
  • 捕获发往打印机端口的数据
  • 数据窗口打印?
  • 数据窗口的打印.....
  • 打印 数据窗口
  • 窗口打印问题
  • 串口打印机问题
  • 并口打印机如何直接写并口打印??
  • 数据窗口打印的问题(打印顺序号)

关键词

  • unicode
  • callingconvention
  • printbill
  • theprint
  • winspool
  • exactspelling
  • drawstring
  • button
  • charset
  • intptr hprinter

得分解答快速导航

  • 帖主:gasover
  • raulredondo

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo