kingno
2025-06-09 305e2990bb2161177e87ab7711f9dfb205762bcd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
 
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.UI.Model.Winform;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;
using static System.Windows.Forms.AxHost;
 
//using System.Printing;
 
namespace Bro.M141.Process
{
    [MenuNode("PrinterStatus", "打印机显示界面", 2, EnumHelper.TopMenu.SystemInfo, MenuNodeType.Form)]
    public partial class UIPrinter : MenuFormBase
    //public partial class UIPrinter : UserControl
    {
        M141Config Config141 => Process.IConfig as M141Config;
        M141Process Process141 => Process as M141Process;
 
 
        public UIPrinter()
        {
            InitializeComponent();
        }
 
 
        public override void OnProcessUpdated()
        {
            base.OnProcessUpdated();
            printDocument1.PrintPage += new PrintPageEventHandler(printDocument_Print);
 
            Process141.StartPrinter += StartPrint;
            textBox1.Text = "Broconcentric";
        }
 
 
 
        PrintDocument printDocument1 = new PrintDocument();
        int ttwith = (int)(80 * 4);
        int ttheigh = (int)(40 * 4);
        string message = "";
 
        public void StartPrint(string str)
        {
            try
            {
                ttwith = 320;
                ttheigh = 160;
                message = str;
 
                if (string.IsNullOrEmpty(message))
                {
                    message = textBox1.Text;
                }
 
                this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = "Honeywell PX240S (300 dpi)";
                this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", ttwith, ttheigh);
 
                this.printDocument1.PrintController = new System.Drawing.Printing.StandardPrintController();
                this.printDocument1.Print();
 
            }
            catch
            {
 
            }
 
            int statuscode = GetPrinterStatusCodeInt();
            string status = GetPrinterStatusMessage(statuscode);
 
            //Config141
            Process141.PlcwritePrinter(1520, 0);
            Process141.PlcwritePrinter(1510, 1);
 
        }
        private void printDocument_Print(object sender, PrintPageEventArgs e)
        {
            Font fntTxt = new Font("黑体", 15, System.Drawing.FontStyle.Bold);//正文文字               
            System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black);//画刷           
            try
            {
                string numvalue = message;
 
                float Scale = (float)0.95;
                int startX = 10;
                int startY = 25;
 
                //int length = 320 * Scale;
                //int height = 160 * Scale;
                var g = e.Graphics;
 
                Font font = new Font("Arial", 8 * Scale);//设置字体颜色
                Font font2 = new Font("Arial", 11 * Scale);//设置字体颜色
                Font font3 = new Font("Arial", 36 * Scale);//设置字体颜色
                Font font4 = new Font("Arial", 12 * Scale);//设置字体颜色
 
 
                Pen p = new Pen(Color.Black, Scale);//定义了一个黑色,宽度为1的画笔
                g.Clear(Color.White);
                g.DrawRectangle(p, startX * Scale, startY * Scale, 70 * Scale, 20 * Scale);//在画板上画矩形,起始坐标为(10,10),宽为80,高为20
                g.DrawRectangle(p, startX * Scale, startY * Scale, 280 * Scale, 20 * Scale);//在画板上画矩形,起始坐标为(90,10),宽为80,高为20
                g.DrawString("厂商", font, brush, (startX + 20) * Scale, (startY + 5) * Scale);//
                g.DrawString("嘉彰科技", font2, brush, (startX + 140) * Scale, (startY + 4) * Scale);
 
 
                //第二行数据
                g.DrawRectangle(p, startX * Scale, (startY + 20) * Scale, 70 * Scale, 20 * Scale);
                g.DrawRectangle(p, startX * Scale, (startY + 20) * Scale, 280 * Scale, 20 * Scale);
                g.DrawString("品名", font, brush, (startX + 20) * Scale, (startY + 25) * Scale);
                g.DrawString(@"COVER LG208H-RC100 #2", font4, brush, (startX + 71) * Scale, (startY + 22) * Scale);
 
                //第三行数据
                g.DrawRectangle(p, startX * Scale, (startY + 40) * Scale, 70 * Scale, 20 * Scale);
                g.DrawRectangle(p, startX * Scale, (startY + 40) * Scale, 280 * Scale, 20 * Scale);
                g.DrawString("料号", font, brush, (startX + 20) * Scale, (startY + 45) * Scale);
                g.DrawString(@"436LG208I000", font2, brush, (startX + 130) * Scale, (startY + 43) * Scale);
 
                //第四行数据
                g.DrawRectangle(p, startX * Scale, (startY + 60) * Scale, 70 * Scale, 20 * Scale);
                g.DrawRectangle(p, startX * Scale, (startY + 60) * Scale, 210 * Scale, 20 * Scale);
                g.DrawString("数量", font, brush, (startX + 20) * Scale, (startY + 65) * Scale);
                g.DrawString(@"50PCS", font2, brush, (startX + 120) * Scale, (startY + 62) * Scale);
 
                //第五行数据
                g.DrawRectangle(p, startX * Scale, (startY + 80) * Scale, 70 * Scale, 20 * Scale);
                g.DrawRectangle(p, startX * Scale, (startY + 80) * Scale, 210 * Scale, 20 * Scale);
                g.DrawString("生产日期", font, brush, (startX + 10) * Scale, (startY + 85) * Scale);
                g.DrawString($"{DateTime.Now.ToString("yyyy.MM.dd")}", font2, brush, (startX + 105) * Scale, (startY + 82) * Scale);
 
                //第六行数据
                g.DrawRectangle(p, startX * Scale, (startY + 100) * Scale, 70 * Scale, 20 * Scale);
                g.DrawRectangle(p, startX * Scale, (startY + 100) * Scale, 140 * Scale, 20 * Scale);
                g.DrawRectangle(p, startX * Scale, (startY + 100) * Scale, 210 * Scale, 20 * Scale);
                g.DrawString("MARK1", font, brush, (startX + 15) * Scale, (startY + 104) * Scale);
                g.DrawString("MARK2", font, brush, (startX + 155) * Scale, (startY + 104) * Scale);
 
 
                //侧面
                g.DrawRectangle(p, (startX + 210) * Scale, (startY + 60) * Scale, 70 * Scale, 60 * Scale);
                g.DrawString(@"#2", font3, brush, (startX + 210) * Scale, (startY + 62) * Scale);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
 
        private void printDocument_Print222(object sender, PrintPageEventArgs e)
        {
            Font fntTxt = new Font("黑体", 15, System.Drawing.FontStyle.Bold);//正文文字               
            System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black);//画刷           
            try
            {
                string numvalue = message;
                //Bitmap bitmap = getexcel();// CreateCode(numvalue);
 
                //int with = (ttwith - bitmap.Width) / 2 - 20;
                //int heih = (ttheigh - bitmap.Height) / 2;
                //條碼的位置
                //e.Graphics.DrawImage(bitmap, new System.Drawing.Point(0, 0));
                //條碼信息數字的位置
                //e.Graphics.DrawString(numvalue, fntTxt, brush, new System.Drawing.Point(bitmap.Width / 4 + with - 10, heih + bitmap.Height + 5));
 
 
                //e.Graphics.DrawImage(img, new Rectangle(25, 25, bmpwidth, bmpheight), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
                int Scale = 1;
                int startX = 10;
                int startY = 25;
 
                int length = 320 * Scale;
                int height = 160 * Scale;
                var g = e.Graphics;
 
                Font font = new Font("Arial", 8 * Scale);//设置字体颜色
                Font font2 = new Font("Arial", 11 * Scale);//设置字体颜色
                Font font3 = new Font("Arial", 36 * Scale);//设置字体颜色
                Font font4 = new Font("Arial", 12 * Scale);//设置字体颜色
 
 
                //SolidBrush brush = new SolidBrush(Color.Black);//新建一个画刷,到这里为止,我们已经准备好了画板、画刷、和数据
 
 
                Pen p = new Pen(Color.Black, Scale);//定义了一个黑色,宽度为1的画笔
                g.Clear(Color.White); //设置白色背景
                                      //第一行数据
                g.DrawRectangle(p, startX * Scale, startY * Scale, 70 * Scale, 20 * Scale);//在画板上画矩形,起始坐标为(10,10),宽为80,高为20
                g.DrawRectangle(p, startX * Scale, startY * Scale, 280 * Scale, 20 * Scale);//在画板上画矩形,起始坐标为(90,10),宽为80,高为20
                g.DrawString("厂商", font, brush, (startX + 20) * Scale, (startY + 5) * Scale);//
                g.DrawString("嘉彰科技", font2, brush, (startX + 140) * Scale, (startY + 4) * Scale);
 
 
                //第二行数据
                g.DrawRectangle(p, startX * Scale, (startY + 20) * Scale, 70 * Scale, 20 * Scale);
                g.DrawRectangle(p, startX * Scale, (startY + 20) * Scale, 280 * Scale, 20 * Scale);
                g.DrawString("品名", font, brush, (startX + 20) * Scale, (startY + 25) * Scale);
                g.DrawString(@"COVER LG208H-RC100 #2", font4, brush, (startX + 71) * Scale, (startY + 22) * Scale);
 
                //第三行数据
                g.DrawRectangle(p, startX * Scale, (startY + 40) * Scale, 70 * Scale, 20 * Scale);
                g.DrawRectangle(p, startX * Scale, (startY + 40) * Scale, 280 * Scale, 20 * Scale);
                g.DrawString("料号", font, brush, (startX + 20) * Scale, (startY + 45) * Scale);
                g.DrawString(@"436LG208I000", font2, brush, (startX + 130) * Scale, (startY + 43) * Scale);
 
                //第四行数据
                g.DrawRectangle(p, startX * Scale, (startY + 60) * Scale, 70 * Scale, 20 * Scale);
                g.DrawRectangle(p, startX * Scale, (startY + 60) * Scale, 210 * Scale, 20 * Scale);
                g.DrawString("数量", font, brush, (startX + 20) * Scale, (startY + 65) * Scale);
                g.DrawString(@"50PCS", font2, brush, (startX + 120) * Scale, (startY + 62) * Scale);
 
                //第五行数据
                g.DrawRectangle(p, startX * Scale, (startY + 80) * Scale, 70 * Scale, 20 * Scale);
                g.DrawRectangle(p, startX * Scale, (startY + 80) * Scale, 210 * Scale, 20 * Scale);
                g.DrawString("生产日期", font, brush, (startX + 10) * Scale, (startY + 85) * Scale);
                g.DrawString($"{DateTime.Now.ToString("yyyy.MM.dd")}", font2, brush, (startX + 105) * Scale, (startY + 82) * Scale);
 
                //第六行数据
                g.DrawRectangle(p, startX * Scale, (startY + 100) * Scale, 70 * Scale, 20 * Scale);
                g.DrawRectangle(p, startX * Scale, (startY + 100) * Scale, 140 * Scale, 20 * Scale);
                g.DrawRectangle(p, startX * Scale, (startY + 100) * Scale, 210 * Scale, 20 * Scale);
                g.DrawString("MARK1", font, brush, (startX + 15) * Scale, (startY + 104) * Scale);
                g.DrawString("MARK2", font, brush, (startX + 155) * Scale, (startY + 104) * Scale);
 
 
                //侧面
                g.DrawRectangle(p, (startX + 210) * Scale, (startY + 60) * Scale, 70 * Scale, 60 * Scale);
                g.DrawString(@"#2", font3, brush, (startX + 210) * Scale, (startY + 62) * Scale);
 
 
 
 
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
 
        public static Bitmap CreateCode(string asset)
        {
            // 1.设置条形码规格
            EncodingOptions options = new EncodingOptions();
            options.Height = 100; // 必须制定高度、宽度
            options.Width = 250;
 
            // 2.生成条形码图片并保存
            var writer = new BarcodeWriter();
 
            writer.Options = options;
            writer.Format = BarcodeFormat.CODE_128;  // 这里可以设定条码的标准
 
            //為true不自帶條碼信息
            options.PureBarcode = true;
            Bitmap bp = writer.Write(asset);
 
            return bp;     // 生成图片
        }
 
        //打印
        private void button1_Click(object sender, EventArgs e)
        {
            message = textBox1.Text;
            if (string.IsNullOrEmpty(message))
            {
                MessageBox.Show("打印内容不可为空");
                return;
            }
            StartPrint(message);
        }
 
 
        //预览
        private void button2_Click(object sender, EventArgs e)
        {
            message = textBox1.Text;
            if (string.IsNullOrEmpty(message))
            {
                MessageBox.Show("打印内容不可为空");
                return;
            }
            PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
            this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = "Honeywell PX240S (300 dpi)";
            this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", ttwith, ttheigh);
            //this.printDocument1.PrintPage += new PrintPageEventHandler(this.printDocument_Print);
            printPreviewDialog1.Document = printDocument1;
            DialogResult res = printPreviewDialog1.ShowDialog();
            printPreviewDialog1.Document.Dispose();
        }
 
 
 
        public Bitmap getexcel()
        {
 
 
            int startX = 10;
            int startY = 25;
 
            int Scale = 1;
 
            int length = 320 * Scale;
            int height = 160 * Scale;
 
            Bitmap bmp = new Bitmap(length, height);//, System.Drawing.Imaging.PixelFormat.Format32bppArgb);//新建一个图片对象
 
            //bmp.SetResolution(320, 160);
 
            Graphics g = Graphics.FromImage(bmp);//利用该图片对象生成“画板”
 
            Font font = new Font("Arial", 8 * Scale);//设置字体颜色
            Font font2 = new Font("Arial", 11 * Scale);//设置字体颜色
            Font font3 = new Font("Arial", 36 * Scale);//设置字体颜色
            Font font4 = new Font("Arial", 12 * Scale);//设置字体颜色
 
 
            SolidBrush brush = new SolidBrush(Color.Black);//新建一个画刷,到这里为止,我们已经准备好了画板、画刷、和数据
 
            //g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
 
 
            //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //g.PixelOffsetMode = PixelOffsetMode.Half;
 
 
            Pen p = new Pen(Color.Black, Scale);//定义了一个黑色,宽度为1的画笔
            g.Clear(Color.White); //设置白色背景
            //第一行数据
            g.DrawRectangle(p, startX * Scale, startY * Scale, 70 * Scale, 20 * Scale);//在画板上画矩形,起始坐标为(10,10),宽为80,高为20
            g.DrawRectangle(p, startX * Scale, startY * Scale, 280 * Scale, 20 * Scale);//在画板上画矩形,起始坐标为(90,10),宽为80,高为20
            g.DrawString("厂商", font, brush, (startX + 20) * Scale, (startY + 5) * Scale);//
            g.DrawString("嘉彰科技", font2, brush, (startX + 140) * Scale, (startY + 4) * Scale);
 
 
            //第二行数据
            g.DrawRectangle(p, startX * Scale, (startY + 20) * Scale, 70 * Scale, 20 * Scale);
            g.DrawRectangle(p, startX * Scale, (startY + 20) * Scale, 280 * Scale, 20 * Scale);
            g.DrawString("品名", font, brush, (startX + 20) * Scale, (startY + 25) * Scale);
            g.DrawString(@"COVER LG208H-RC100 #2", font4, brush, (startX + 71) * Scale, (startY + 22) * Scale);
 
            //第三行数据
            g.DrawRectangle(p, startX * Scale, (startY + 40) * Scale, 70 * Scale, 20 * Scale);
            g.DrawRectangle(p, startX * Scale, (startY + 40) * Scale, 280 * Scale, 20 * Scale);
            g.DrawString("料号", font, brush, (startX + 20) * Scale, (startY + 45) * Scale);
            g.DrawString(@"436LG208I000", font2, brush, (startX + 130) * Scale, (startY + 43) * Scale);
 
            //第四行数据
            g.DrawRectangle(p, startX * Scale, (startY + 60) * Scale, 70 * Scale, 20 * Scale);
            g.DrawRectangle(p, startX * Scale, (startY + 60) * Scale, 210 * Scale, 20 * Scale);
            g.DrawString("数量", font, brush, (startX + 20) * Scale, (startY + 65) * Scale);
            g.DrawString(@"50PCS", font2, brush, (startX + 120) * Scale, (startY + 62) * Scale);
 
            //第五行数据
            g.DrawRectangle(p, startX * Scale, (startY + 80) * Scale, 70 * Scale, 20 * Scale);
            g.DrawRectangle(p, startX * Scale, (startY + 80) * Scale, 210 * Scale, 20 * Scale);
            g.DrawString("生产日期", font, brush, (startX + 10) * Scale, (startY + 85) * Scale);
            g.DrawString($"{DateTime.Now.ToString("yyyy.MM.dd")}", font2, brush, (startX + 105) * Scale, (startY + 82) * Scale);
 
            //第六行数据
            g.DrawRectangle(p, startX * Scale, (startY + 100) * Scale, 70 * Scale, 20 * Scale);
            g.DrawRectangle(p, startX * Scale, (startY + 100) * Scale, 140 * Scale, 20 * Scale);
            g.DrawRectangle(p, startX * Scale, (startY + 100) * Scale, 210 * Scale, 20 * Scale);
            g.DrawString("MARK1", font, brush, (startX + 15) * Scale, (startY + 104) * Scale);
            g.DrawString("MARK2", font, brush, (startX + 155) * Scale, (startY + 104) * Scale);
 
 
            //侧面
            g.DrawRectangle(p, (startX + 210) * Scale, (startY + 60) * Scale, 70 * Scale, 60 * Scale);
            g.DrawString(@"#2", font3, brush, (startX + 210) * Scale, (startY + 62) * Scale);
 
 
 
 
 
            //Font font22 = new Font("Arial", 6);//设置字体颜色
            //for (int i = 0; i < 32;i++) 
            //{
            //    g.DrawString(i.ToString(), font22, brush, 0, i*10);
            //    g.DrawString(i.ToString(), font22, brush, i * 10, 0);
            //}
 
            //bmp.Save("E:/test.bmp");//保存为输出流,否则页面上显示不出来
            g.Dispose();//释放掉该资源
 
            bmp.SetResolution(105 * Scale, 105 * Scale);
 
 
            return bmp;
        }
 
 
 
 
 
 
 
 
 
 
 
 
        #region 
        [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool OpenPrinter(string pPrinterName, out IntPtr hPrinter, IntPtr pDefault);
 
 
        [DllImport("winspool.drv", SetLastError = true)]
        private static extern bool ClosePrinter(IntPtr hPrinter);
 
 
        [DllImport("winspool.drv", SetLastError = true)]
        private static extern bool GetPrinter(IntPtr hPrinter,
            int dwLevel, IntPtr pPrinter, int cbBuf, out int pcbNeeded);
 
 
        [DllImport("winspool.drv", CharSet = CharSet.Auto)]
        public static extern int EnumJobs(IntPtr hPrinter, int FirstJob, int NoJobs, int Level, IntPtr pInfo, int cdBuf,
                                  out int pcbNeeded, out int pcReturned);
 
 
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct PRINTER_INFO_2
        {
            public string pServerName;
            public string pPrinterName;
            public string pShareName;
            public string pPortName;
            public string pDriverName;
            public string pComment;
            public string pLocation;
            public IntPtr pDevMode;
            public string pSepFile;
            public string pPrintProcessor;
            public string pDatatype;
            public string pParameters;
            public IntPtr pSecurityDescriptor;
            public uint Attributes;
            public uint Priority;
            public uint DefaultPriority;
            public uint StartTime;
            public uint UntilTime;
            public uint Status;
            public uint cJobs;
            public uint AveragePPM;
        }
 
        /// <summary>
        /// 默认打印机的名称
        /// </summary>
        //private static string printerName = new LocalPrintServer().DefaultPrintQueue.Name;
        private static string printerName = "Honeywell PX240S (300 dpi)";
 
        /// <summary>
        /// 获取打印机的状态编码(方式1)
        /// </summary>
        /// <returns></returns>
        public static int GetPrinterStatusCodeInt()
        {
            int intRet = -1;
            IntPtr hPrinter;
 
 
            if (OpenPrinter(printerName, out hPrinter, IntPtr.Zero))
            {
                int cbNeeded = 0;
                bool bolRet = GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out cbNeeded);
                if (cbNeeded > 0)
                {
                    IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
                    bolRet = GetPrinter(hPrinter, 2, pAddr, cbNeeded, out cbNeeded);
                    if (bolRet)
                    {
                        PRINTER_INFO_2 Info2 = new PRINTER_INFO_2();
 
 
                        Info2 = (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2));
 
 
                        intRet = System.Convert.ToInt32(Info2.Status);
                    }
                    Marshal.FreeHGlobal(pAddr);
                }
                ClosePrinter(hPrinter);
            }
 
 
            return intRet;
        }
        /// <summary>
        /// 检查打印机是否可用(方式1)
        /// </summary>
        /// <param name="intStatusCodeValue"></param>
        /// <returns></returns>
        public static bool CheckIsEnable(int intStatusCodeValue)
        {
            bool isEnable = false;
            if (UIPrinter.GetTaskNumber() == 0)
            {
                return true;
            }
            if (intStatusCodeValue == 0x0008000 || intStatusCodeValue == 0x00000400 || intStatusCodeValue == 0x00004000)
            {
                return true;
            }
            return isEnable;
        }
 
 
        /// <summary>
        /// 获取打印机的状态信息(方式1)
        /// </summary>
        /// <returns></returns>
        public static string GetPrinterStatusMessage(int intStatusCodeValue)
        {
            string strRet = string.Empty;
            switch (intStatusCodeValue)
            {
                case 0:
                    strRet = "准备就绪(Ready)";
                    break;
                case 0x00000200:
                    strRet = "忙(Busy)";
                    break;
                case 0x00400000:
                    strRet = "被打开(Printer Door Open)";
                    break;
                case 0x00000002:
                    strRet = "错误(Printer Error)";
                    break;
                case 0x0008000:
                    strRet = "初始化(Initializing)";
                    break;
                case 0x00000100:
                    strRet = "正在输入,输出(I/O Active)";
                    break;
                case 0x00000020:
                    strRet = "手工送纸(Manual Feed)";
                    break;
                case 0x00040000:
                    strRet = "无墨粉(No Toner)";
                    break;
                case 0x00001000:
                    strRet = "不可用(Not Available)";
                    break;
                case 0x00000080:
                    strRet = "脱机(Off Line)";
                    break;
                case 0x00200000:
                    strRet = "内存溢出(Out of Memory)";
                    break;
                case 0x00000800:
                    strRet = "输出口已满(Output Bin Full)";
                    break;
                case 0x00080000:
                    strRet = "当前页无法打印(Page Punt)";
                    break;
                case 0x00000008:
                    strRet = "塞纸(Paper Jam)";
                    break;
                case 0x00000010:
                    strRet = "打印纸用完(Paper Out)";
                    break;
                case 0x00000040:
                    strRet = "纸张问题(Page Problem)";
                    break;
                case 0x00000001:
                    strRet = "暂停(Paused)";
                    break;
                case 0x00000004:
                    strRet = "正在删除(Pending Deletion)";
                    break;
                case 0x00000400:
                    strRet = "正在打印(Printing)";
                    break;
                case 0x00004000:
                    strRet = "正在处理(Processing)";
                    break;
                case 0x00020000:
                    strRet = "墨粉不足(Toner Low)";
                    break;
                case 0x00100000:
                    strRet = "需要用户干预(User Intervention)";
                    break;
                case 0x20000000:
                    strRet = "等待(Waiting)";
                    break;
                case 0x00010000:
                    strRet = "热机中(Warming Up)";
                    break;
                case 1048706:
                    strRet = "色带已用尽";
                    break;
                default:
                    strRet = "未知状态(Unknown Status)";
                    break;
            }
            return strRet;
        }
 
        /// <summary>
        /// 获取打印机正在打印的任务数(方式1)
        /// </summary>
        /// <returns></returns>
        public static int GetTaskNumber()
        {
            IntPtr handle;
            int FirstJob = 0;
            int NumJobs = 127;
            int pcbNeeded;
            int pcReturned = -1;
            OpenPrinter(printerName, out handle, IntPtr.Zero);
            // get num bytes required, here we assume the maxt job for the printer quest is 128 (0..127)
            EnumJobs(handle, FirstJob, NumJobs, 1, IntPtr.Zero, 0, out pcbNeeded, out pcReturned);
 
            // allocate unmanaged memory
            IntPtr pData = Marshal.AllocHGlobal(pcbNeeded);
 
            // get structs
            EnumJobs(handle, FirstJob, NumJobs, 1, pData, pcbNeeded, out pcbNeeded, out pcReturned);
 
            return pcReturned;
        }
        #endregion
 
 
        private void button3_Click(object sender, EventArgs e)
        {
            int taskNum = GetTaskNumber();
            int statuscode = GetPrinterStatusCodeInt();
            string status = GetPrinterStatusMessage(statuscode);
            //只有在打印机异常时再次下发打印任务后,才能获取到打印机状态。这是因为打印机驱动程序通常只在处理打印请求时更新其状态信息。如果打印机没有接收到新的打印任务,可能无法立即反映出真实状态。
            textBox2.Text = "taskNum:" + taskNum + ",statuscode:" + statuscode + ",status:" + status;
        }
    }
}