一个返回纸张尺寸的代码

  1 //filename:class1.cs
  2 #region 纸张类
  3 #region defines
  4 #define WIN32
  5 #endregion
  6 #region usings
  7 using System;
  8 using System.Diagnostics;
  9 using System.Drawing;
 10 using System.IO;
 11 using System.Runtime.ConstrainedExecution;
 12 using System.Security;
 13 #endregion
 14 #region Paperclass
 15 namespace PaperTest
 16 { 
 17     #region 测试特征
 18     [AttributeUsage(AttributeTargets.Class|AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)]
 19     public sealed class MyAttribute : Attribute
 20     {
 21         // See the attribute guidelines at 
 22         //  http://go.microsoft.com/fwlink/?LinkId=85236
 23         internal  readonly string positionalString;
 24         internal readonly int NameInt;
 25         // This is a positional argument
 26         public MyAttribute(string positionalString,int NameInt)
 27         {
 28             this.positionalString = positionalString;
 29             this.NameInt = NameInt;
 30             // TODO: Implement code here
 31             
 32         }
 33 
 34         public string PositionalString
 35         {
 36             get { return positionalString; }
 37         }
 38 
 39         // This is a named argument
 40         public int NamedInt {
 41             get { return this.NameInt; }
 42         }
 43     }
 44 #endregion
 45     #region TestAttribute
 46     [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
 47 public sealed class BaseAttribute :Attribute
 48 {
 49     public BaseAttribute()
 50     {
 51        
 52     }
 53     public override object TypeId
 54     {
 55         get
 56         {
 57             return base.TypeId;
 58         }
 59     }
 60     public override bool Equals(object obj)
 61     {
 62         return base.Equals(obj);
 63     }
 64     public override int GetHashCode()
 65     {
 66         return base.GetHashCode();
 67     }
 68     public override bool IsDefaultAttribute()
 69     {
 70         return base.IsDefaultAttribute();
 71     }
 72     public override bool Match(object obj)
 73     {
 74         return base.Match(obj);
 75     }
 76     public override string ToString()
 77     {
 78         return base.ToString();
 79     }
 80 }
 81     #endregion
 82     #region 测试c#新关键字
 83     public class TestNameof : ClassLibrary1.Itestnameof
 84     {
 85         private Type type;
 86         /// <summary>
 87         /// 初始化
 88         /// </summary>
 89         /// <param name="type">typeof获取type</param>
 90         public TestNameof(Type type)
 91         {
 92             this.type= type;
 93         }
 94         /// <summary>
 95         /// 测试c#新关键字
 96         /// </summary>
 97         /// <returns>名称</returns>
 98         public virtual string NameOf()
 99         {
100             return this.type.FullName;
101         }
102 
103         string ClassLibrary1.Itestnameof.NameOf()
104         {
105             return this.NameOf();
106         }
107     }
108     #endregion
109     #region 纸张测试类
110     public class PaperSize : ClassLibrary1.IPaperSize, IDisposable, ClassLibrary1.IPaperSize1
111     {
112         [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
113         [SecurityCritical]
114 
115          public PaperSize()
116         {
117         }       
118         internal readonly  FileStream fs = new FileStream("E:\\tester.txt",FileMode.Create,FileAccess.ReadWrite,FileShare.Delete,2048,false);
119         internal int MAX_BYTE_LENGHT = 100;
120         public  virtual   void PaperSize_Remove(object sender, EventArgs e)
121         {
122             throw new NotSupportedException();
123         }
124 
125         public  virtual  void PaperSize_Add(object sender, EventArgs e)
126         {
127             return;
128         }
129         public static  PaperSize getit { 
130             get { return new PaperSize(); }
131         }
132         #region 系统位数判断实现
133         public static bool getbits
134         {
135             get{
136                 bool is64Bit;
137                 is64Bit = Environment.Is64BitOperatingSystem;
138                 //Console.WriteLine(is64Bit); //true为64位; false为32位
139                 return is64Bit;
140             }
141         }
142         #endregion
143         #region 实现
144         /// <summary>
145         /// 数据对结构
146         /// </summary>
147         public  struct Pointx
148         {
149             /// <summary>
150             /// 长和宽
151             /// </summary>
152             internal  int widght;
153             internal   int lenght;
154             /// <summary>
155             /// 获取长度
156             /// </summary>
157             /// <param name="widght">宽度</param>
158             /// <param name="lenght">长度</param>
159             public Pointx(int widght, int lenght)
160             {
161                 this.lenght = lenght;
162                 this.widght = widght;
163             }
164             public int GetWidght
165             {
166                 get
167                 {
168                     return this.widght;
169                 }
170             }
171             public int GetLenght
172             {
173                 get
174                 {
175                     return this.lenght;
176                 }
177             }
178             public unsafe Pointx* GetPtr {
179                 get {
180                 Pointx p = new Pointx();
181                 Pointx* k = &p;
182                 return k;  
183             } 
184             }
185             public Pointx GetIt{
186                 get { return this; }
187             }
188             public override bool Equals(object obj)
189             {
190              return base.Equals(obj);
191             }
192             public override int GetHashCode()
193            {
194                int i = new Random(1006).Next(10,1000);
195                return i;
196            }
197            public static  bool operator  ==(Pointx left,Pointx right)
198            {
199                return left==right;
200            }
201             public static  bool  operator !=(Pointx left,Pointx right)
202             {
203                 return left!=right;
204             }
205         }
206         /// <summary>
207         /// 纸模式
208         /// </summary>
209         public enum PaperMode
210         {
211             A3 = 0,
212             A4 = 1,
213             A5 = 2,
214             B4 = 3,
215             A0 = 4
216         }
217         /// <summary>
218         /// 单位模式
219         /// </summary>
220         public enum SizeMode
221         {
222             Millimetere = 0,
223             Centimeter = 1,
224             Meter=2
225         }
226         /// <summary>
227         /// 纸张定义
228         /// </summary>
229         internal  const int A3length = 420;
230         internal const int A3width = 297;
231         internal const int A4length = 297;
232         internal const int A4width = 210;
233         internal const int A5length = 210;
234         internal const int A5width = 148;
235         internal const int B4length = 353;
236         internal const int B4width = 250;
237         internal const int A0length = 1189;
238         internal const int A0width = 841;
239         internal readonly Pointx A3 = new Pointx(A3width, A3length);
240         internal readonly Pointx A4 = new Pointx(A4width, A4length);
241         internal readonly Pointx A5 = new Pointx(A5width, A5length);
242         internal readonly Pointx B4 = new Pointx(B4width, B4length);
243         internal readonly Pointx A0 = new Pointx(A0width, A0length);
244         //length 长度
245         //width  宽度
246         /// <summary>
247         /// 获取长度
248         /// </summary>
249         /// <param name="papermode1">纸模式</param>
250         /// <returns></returns>
251         public int getlenght(PaperMode PaperMode1, SizeMode SizeMode1)
252         {
253             switch (SizeMode1)
254             {
255                 case SizeMode.Millimetere:
256                     switch (PaperMode1)
257                     {
258                         case PaperMode.A3:
259                             return A3length;
260                         case PaperMode.A4:
261                             return A4length;
262                         case PaperMode.A5:
263                             return A5length;
264                         case PaperMode.B4:
265                             return B4length;
266                         case PaperMode.A0:
267                             return A0length;
268                         default:
269                             throw new ArgumentException(typeof(PaperSize).FullName);
270                     }
271                 case SizeMode.Centimeter:
272                     switch (PaperMode1)
273                     {
274                         case PaperMode.A3:
275                             return A3length / 10;
276                         case PaperMode.A4:
277                             return A4length / 10;
278                         case PaperMode.A5:
279                             return A5length / 10;
280                         case PaperMode.B4:
281                             return B4length / 10;
282                         case PaperMode.A0:
283                             return A0length / 10;
284                         default:
285                             throw new ArgumentException(typeof(PaperSize).FullName);
286                     }
287                 case SizeMode.Meter:
288                     switch (PaperMode1)
289                     {
290                         case PaperMode.A3:
291                             return A3length / 1000;
292                         case PaperMode.A4:
293                             return A4length / 1000;
294                         case PaperMode.A5:
295                             return A5length / 1000;
296                         case PaperMode.B4:
297                             return B4length / 1000;
298                         case PaperMode.A0:
299                             return A0length / 1000;
300                         default:
301                             throw new ArgumentException(typeof(PaperSize).FullName);
302                     }
303                 default:
304                     throw new ArgumentException(typeof(PaperSize).FullName);
305             }
306         }
307         /// <summary>
308         /// 获取宽度
309         /// </summary>
310         /// <param name="papermode1">纸模式</param>
311         /// <returns></returns>
312         public virtual int getwidth(PaperMode papermode1, SizeMode SizeMode1)
313         {
314             switch (SizeMode1)
315             {
316                 case SizeMode.Millimetere:
317                     switch (papermode1)
318                     {
319                         case PaperMode.A3:
320                             return A3width;
321                         case PaperMode.A4:
322                             return A4width;
323                         case PaperMode.A5:
324                             return A5length;
325                         case PaperMode.B4:
326                             return B4width;
327                         case PaperMode.A0:
328                             return A0width;
329                         default:
330                             throw new ArgumentException(typeof(PaperSize).FullName);
331                     }
332                 case SizeMode.Centimeter:
333                     switch (papermode1)
334                     {
335                         case PaperMode.A3:
336                             return A3width / 10;
337                         case PaperMode.A4:
338                             return A4width / 10;
339                         case PaperMode.A5:
340                             return A5length / 10;
341                         case PaperMode.B4:
342                             return B4width / 10;
343                         case PaperMode.A0:
344                             return A0width / 10;
345                         default:
346                             throw new ArgumentException(typeof(PaperSize).FullName);
347                     }
348                 case SizeMode.Meter:
349                     switch (papermode1)
350                     {
351                         case PaperMode.A3:
352                             return A3width / 1000;
353                         case PaperMode.A4:
354                             return A4width / 1000;
355                         case PaperMode.A5:
356                             return A5length / 1000;
357                         case PaperMode.B4:
358                             return B4width / 1000;
359                         case PaperMode.A0:
360                             return A0width / 1000;
361                         default:
362                             throw new ArgumentException(typeof(PaperSize).FullName);
363                     }
364                 default:
365                     throw new ArgumentException(typeof(PaperSize).FullName);
366             }
367         }
368         /// <summary>
369         /// 获取point
370         /// </summary>
371         /// <param name="papermode1">纸模式</param>
372         /// <returns></returns>
373         public virtual Pointx getpointx(PaperMode papermode1)
374         {
375             switch (papermode1)
376             {
377                 case PaperMode.A3:
378                     return A3;
379                 case PaperMode.A4:
380                     return A4;
381                 case PaperMode.A5:
382                     return A5;
383                 case PaperMode.B4:
384                     return B4;
385                 case PaperMode.A0:
386                     return A0;
387                 default:
388                     throw new ArgumentException(typeof(PaperSize).FullName);
389             }
390         }
391         /// <summary>
392         /// 用指定绘画对象画出纸张样图
393         /// </summary>
394         /// <param name="g">绘画对象</param>
395         /// <param name="p"></param>
396         /// <param name="papermode1">纸模式</param>
397         public virtual void drawtest(Graphics g, Pen p, PaperMode papermode1)
398         {
399             if (g==null && p==null)
400             {
401                 throw new ArgumentException("error");
402             }
403             Rectangle r;
404             switch (papermode1)
405             {
406                 case PaperMode.A3:
407                     r = new Rectangle(10, 10, getwidth(PaperMode.A3, SizeMode.Millimetere), getlenght(PaperMode.A3, SizeMode.Millimetere));
408                     break;
409                 case PaperMode.A4:
410                     r = new Rectangle(10, 10, getwidth(PaperMode.A4, SizeMode.Millimetere), getlenght(PaperMode.A4, SizeMode.Millimetere));
411                     break;
412                 case PaperMode.A5:
413                     r = new Rectangle(10, 10, getwidth(PaperMode.A5, SizeMode.Millimetere), getlenght(PaperMode.A5, SizeMode.Millimetere));
414                     break;
415                 case PaperMode.B4:
416                     r = new Rectangle(10, 10, getwidth(PaperMode.B4, SizeMode.Millimetere), getlenght(PaperMode.B4, SizeMode.Millimetere));
417                     break;
418                 case PaperMode.A0:
419                     r = new Rectangle(10, 10, getwidth(PaperMode.A0, SizeMode.Millimetere), getlenght(PaperMode.A0, SizeMode.Millimetere));
420                     break;
421                 default:
422                     r = new Rectangle(0, 0, 0, 0);
423                     break;
424             }
425             if (r != new Rectangle(0, 0, 0, 0))
426             {
427                 g.DrawRectangle(p, r);
428             }
429             else
430             {
431                 g.DrawPie(p, r, 5.5f, 8.5f);
432             }
433         }
434         public virtual unsafe IntPtr getpapersizeptr
435         {
436             get
437             {
438                 void* i = (void*)(0x80097009);
439                 int offest = unchecked((int)0x80097009);
440                 IntPtr n = new IntPtr(i);
441                 IntPtr.Add(n, offest);
442                 return n;
443             }
444             set
445             {
446                 value = new IntPtr((void*)unchecked(0x8009409));
447             }
448         }
449         public virtual int size
450         {
451             get
452             {
453 if(!getbits) {
454                 return 4;
455  }
456 else {
457                 return 8;
458 }
459             }
460         }
461         public  bool writewidghtandlenght(PaperMode pm,SizeMode sm,out int lenght)
462         {
463             string a = "长度:";
464             string d;
465             string b = "宽度: ";
466             switch (sm)
467             {
468                 case SizeMode.Millimetere:
469                 j:
470                     {
471                       d = "毫米";
472                     }
473                     break;
474                 case SizeMode.Centimeter:
475                     d = "厘米";
476                     break;
477                 case SizeMode.Meter:
478                     d = "";
479                     break;
480                 default:
481                     goto j;
482             }
483             string s = a + getlenght(pm, sm).ToString() + d + "\n" + b + getwidth(pm, sm).ToString() + d + "\n";
484             char[] c = s.ToCharArray();
485             byte[] buffer = new byte[MAX_BYTE_LENGHT];
486             buffer=System.Text.Encoding.UTF8.GetBytes(c);
487             fs.Write(buffer, 0, buffer.Length);
488             lenght = buffer.Length;
489             if (fs.ReadByte()==-1)
490            {
491             return true;
492            }
493            return false;
494             }
495         #endregion
496         #region 运算符定义
497         public static bool operator >(PaperSize left, PaperSize right) { return left.getpointx(PaperMode.A4).lenght > right.getpointx(PaperMode.A4).lenght && left.getpointx(PaperMode.A4).widght > right.getpointx(PaperMode.A4).widght; }
498         public static bool operator <(PaperSize left, PaperSize right) { return left.getpointx(PaperMode.A4).lenght < right.getpointx(PaperMode.A4).lenght && left.getpointx(PaperMode.A4).widght < right.getpointx(PaperMode.A4).widght; }
499         public bool Compare(PaperSize left, PaperSize right) { return left.getpointx(PaperMode.A4).lenght < right.getpointx(PaperMode.A4).lenght && left.getpointx(PaperMode.A4).widght < right.getpointx(PaperMode.A4).widght; }
500         public static bool operator ==(PaperSize left, PaperSize right) { return left == right; }
501         public static bool operator !=(PaperSize left, PaperSize right) { return left != right; }
502         public static PaperSize operator +(PaperSize left, PaperSize right) { return left + right; }
503         public PaperSize Plus(PaperSize left, PaperSize right) { return left + right; }
504         public static PaperSize operator -(PaperSize left, PaperSize right) { return left - right; }
505         public PaperSize Negate(PaperSize left, PaperSize right) { return left - right; }
506         public virtual bool Equals(object obj, PaperMode pm)
507         {
508             if (obj is int)
509             {
510                 return getpointx(pm).Equals(obj);
511             }
512             else
513             {
514                 return false;
515             }
516         }
517         public override int GetHashCode()
518         {
519             return base.GetHashCode();
520         }
521         public override string ToString()
522         {
523             return typeof(PaperSize).FullName;
524         }
525         public override bool Equals(object obj)
526         {
527             return base.Equals(obj);
528         }
529         #endregion
530         #region 释放资源
531         ~PaperSize()
532         {
533             Dispose(false);
534         }
535         #endregion
536         #region 实现接口
537         void ClassLibrary1.IPaperSize.drawtest(Graphics g, Pen p, PaperSize.PaperMode papermode1)
538         {
539             this.drawtest(g, p, papermode1);
540         }
541         void ClassLibrary1.IPaperSize1.Dispose(bool b)
542         {
543             this.Dispose(b);
544         }
545         bool ClassLibrary1.IPaperSize.Equals(object obj)
546         {
547             return this.Equals(obj);
548         }
549 
550         bool ClassLibrary1.IPaperSize.Equals(object obj, PaperSize.PaperMode pm)
551         {
552             return this.Equals(obj, pm);
553         }
554 
555         int ClassLibrary1.IPaperSize.GetHashCode()
556         {
557             return this.GetHashCode();
558         }
559 
560         int ClassLibrary1.IPaperSize.getlenght(PaperSize.PaperMode PaperMode1, PaperSize.SizeMode SizeMode1)
561         {
562             return this.getlenght(PaperMode1, SizeMode1);
563         }
564 
565         IntPtr ClassLibrary1.IPaperSize.getpapersize
566         {
567             get { return this.getpapersizeptr; }
568         }
569 
570         PaperSize.Pointx ClassLibrary1.IPaperSize.getpointx(PaperSize.PaperMode papermode1)
571         {
572             return this.getpointx(papermode1);
573         }
574 
575         int ClassLibrary1.IPaperSize.getwidth(PaperSize.PaperMode papermode1, PaperSize.SizeMode SizeMode1)
576         {
577             return getwidth(papermode1, SizeMode1);
578         }
579 
580         int ClassLibrary1.IPaperSize.size
581         {
582             get { return this.size; }
583         }
584         string ClassLibrary1.IPaperSize.ToString()
585         {
586            
587             return this.ToString();
588         }
589         public string d { 
590             get {
591             Debug.Print("test {0}", new int[2] { 1, 3 });
592             return Debug.Listeners.ToString();
593         }
594         }
595         #region 资源清理
596         public void Dispose()
597         {
598             Dispose(true);
599         }
600        protected  virtual void Dispose(bool IsDispose)
601       {
602           if (IsDispose)
603           {
604               GC.SuppressFinalize(this);
605               fs.Close();
606               fs.Dispose();
607           }
608           else
609           {
610               GC.SuppressFinalize(this);
611           }
612       }
613         #endregion     
614         #endregion
615         #region 事件
616         public virtual  event EventHandler Add
617       {
618           add { Console.WriteLine("ADD"); }
619           remove { Console.WriteLine("REMOVE"); }
620       }
621       public  virtual  event  EventHandler Remove
622       {
623           add {  }
624           remove { }
625       }
626         #endregion
627     }
628     }
629     #endregion
630 #endregion
631 #endregion
纸张类
 1   //filename:IPaperSize.cs
 2    using System;
 3    namespace ClassLibrary1
 4    {
 5        interface IPaperSize
 6        {
 7            void drawtest(System.Drawing.Graphics g, System.Drawing.Pen p, PaperTest.PaperSize.PaperMode papermode1);
 8            bool Equals(object obj);
 9           bool Equals(object obj, PaperTest.PaperSize.PaperMode pm);
10           int GetHashCode();
11           int getlenght(PaperTest.PaperSize.PaperMode PaperMode1, PaperTest.PaperSize.SizeMode SizeMode1);
12           IntPtr getpapersize { get; }
13           PaperTest.PaperSize.Pointx getpointx(PaperTest.PaperSize.PaperMode papermode1);
14           int getwidth(PaperTest.PaperSize.PaperMode papermode1, PaperTest.PaperSize.SizeMode SizeMode1);
15           int size { get; }
16           string ToString();
17       }
18   }
 1 //filename:IPaperSize1.cs
 2 using System;
 3 namespace ClassLibrary1
 4 {
 5     interface IPaperSize1
 6     {
 7         event EventHandler Add;
 8         string d { get; }
 9         void Dispose(bool b);
10         void drawtest(System.Drawing.Graphics g, System.Drawing.Pen p, PaperTest.PaperSize.PaperMode papermode1);
11         bool Equals(object obj);
12         bool Equals(object obj, PaperTest.PaperSize.PaperMode pm);
13         int GetHashCode();
14         int getlenght(PaperTest.PaperSize.PaperMode PaperMode1, PaperTest.PaperSize.SizeMode SizeMode1);
15         IntPtr getpapersizeptr { get; set; }
16         PaperTest.PaperSize.Pointx getpointx(PaperTest.PaperSize.PaperMode papermode1);
17         int getwidth(PaperTest.PaperSize.PaperMode papermode1, PaperTest.PaperSize.SizeMode SizeMode1);
18         void PaperSize_Add(object sender, EventArgs e);
19         void PaperSize_Remove(object sender, EventArgs e);
20         event EventHandler Remove;
21         int size { get; }
22         string ToString();
23         bool writewidghtandlenght(PaperTest.PaperSize.PaperMode pm, PaperTest.PaperSize.SizeMode sm, out int lenght);
24     }
25 }
1 //filename:Itestnameof.cs
2 using System;
3 namespace ClassLibrary1
4 {
5     interface Itestnameof
6     {
7         string NameOf();
8     }
9 }

 

posted @ 2016-06-09 13:18  yh20021212  阅读(485)  评论(1)    收藏  举报