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:
| unit Unit1; interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
type TForm1 = class(TForm) Memo1: TMemo; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormCreate(Sender: TObject); private
public
end;
KontrollThread = class(TThread) private Druckerhandle : THandle; Benachrichtigung : THandle; Ausgabe : TStrings; Procedure Ausgabeaktualisieren; protected procedure Execute; override; end;
var Form1: TForm1;
implementation
Uses WinSpool; {$R *.dfm}
Var MKT : KontrollThread = Nil;
procedure KontrollThread.Execute; Var DJWechseln : DWord; PInfo : PPrinterNotifyInfo; Optionen : TPrinterNotifyOptions; begin Ausgabe := TStringlist.Create; Druckerhandle := INVALID_HANDLE_VALUE; Benachrichtigung := INVALID_HANDLE_VALUE; If OpenPrinter(Nil,Druckerhandle, Nil ) Then Begin Benachrichtigung:=FindFirstPrinterChangeNotification( Druckerhandle, PRINTER_CHANGE_ALL, 0, Nil); End; While Not Terminated Do Begin If Benachrichtigung<>INVALID_HANDLE_VALUE Then Begin Case WaitForSingleObject(Benachrichtigung,200) Of WAIT_OBJECT_0:Begin If FindNextPrinterChangeNotification( Benachrichtigung, DJWechseln, Nil, Pointer(PInfo)) Then Begin If (DJWechseln And PRINTER_CHANGE_ADD_JOB )<>0 Then Ausgabe.Add('Es wurde ein Druckjob gesendet'); If (PInfo<>Nil) And ((PInfo^.Flags And PRINTER_NOTIFY_INFO_DISCARDED)<>0) Then Begin FreePrinterNotifyInfo(PInfo); FillChar(Optionen,SizeOf(Optionen),#0); Optionen.Flags := PRINTER_NOTIFY_OPTIONS_REFRESH; FindNextPrinterChangeNotification(Benachrichtigung, DJWechseln,@Optionen,Pointer(PInfo)); End; FreePrinterNotifyInfo(PInfo); Synchronize(Ausgabeaktualisieren); End; End; WAIT_TIMEOUT :; End; End; End; If Benachrichtigung<>INVALID_HANDLE_VALUE Then Begin FindClosePrinterChangeNotification(Benachrichtigung); Benachrichtigung:=INVALID_HANDLE_VALUE; End; If Druckerhandle<>INVALID_HANDLE_VALUE Then Begin ClosePrinter(Druckerhandle); Druckerhandle:=INVALID_HANDLE_VALUE; End; Ausgabe.Free; end;
procedure KontrollThread.Ausgabeaktualisieren; Begin Form1.Memo1.Lines.AddStrings(Ausgabe); End;
procedure TForm1.FormCreate(Sender: TObject); begin MKT:=KontrollThread.Create(False); end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin If MKT<>Nil Then Begin MKT.Terminate; MKT.WaitFor; End; end;
end. |