您的位置:程序门 -> .net技术 -> c#



直接输出字符串到远程打印机,出问题,贴代码,100分急求解!在线等


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


直接输出字符串到远程打印机,出问题,贴代码,100分急求解!在线等[已结贴,结贴人:laurel218]
发表于:2007-03-26 10:01:16 楼主
代码可以直接输出到远程打印机,但是a4纸上的却是乱码,代码如下,急求解,请高手指教!谢谢

public   partial   class   form1   :   form
{
        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   button2_click(object   sender,   eventargs   e)
                {
                        if   (printstr   ==   " "   ¦ ¦   printstr   ==   null)
                        {
                                messagebox.show( "对不起,现在没有要打印的数据! ");
                        }
                        else  
                        {
                                system.intptr   lhprinter   =   new   system.intptr();

                                docinfo   di   =   new   docinfo();
                                int   pcwritten   =   0;

                                printdirect.openprinter( "\\\\192.168.112.5\\1150pcl   5e ",   ref   lhprinter,   0);
                                printdirect.startdocprinter(lhprinter,   1,   ref   di);
                                printdirect.startpageprinter(lhprinter);

                                myreader   =   new   stringreader(this.richtextbox1.text);

                                try
                                {
                                        printdirect.writeprinter(lhprinter,   ds.tables[ "print "].rows[0][ "data "].tostring(),   printstr.length,   ref   pcwritten);
                                }
                                catch   (exception   ee)
                                {
                                        console.writeline(ee.message);
                                }

                                printdirect.endpageprinter(lhprinter);
                                printdirect.enddocprinter(lhprinter);
                                printdirect.closeprinter(lhprinter);
                        }
                }
}

ps:
打印代码为:
printdirect.writeprinter(lhprinter,   ds.tables[ "print "].rows[0][ "data "].tostring(),   printstr.length,   ref   pcwritten);

ds.tables[ "print "].rows[0][ "data "].tostring()//这是一段带格式的字符串

发表于:2007-03-26 10:06:411楼 得分:10
乱码?会不会是编码没有统一啊
发表于:2007-03-26 10:08:042楼 得分:0
请问编码应该怎么统一?
发表于:2007-03-26 10:08:153楼 得分:10
ms-help://ms.msdnqtr.2003feb.2052/gdi/prntspol_93g2.htm
发表于:2007-03-26 10:13:244楼 得分:0
楼上兄弟,你给的连接在msdn2005里面打不开啊
发表于:2007-03-26 10:30:075楼 得分:20
ms-help://ms.msdnqtr.v80.chs/ms.msdn.v80/ms.win32com.v10.en/gdi/prntspol_93g2.htm
发表于:2007-03-26 10:48:316楼 得分:0
谢谢楼上的兄弟,我把找到的代码贴出来,还有问题请教一下:

//   rawdatatoprinter   -   sends   binary   data   directly   to   a   printer
            //  
            //   szprintername:   null-terminated   string   specifying   printer   name
            //   lpdata:                 pointer   to   raw   data   bytes
            //   dwcount                 length   of   lpdata   in   bytes
            //  
            //   returns:   true   for   success,   false   for   failure.
            //  
              bool   rawdatatoprinter(lpstr   szprintername,   lpbyte   lpdata,   dword   dwcount)
            {
                handle           hprinter;
                doc_info_1   docinfo;
                dword             dwjob;
                dword             dwbyteswritten;

                //   need   a   handle   to   the   printer.
                if(   !   openprinter(   szprintername,   &hprinter,   null   )   )
                    return   false;

                //   fill   in   the   structure   with   info   about   this   "document. "
                docinfo.pdocname   =   "my   document ";
                docinfo.poutputfile   =   null;
                docinfo.pdatatype   =   "raw ";
                //   inform   the   spooler   the   document   is   beginning.
                if(   (dwjob   =   startdocprinter(   hprinter,   1,   (lpstr)&docinfo   ))   ==   0   )
                {
                    closeprinter(   hprinter   );
                    return   false;
                }
                //   start   a   page.
                if(   !   startpageprinter(   hprinter   )   )
                {
                    enddocprinter(   hprinter   );
                    closeprinter(   hprinter   );
                    return   false;
                }
                //   send   the   data   to   the   printer.
                if(   !writeprinter(   hprinter,   lpdata,   dwcount,   &dwbyteswritten   )   )
                {
                    endpageprinter(   hprinter   );
                    enddocprinter(   hprinter   );
                    closeprinter(   hprinter   );
                    return   false;
                }
                //   end   the   page.
                if(   !   endpageprinter(   hprinter   )   )
                {
                    enddocprinter(   hprinter   );
                    closeprinter(   hprinter   );
                    return   false;
                }
                //   inform   the   spooler   that   the   document   is   ending.
                if(   !   enddocprinter(   hprinter   )   )
                {
                    closeprinter(   hprinter   );
                    return   false;
                }
                //   tidy   up   the   printer   handle.
                closeprinter(   hprinter   );
                //   check   to   see   if   correct   number   of   bytes   were   written.
                if(   dwbyteswritten   !=   dwcount   )
                    return   false;
                return   true;
            }

bool   rawdatatoprinter(lpstr   szprintername,   lpbyte   lpdata,   dword   dwcount)

lpstr   szprintername   是不是远程打印机名称?格式是 "\\\\打印ip\\打印机器名 "   ?
lpbyte   lpdata   指什么?格式是什么?
dword   dwcount   是不是指输出的字符串,可不可以是带格式的字符串?格式是怎么样的?

发表于:2007-03-26 10:58:577楼 得分:30
编码问题吧?charset   里改一下看看
1、试下byte[]     编码格式,
这个api用的是本机编码的吧,在形成byte[]时用encoding的unicode编码,或者utf8编码试下,在system.text里,
不过得将声明改一下
[dllimport( "winspool.drv ",   callingconvention=callingconvention.stdcall,   charset=charset.??,   setlasterror=true,   exactspelling=true)]
  public   static   extern   bool   writeprinter(intptr   hprinter,   byte[]   pbytes,   int   dwcount,   ref   int   dwwritten);

2、可能是驱动问题,驱动程序不正确就打印不了中文!因为很多激光和喷墨打印机都没有汉字库,所以无法直接打印中文,在windows下只有借助驱动程序,打印机才可以打印中文!
发表于:2007-03-26 11:04:288楼 得分:0
我打印的是英文呢,也是乱码
发表于:2007-03-26 11:13:319楼 得分:10
请试下解决方法1
发表于:2007-03-26 11:14:0710楼 得分:20
try
lpbyte   lpdata   ===>   system.intptr;
string   str   =   ...
system.intptr   lpdata   =   system.text.encoding.default.getbytes(   str   );
发表于:2007-03-26 12:39:3811楼 得分:0
谢谢,楼上兄弟,请教一下:
找不到类型或命名空间名称“lpstr”(是否缺少   using   指令或程序集引用?)

在msdn里面找到的是:using   system.runtime.interopservices;  
但是没用

麻烦告诉一下命名空间
发表于:2007-03-26 13:05:0712楼 得分:0
我想把需要打印的带格式的字符串生成到word文档里面,然后打印这个word,这样的话,格式比较好控制
请高手兄弟们指教啊!
发表于:2007-03-27 16:44:0013楼 得分:0
再顶起来,求解!


快速检索

最新资讯
热门点击