- 'Créez une Form, nommez-la PrinterLBP660 (ou autre)
- 'Ajoutez-y un Label nommé lblAffichage
- 'Ajoutez-y un FileSystemWatcher nommé FileSystemWatcher1
- 'Et copiez ce code après avoir modifié :
- ' 1/ le dossier (ici "C:\AImprimer") -> il doit exister (pas de traitement d'erreur)
- ' 2/ le nom de l'imprimante (avec le nom donné dans le Panneau de Configuration)
- Imports System.Drawing.Printing
- Imports System.Environment
-
- Public Class PrinterLBP660
- Dim PrintDoc As New PrintDocument()
- Dim FichiersQueue As New Queue(Of String)
-
- Private Sub PrinterLBP660_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- ChDir("C:\AImprimer") ' <--- Modifier en indiquant ici votre dossier d'impression
- AddHandler PrintDoc.PrintPage, AddressOf PrintDocument_PrintPage
- lblAffichage.Text = "Prêt à imprimer"
- End Sub
-
- Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
- 'S'il y a détection de la création d'un fichier
- If e.ChangeType = IO.WatcherChangeTypes.Created Then
- Dim NomDuFichier As String = e.Name
- 'tout fichier du nom de "shutdown.txt" quel que soit son contenu (même vide) provoque l'arrêt de Windows
- If NomDuFichier = "shutdown.txt" Then
- lblAffichage.Text = "Arrêt de Windows..."
- lblAffichage.BackColor = Color.Red
- lblAffichage.Refresh()
- 'temporisation de 10 secondes pour être sûr de ne pas l'effacer avant sa fermeture en écriture
- Dim t As DateTime = Now.Add(New TimeSpan(0, 0, 10))
- Do
- If Now > t Then Exit Do
- Loop
- 'on l'efface
- Kill("shutdown.txt")
- Dim osInfo As OperatingSystem = OSVersion
- 'selon la version de Windows
- If osInfo.Platform = PlatformID.Win32Windows Then 'W95/98
- 'pour arrêter Windows98
- Shell("rundll32.exe user.exe,exitwindows")
- Else
- 'pour arrêter XP
- Shell("shutdown -s -t 0")
- End If
- ElseIf NomDuFichier.Substring(NomDuFichier.IndexOf(".") + 1) = "jpg" Then
- lblAffichage.Text = "Impression en cours..."
- lblAffichage.BackColor = Color.Red
- lblAffichage.Refresh()
- FichiersQueue.Enqueue(NomDuFichier)
- PrintDoc.PrinterSettings.PrinterName = "Canon LBP-660" ' <-- Modifier en indiquant ici le nom de votre imprimante
- 'tel qu'il apparait dans le Panneau de Configuration / Imprimantes et Télécopieurs
- 'on commande l'impression
- PrintDoc.Print()
- 'temporisation pour laisser le temps au fichier d'être complétement imprimé et fermé
- Do
- Dim t As DateTime = Now.Add(New TimeSpan(0, 0, 1))
- Do
- If Now > t Then Exit Do
- Loop
- 'toutes les secondes on essaye
- Try
- 'effacement du fichier
- Kill(FichiersQueue.Peek)
- Exit Do
- Catch
- End Try
- Loop
- FichiersQueue.Dequeue()
- lblAffichage.Text = "Prêt à imprimer..."
- lblAffichage.BackColor = Color.GreenYellow
- lblAffichage.Refresh()
- End If
- End If
- End Sub
-
- Private Sub PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
- 'temporisation pour laisser le temps au fichier d'être complétement transféré et fermé
- Do
- Dim t As DateTime = Now.Add(New TimeSpan(0, 0, 1))
- Do
- If Now > t Then Exit Do
- Loop
- 'toutes les secondes on essaye
- Try
- FileOpen(1, FichiersQueue.Peek, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.Shared)
- FileClose(1)
- Exit Do
- Catch
- End Try
- Loop
- 'copie du bitmap du fichier-image sur le graphics de l'imprimante
- e.Graphics.DrawImage(New Bitmap(FichiersQueue.Peek), e.Graphics.VisibleClipBounds)
- e.HasMorePages = False
- End Sub
-
- End Class
'Créez une Form, nommez-la PrinterLBP660 (ou autre)
'Ajoutez-y un Label nommé lblAffichage
'Ajoutez-y un FileSystemWatcher nommé FileSystemWatcher1
'Et copiez ce code après avoir modifié :
' 1/ le dossier (ici "C:\AImprimer") -> il doit exister (pas de traitement d'erreur)
' 2/ le nom de l'imprimante (avec le nom donné dans le Panneau de Configuration)
Imports System.Drawing.Printing
Imports System.Environment
Public Class PrinterLBP660
Dim PrintDoc As New PrintDocument()
Dim FichiersQueue As New Queue(Of String)
Private Sub PrinterLBP660_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ChDir("C:\AImprimer") ' <--- Modifier en indiquant ici votre dossier d'impression
AddHandler PrintDoc.PrintPage, AddressOf PrintDocument_PrintPage
lblAffichage.Text = "Prêt à imprimer"
End Sub
Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
'S'il y a détection de la création d'un fichier
If e.ChangeType = IO.WatcherChangeTypes.Created Then
Dim NomDuFichier As String = e.Name
'tout fichier du nom de "shutdown.txt" quel que soit son contenu (même vide) provoque l'arrêt de Windows
If NomDuFichier = "shutdown.txt" Then
lblAffichage.Text = "Arrêt de Windows..."
lblAffichage.BackColor = Color.Red
lblAffichage.Refresh()
'temporisation de 10 secondes pour être sûr de ne pas l'effacer avant sa fermeture en écriture
Dim t As DateTime = Now.Add(New TimeSpan(0, 0, 10))
Do
If Now > t Then Exit Do
Loop
'on l'efface
Kill("shutdown.txt")
Dim osInfo As OperatingSystem = OSVersion
'selon la version de Windows
If osInfo.Platform = PlatformID.Win32Windows Then 'W95/98
'pour arrêter Windows98
Shell("rundll32.exe user.exe,exitwindows")
Else
'pour arrêter XP
Shell("shutdown -s -t 0")
End If
ElseIf NomDuFichier.Substring(NomDuFichier.IndexOf(".") + 1) = "jpg" Then
lblAffichage.Text = "Impression en cours..."
lblAffichage.BackColor = Color.Red
lblAffichage.Refresh()
FichiersQueue.Enqueue(NomDuFichier)
PrintDoc.PrinterSettings.PrinterName = "Canon LBP-660" ' <-- Modifier en indiquant ici le nom de votre imprimante
'tel qu'il apparait dans le Panneau de Configuration / Imprimantes et Télécopieurs
'on commande l'impression
PrintDoc.Print()
'temporisation pour laisser le temps au fichier d'être complétement imprimé et fermé
Do
Dim t As DateTime = Now.Add(New TimeSpan(0, 0, 1))
Do
If Now > t Then Exit Do
Loop
'toutes les secondes on essaye
Try
'effacement du fichier
Kill(FichiersQueue.Peek)
Exit Do
Catch
End Try
Loop
FichiersQueue.Dequeue()
lblAffichage.Text = "Prêt à imprimer..."
lblAffichage.BackColor = Color.GreenYellow
lblAffichage.Refresh()
End If
End If
End Sub
Private Sub PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
'temporisation pour laisser le temps au fichier d'être complétement transféré et fermé
Do
Dim t As DateTime = Now.Add(New TimeSpan(0, 0, 1))
Do
If Now > t Then Exit Do
Loop
'toutes les secondes on essaye
Try
FileOpen(1, FichiersQueue.Peek, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.Shared)
FileClose(1)
Exit Do
Catch
End Try
Loop
'copie du bitmap du fichier-image sur le graphics de l'imprimante
e.Graphics.DrawImage(New Bitmap(FichiersQueue.Peek), e.Graphics.VisibleClipBounds)
e.HasMorePages = False
End Sub
End Class