从打印机打印时出现问题
我正在创建一个桌面应用程序来从 Cognitive Advantage DLX 打印机打印标签。我正在尝试在 Visual C#.Net 中逐行打印。我的程序和打印机工作正常。我曾尝试从打印机中进行“测试打印”,它成功地进行了我的打印测试。但是当我尝试从打印机打印时,它不起作用。另外,我没有收到任何错误。
这是我在打印课上的代码,
class LPrinter
32 {
33 // Structure and API declarions:
34 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
35 public class DOCINFOA
36 {
37 [MarshalAs(UnmanagedType.LPStr)] public string pDocName;
38 [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
39 [MarshalAs(UnmanagedType.LPStr)] public string pDataType;
40 }
41 [DllImport("winspool.Drv", EntryPoint="OpenPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
42 public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
43
44 [DllImport("winspool.Drv", EntryPoint="ClosePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
45 public static extern bool ClosePrinter(IntPtr hPrinter);
46
47 [DllImport("winspool.Drv", EntryPoint="StartDocPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
48 public static extern bool StartDocPrinter( IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
49
50 [DllImport("winspool.Drv", EntryPoint="EndDocPrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
51 public static extern bool EndDocPrinter(IntPtr hPrinter);
52
53 [DllImport("winspool.Drv", EntryPoint="StartPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
54 public static extern bool StartPagePrinter(IntPtr hPrinter);
55
56 [DllImport("winspool.Drv", EntryPoint="EndPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
57 public static extern bool EndPagePrinter(IntPtr hPrinter);
58
59 [DllImport("winspool.Drv", EntryPoint="WritePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
60 public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten );
61
62 /*=================================================*/
63
64 private IntPtr HandlePrinter;
65 private PrinterSettings ps;
66
67 public LPrinter()
68 {
69 HandlePrinter = IntPtr.Zero;
70 ps = new PrinterSettings();
71 }
72
73 public string PrinterName
74 {
75 get
76 {
77 return ps.PrinterName;
78 }
79 set
80 {
81 ps.PrinterName = value;
82 }
83 }
84
85 public bool ChoosePrinter()
86 {
87 PrintDialog pd = new PrintDialog();
88 pd.PrinterSettings = ps;
89 if(pd.ShowDialog()==DialogResult.OK)
90 {
91 ps = pd.PrinterSettings;
92 return true;
93 }
94 else return false;
95 }
96
97 public bool Open(string DocName)
98 {
99 // see if printer is already open
100 if(HandlePrinter!=IntPtr.Zero) return false;
101
102 // opens the printer
103 bool risp = OpenPrinter(ps.PrinterName,out HandlePrinter,IntPtr.Zero);
104 if(risp==false) return false;
105
106 // starts a print job
107 DOCINFOA MyDocInfo = new DOCINFOA();
108 MyDocInfo.pDocName = DocName;
109 MyDocInfo.pOutputFile = null;
110 MyDocInfo.pDataType = "RAW";
111
112 if(StartDocPrinter(HandlePrinter, 1, MyDocInfo))
113 {
114 StartPagePrinter(HandlePrinter); //starts a page
115 return true;
116 }
117 else return false;
118 }
119
120 public bool Close()
121 {
122 if(HandlePrinter==IntPtr.Zero) return false;
123 if(!EndPagePrinter(HandlePrinter)) return false;
124 if(!EndDocPrinter(HandlePrinter)) return false;
125 if(!ClosePrinter(HandlePrinter)) return false;
126 HandlePrinter = IntPtr.Zero;
127 return true;
128 }
129
130 public bool Print(string outputstring)
131 {
132 if(HandlePrinter==IntPtr.Zero) return false;
133
134 IntPtr buf = Marshal.StringToCoTaskMemAnsi(outputstring);
135
136 Int32 done = 0;
137 bool ok = WritePrinter(HandlePrinter,buf,outputstring.Length,out done);
138
139 Marshal.FreeCoTaskMem(buf);
140
141 if(!ok) return false;
142 else return true;
143 }
144
145 }
有人可以帮助我吗?
I am creating an desktop application to print label from Cognitive Advantage DLX printer. I am trying to print line by line in Visual C#.Net. My program and printer are working properly. I had try to take "Test Print" from the printer and it gives my print test successfully. But when I try to take print from my printer it is not working. Also, I am not getting any error.
Here is my code from print class,
class LPrinter
32 {
33 // Structure and API declarions:
34 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
35 public class DOCINFOA
36 {
37 [MarshalAs(UnmanagedType.LPStr)] public string pDocName;
38 [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
39 [MarshalAs(UnmanagedType.LPStr)] public string pDataType;
40 }
41 [DllImport("winspool.Drv", EntryPoint="OpenPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
42 public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
43
44 [DllImport("winspool.Drv", EntryPoint="ClosePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
45 public static extern bool ClosePrinter(IntPtr hPrinter);
46
47 [DllImport("winspool.Drv", EntryPoint="StartDocPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
48 public static extern bool StartDocPrinter( IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
49
50 [DllImport("winspool.Drv", EntryPoint="EndDocPrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
51 public static extern bool EndDocPrinter(IntPtr hPrinter);
52
53 [DllImport("winspool.Drv", EntryPoint="StartPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
54 public static extern bool StartPagePrinter(IntPtr hPrinter);
55
56 [DllImport("winspool.Drv", EntryPoint="EndPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
57 public static extern bool EndPagePrinter(IntPtr hPrinter);
58
59 [DllImport("winspool.Drv", EntryPoint="WritePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
60 public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten );
61
62 /*=================================================*/
63
64 private IntPtr HandlePrinter;
65 private PrinterSettings ps;
66
67 public LPrinter()
68 {
69 HandlePrinter = IntPtr.Zero;
70 ps = new PrinterSettings();
71 }
72
73 public string PrinterName
74 {
75 get
76 {
77 return ps.PrinterName;
78 }
79 set
80 {
81 ps.PrinterName = value;
82 }
83 }
84
85 public bool ChoosePrinter()
86 {
87 PrintDialog pd = new PrintDialog();
88 pd.PrinterSettings = ps;
89 if(pd.ShowDialog()==DialogResult.OK)
90 {
91 ps = pd.PrinterSettings;
92 return true;
93 }
94 else return false;
95 }
96
97 public bool Open(string DocName)
98 {
99 // see if printer is already open
100 if(HandlePrinter!=IntPtr.Zero) return false;
101
102 // opens the printer
103 bool risp = OpenPrinter(ps.PrinterName,out HandlePrinter,IntPtr.Zero);
104 if(risp==false) return false;
105
106 // starts a print job
107 DOCINFOA MyDocInfo = new DOCINFOA();
108 MyDocInfo.pDocName = DocName;
109 MyDocInfo.pOutputFile = null;
110 MyDocInfo.pDataType = "RAW";
111
112 if(StartDocPrinter(HandlePrinter, 1, MyDocInfo))
113 {
114 StartPagePrinter(HandlePrinter); //starts a page
115 return true;
116 }
117 else return false;
118 }
119
120 public bool Close()
121 {
122 if(HandlePrinter==IntPtr.Zero) return false;
123 if(!EndPagePrinter(HandlePrinter)) return false;
124 if(!EndDocPrinter(HandlePrinter)) return false;
125 if(!ClosePrinter(HandlePrinter)) return false;
126 HandlePrinter = IntPtr.Zero;
127 return true;
128 }
129
130 public bool Print(string outputstring)
131 {
132 if(HandlePrinter==IntPtr.Zero) return false;
133
134 IntPtr buf = Marshal.StringToCoTaskMemAnsi(outputstring);
135
136 Int32 done = 0;
137 bool ok = WritePrinter(HandlePrinter,buf,outputstring.Length,out done);
138
139 Marshal.FreeCoTaskMem(buf);
140
141 if(!ok) return false;
142 else return true;
143 }
144
145 }
Can any help me out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论