Advertisement
GlobalAccessSoftware

MyPrintThis.cs

Apr 26th, 2019
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.52 KB | None | 0 0
  1.  
  2.  
  3. #region Top Part
  4. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  5. // PrintThis.cs 13-Apr-18 v3.2.4.47
  6. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  7. // Run This as a Separate Form.
  8. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  9. // Brought to PrintBlaster NameSpace
  10. // v2.3.7.28 21-Jan-2018 (Beginning)
  11. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  12. // v2.5.9.31 09-Feb-2018 Isolated &
  13. // Ready for Further Development!
  14. // v3.2.3.47 06-Apr-018 ^ Beginning
  15. // Split out to Separate Form also.
  16. // v3.2.4.47 09 Near Total Concept!
  17. // ReDesigned App + ReWritten Class.
  18. // v4.3.5.50 Brought Data Mechanism.
  19. // Obsoletes mmf2 and mmf3 stuff.
  20. // (c) John P. Edwards 20-May-2018
  21. // v4.5.6.54 name & optional no-path
  22. // v4.8.0.76 09/30 MCT.SaveList mods.
  23. // v4.9.5.95 23-Dec-2018 Coding it.
  24. // v4.9.5.96 31-Dec Rev: simplifies greatly.
  25. // v4.9.5.98 12-Jan-2019 Moved to Project.
  26. // v5.0.5.98 12-Jan-2019 Integrate to Beta.
  27. // v5.0.5.99 15-Jan-2019 MCPL Design change.
  28. // v5.0.6.00 21-Feb MCPL Integrate & Test.
  29. // v5.0.6.01 22-Feb-2019 Dialing it all in Now!
  30. // v5.0.6.01 02-Mar-2019 Fixedwell w/MPP Menu
  31. // items calling back across, etc.
  32. // v5.0.6.02 08-Mar-2019 Batch Stuff & Debug
  33. // v5.0.6.03 14-Mar-2019 ReSetPages Call.
  34. // v5.1.6.05 01-Apr-2019 Solidification.
  35. // v5.1.6.06 10-Apr-2019 Highly Polished.
  36. // v5.1.6.07 17-Apr Evolved to Perfection!
  37. // v5.1.6.08 22-Apr Final Tested Well.
  38. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  39.  
  40. using System;
  41. using System.Collections.Generic;
  42. using System.Drawing.Printing;
  43. using System.IO;
  44. using System.Threading;
  45. using System.Windows.Forms;
  46.  
  47. using MCC = MyCustomControls.CustomControl;
  48. using MCT = MyCustomLibrary.MyToolbox;
  49. using MPC = MyCustomPrintLibrary.MyPrintClass;
  50. using MPE = MyCustomPrintLibrary.MyPrintEvent;
  51.  
  52. using MPR = MyCustomPrintLibrary
  53.   .Properties.Resources;
  54.  
  55.  
  56. namespace MyCustomPrintLibrary
  57. {
  58.   public static class MyPrintThis
  59.   {
  60.     private static bool Batch
  61.     { get; set; }
  62.  
  63.     private static readonly Mutex
  64.       Mutex1 = new Mutex(
  65.         false, "PrintThisMutex1");
  66.  
  67.     private static readonly Mutex
  68.       Mutex2 = new Mutex(
  69.         false, "PrintThisMutex2");
  70.  
  71.     public static void
  72.       PrintThis(object vars)
  73.     {
  74.       var args = vars as string[];
  75.       if (args == null
  76.         || args.Length < 3) return;
  77.       Mutex1.WaitOne();
  78.       Batch = false;
  79.       if (args.Length < 4)
  80.       {
  81.         var f = args[2];
  82.         if (MPC.IsFile(f)) PrintFile(f);
  83.         else PrintSelection(f);
  84.       }
  85.       else PrintBatch(args);
  86.       Mutex1.ReleaseMutex();
  87.     }
  88. #endregion Top Part
  89.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  90.  
  91.  
  92. #region Active Form
  93.    
  94.     /* * General Parms Doc: * *
  95.      * --------------------------------------
  96.      * Args[0] = App FullPath;
  97.      * Args[1] = Sender/Who;
  98.      * Args[2] = Selection or FileName;
  99.      * Args[n] = Additional Files if a Batch;
  100.      * --------------------------------------
  101.      */
  102.  
  103.     private static void
  104.       PrintBatch(IList<string> args)
  105.     {
  106.       if (args == null
  107.         || args.Count < 3) return;
  108.       Batch = true;
  109.       for (var i = 2; i < args.Count; ++i)
  110.         PrintFile(args[i]);
  111.     }
  112.  
  113.     private static void
  114.       PrintFile(string file)
  115.     {
  116.       Mutex2.WaitOne();
  117.       if (string.IsNullOrEmpty(file)
  118.         || !File.Exists(file)) return;
  119.       PrintSelection(file);
  120.       Mutex2.ReleaseMutex();
  121.     }
  122.  
  123.     private static void
  124.       PrintSelection(string file)
  125.     {
  126.       using (var d = GetPrintDialog(file))
  127.       {
  128.         if (!d.ShowDialog().Equals( // <-==
  129.           DialogResult.OK)) return;
  130.         MPC.PrintSet = d.PrinterSettings;
  131.         Thread.Sleep(420);
  132.         if (MPC.QuitCode.Equals(00))
  133.           d.Document.Print();
  134.       }
  135.     }
  136. #endregion Active Form
  137.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  138.    
  139.    
  140. #region Get Print Dialog
  141.  
  142.     /* MyCustomControls Legend:
  143.      ****************************
  144.      * O1 = MyOpenDialog;
  145.      * R1 = MyRichTextBox;
  146.      * P1 = MyPrintDialog;
  147.      * P2 = MyPreviewControl;
  148.      * P3 = MyPreviewDialog;
  149.      * D1 = MyPrintDoc;
  150.      * P4 = MyPageSetup;
  151.      * M1 = MyPreviewMenuStrip;
  152.      * M2 = MyColorDialog;
  153.      * F0 = MyFontDialog;
  154.      * S1 = MySaveDialog;
  155.      ****************************
  156.     */
  157.    
  158.     /* --------------------------*/
  159.     // PreSet Defaults Are:
  160.     // --------------------------
  161.     // d.AllowPrintToFile =
  162.     // d.AllowCurrentPage =
  163.     // d.UseEXDialog      = false;
  164.     // --------------------------
  165.     // d.AllowSelection   =
  166.     //   d.AllowSomePages =
  167.     //   d.ShowNetwork    =
  168.     //   d.ShowHelp       = true;
  169.     // --------------------------
  170.     // Some changes occur in code
  171.     // below: See MyCustomControls
  172.     // .Designer, Source, & ResX.
  173.     /* --------------------------*/
  174.     private static PrintDialog
  175.       GetPrintDialog(string file)
  176.     {
  177.       PrintDialog d;
  178.       using (var cc = new MCC())
  179.       { d = cc.P1; }
  180.       // ^ w/PreSet default Properties
  181.       if (MPC.MyPrintDoc!= null)
  182.         d.Document = MPC.MyPrintDoc;
  183.       if (Batch) SetSettings(ref d, false);
  184.       if (string.IsNullOrEmpty(file)) throw
  185.         new ArgumentException(MPR.Err1);
  186.       if (MPC.IsFile(file))
  187.         FileSettings(file, ref d);
  188.       else SelectionSettings(file, ref d);
  189.       d.HelpRequest +=
  190.         MCT.PrintHelpEventHandler;
  191.       return d;
  192.     }
  193.  
  194.     private static void FileSettings(
  195.       string file, ref PrintDialog d)
  196.     {
  197.       if (d.Document.PrinterSettings
  198.         .PrintRange.Equals(
  199.           PrintRange.Selection))
  200.         SetSettings(ref d, true);
  201.       else d.AllowSelection = false;
  202.       d.Document.DocumentName =
  203.         Path.GetFileName(
  204.       d.Document.PrinterSettings
  205.         .PrintFileName = file);
  206.       MPC.GetContent(file);
  207.     }
  208.  
  209.     private static void SelectionSettings(
  210.       string file, ref PrintDialog d)
  211.     {
  212.       d.AllowCurrentPage =
  213.         d.AllowSomePages = false;
  214.       d.AllowSelection   = true;
  215.       d.Document.PrinterSettings.PrintRange
  216.         = d.PrinterSettings.PrintRange =
  217.           PrintRange.Selection;
  218.       MPE.DocContents = file;
  219.     }
  220.  
  221.     private static void SetSettings(
  222.       ref PrintDialog d, bool b)
  223.     {
  224.       d.Document
  225.        .PrinterSettings.PrintRange =
  226.       d.PrinterSettings.PrintRange =
  227.         PrintRange.AllPages;
  228.       MPC.PrintSet = d.PrinterSettings;
  229.       d.AllowSelection = false;
  230.       d.AllowSomePages =
  231.       d.AllowCurrentPage = b;
  232.     }
  233. #endregion Get Print Dialog
  234.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  235.  
  236.  
  237.   }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement