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
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Drawing.Printing;
using System.IO;
using System.Runtime.InteropServices;
namespace Project1
{
class Class1
{
'declaration variable
private PrintDocument printDoc = new PrintDocument();
private PageSettings pgSettings = new PageSettings();
private PrinterSettings prSettings = new PrinterSettings();
int t1, t2, p1, p2, c1, c2, g1, g2, g3, g4, g5, g6, g7; 'il s'agit de toutes les cotes
Bitmap tete, pied, corps;
int largeur = 860;
int hauteur = 1160;
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDefaultPrinter(string Name);
public Class1()
{
'ajout de l'event lors de l'appel à la commande .print()
printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
'je controle si le feu est au vert dans le fichier xml
string temp = "";
XmlTextReader reader = new XmlTextReader("C:\\Fichier xml\\mesure.xml");
while (reader.Read())
{
if (reader.Name.Equals("feu"))
{
reader.Read();
temp = reader.Value;
break;
}
}
reader.Close();
'si le feu est vert alors j'imprime
if (temp == "vert")
{
Rectangle cloneRect;
System.Drawing.Imaging.PixelFormat format;
'initialisation variable (a terme à partir du fichier xml)
t1 = 519;
t2 = 936 - t1;
p1 = 519;
p2 = 918 - p1;
c1 = 538;
c2 = 2896 - c1;
g2 = (t2 * hauteur) / 3507;
g3 = 5;
g4 = (c2 * hauteur) / 3507;
g5 = 5;
g6 = (p2 * hauteur) / 3507;
'récupération du nom des images contenus dans monTemp
string[] files;
int[] numImage;
string stemp;
int itemp;
files = Directory.GetFiles("C:\\monTemp");
int filecount = files.GetUpperBound(0) + 1;
numImage = new int[filecount];
for (int i = 0; i < filecount; i++)
{
stemp = System.IO.Path.GetFileNameWithoutExtension(files[i]);
int.TryParse(stemp.Substring(5), out numImage[i]);
}
'je tri le tableau pour avoir l'entete en position "0", le pied de page en position "1" et les autres après
bool continu = true;
filecount--;
while (continu)
{
continu = false;
for (int i = 0; i < filecount; i++)
{
if (numImage[i] > numImage[i + 1])
{
itemp = numImage[i + 1];
numImage[i + 1] = numImage[i];
numImage[i] = itemp;
continu = true;
}
}
}
filecount++;
'je récupére les bitmap entete et pied
tete = new Bitmap("C:\\monTemp\\image" + numImage[0] + ".bmp");
pied = new Bitmap("C:\\monTemp\\image" + numImage[1] + ".bmp");
cloneRect = new Rectangle(0, t1, tete.Width, t2);
format = tete.PixelFormat;
tete = tete.Clone(cloneRect, format);
cloneRect = new Rectangle(0, p1, pied.Width, p2);
format = pied.PixelFormat;
pied = pied.Clone(cloneRect, format);
'boite de dialogue pour choix de l'imprimante
PrintDialog dlg = new PrintDialog();
dlg.ShowDialog();
prSettings = dlg.PrinterSettings;
'sauvegarde de l'imprimante par defaut
string DefaultPrinter = printDoc.PrinterSettings.PrinterName;
'définir la nouvelle imprimante par defaut
SetDefaultPrinter(prSettings.PrinterName);
'je lance toutes les imrpressions
for (int i = 2; i < filecount; i++)
{
corps = new Bitmap("C:\\monTemp\\image" + numImage[i] + ".bmp");
cloneRect = new Rectangle(0, c1, corps.Width, c2);
format = corps.PixelFormat;
corps = corps.Clone(cloneRect, format);
printDoc.Print();
}
'je remet le feu a rouge pour ne pas partir dans une boucle infinie
XmlTextWriter textWriter = new XmlTextWriter("C:\\Fichier xml\\mesure.xml", null);
textWriter.WriteStartDocument();
textWriter.WriteComment("Ce fichier contient l'autorisation d'imprimer (feu vert/rouge)");
textWriter.WriteStartElement("donnees");
textWriter.WriteStartElement("feu");
textWriter.WriteString("rouge");
textWriter.WriteEndElement();
textWriter.WriteEndDocument();
textWriter.Close();
'retablissement de l'imprimante par defaut initiale
SetDefaultPrinter(DefaultPrinter);
}
}
private void printDoc_PrintPage(Object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(tete, 0, 0, largeur, g2);
e.Graphics.DrawImage(corps, 0, g2 + g3, largeur, g4);
e.Graphics.DrawImage(pied, 0, g2 + g3 + g4 + g5, largeur, g6);
}
public static void Main()
{
new Class1();
}
}
} |
Partager