#region Top Part
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// PrintThis.cs 13-Apr-18 v3.2.4.47
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// Run This as a Separate Form.
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// Brought to PrintBlaster NameSpace
// v2.3.7.28 21-Jan-2018 (Beginning)
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// v2.5.9.31 09-Feb-2018 Isolated &
// Ready for Further Development!
// v3.2.3.47 06-Apr-018 ^ Beginning
// Split out to Separate Form also.
// v3.2.4.47 09 Near Total Concept!
// ReDesigned App + ReWritten Class.
// v4.3.5.50 Brought Data Mechanism.
// Obsoletes mmf2 and mmf3 stuff.
// (c) John P. Edwards 20-May-2018
// v4.5.6.54 name & optional no-path
// v4.8.0.76 09/30 MCT.SaveList mods.
// v4.9.5.95 23-Dec-2018 Coding it.
// v4.9.5.96 31-Dec Rev: simplifies greatly.
// v4.9.5.98 12-Jan-2019 Moved to Project.
// v5.0.5.98 12-Jan-2019 Integrate to Beta.
// v5.0.5.99 15-Jan-2019 MCPL Design change.
// v5.0.6.00 21-Feb MCPL Integrate & Test.
// v5.0.6.01 22-Feb-2019 Dialing it all in Now!
// v5.0.6.01 02-Mar-2019 Fixedwell w/MPP Menu
// items calling back across, etc.
// v5.0.6.02 08-Mar-2019 Batch Stuff & Debug
// v5.0.6.03 14-Mar-2019 ReSetPages Call.
// v5.1.6.05 01-Apr-2019 Solidification.
// v5.1.6.06 10-Apr-2019 Highly Polished.
// v5.1.6.07 17-Apr Evolved to Perfection!
// v5.1.6.08 22-Apr Final Tested Well.
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using MCC = MyCustomControls.CustomControl;
using MCT = MyCustomLibrary.MyToolbox;
using MPC = MyCustomPrintLibrary.MyPrintClass;
using MPE = MyCustomPrintLibrary.MyPrintEvent;
using MPR = MyCustomPrintLibrary
.Properties.Resources;
namespace MyCustomPrintLibrary
{
public static class MyPrintThis
{
private static bool Batch
{ get; set; }
private static readonly Mutex
Mutex1 = new Mutex(
false, "PrintThisMutex1");
private static readonly Mutex
Mutex2 = new Mutex(
false, "PrintThisMutex2");
public static void
PrintThis(object vars)
{
var args = vars as string[];
if (args == null
|| args.Length < 3) return;
Mutex1.WaitOne();
Batch = false;
if (args.Length < 4)
{
var f = args[2];
if (MPC.IsFile(f)) PrintFile(f);
else PrintSelection(f);
}
else PrintBatch(args);
Mutex1.ReleaseMutex();
}
#endregion Top Part
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
#region Active Form
/* * General Parms Doc: * *
* --------------------------------------
* Args[0] = App FullPath;
* Args[1] = Sender/Who;
* Args[2] = Selection or FileName;
* Args[n] = Additional Files if a Batch;
* --------------------------------------
*/
private static void
PrintBatch(IList<string> args)
{
if (args == null
|| args.Count < 3) return;
Batch = true;
for (var i = 2; i < args.Count; ++i)
PrintFile(args[i]);
}
private static void
PrintFile(string file)
{
Mutex2.WaitOne();
if (string.IsNullOrEmpty(file)
|| !File.Exists(file)) return;
PrintSelection(file);
Mutex2.ReleaseMutex();
}
private static void
PrintSelection(string file)
{
using (var d = GetPrintDialog(file))
{
if (!d.ShowDialog().Equals( // <-==
DialogResult.OK)) return;
MPC.PrintSet = d.PrinterSettings;
Thread.Sleep(420);
if (MPC.QuitCode.Equals(00))
d.Document.Print();
}
}
#endregion Active Form
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
#region Get Print Dialog
/* MyCustomControls Legend:
****************************
* O1 = MyOpenDialog;
* R1 = MyRichTextBox;
* P1 = MyPrintDialog;
* P2 = MyPreviewControl;
* P3 = MyPreviewDialog;
* D1 = MyPrintDoc;
* P4 = MyPageSetup;
* M1 = MyPreviewMenuStrip;
* M2 = MyColorDialog;
* F0 = MyFontDialog;
* S1 = MySaveDialog;
****************************
*/
/* --------------------------*/
// PreSet Defaults Are:
// --------------------------
// d.AllowPrintToFile =
// d.AllowCurrentPage =
// d.UseEXDialog = false;
// --------------------------
// d.AllowSelection =
// d.AllowSomePages =
// d.ShowNetwork =
// d.ShowHelp = true;
// --------------------------
// Some changes occur in code
// below: See MyCustomControls
// .Designer, Source, & ResX.
/* --------------------------*/
private static PrintDialog
GetPrintDialog(string file)
{
PrintDialog d;
using (var cc = new MCC())
{ d = cc.P1; }
// ^ w/PreSet default Properties
if (MPC.MyPrintDoc!= null)
d.Document = MPC.MyPrintDoc;
if (Batch) SetSettings(ref d, false);
if (string.IsNullOrEmpty(file)) throw
new ArgumentException(MPR.Err1);
if (MPC.IsFile(file))
FileSettings(file, ref d);
else SelectionSettings(file, ref d);
d.HelpRequest +=
MCT.PrintHelpEventHandler;
return d;
}
private static void FileSettings(
string file, ref PrintDialog d)
{
if (d.Document.PrinterSettings
.PrintRange.Equals(
PrintRange.Selection))
SetSettings(ref d, true);
else d.AllowSelection = false;
d.Document.DocumentName =
Path.GetFileName(
d.Document.PrinterSettings
.PrintFileName = file);
MPC.GetContent(file);
}
private static void SelectionSettings(
string file, ref PrintDialog d)
{
d.AllowCurrentPage =
d.AllowSomePages = false;
d.AllowSelection = true;
d.Document.PrinterSettings.PrintRange
= d.PrinterSettings.PrintRange =
PrintRange.Selection;
MPE.DocContents = file;
}
private static void SetSettings(
ref PrintDialog d, bool b)
{
d.Document
.PrinterSettings.PrintRange =
d.PrinterSettings.PrintRange =
PrintRange.AllPages;
MPC.PrintSet = d.PrinterSettings;
d.AllowSelection = false;
d.AllowSomePages =
d.AllowCurrentPage = b;
}
#endregion Get Print Dialog
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
}
}
Comments
0 B
|👍
/👎