www.pudn.com > PrintServer.rar > Main.pas, change:2006-03-03,size:10299b


unit Main; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, DB, MemDS, DBAccess, MSAccess, Grids, DBGridEh, ExtCtrls, 
  ComCtrls, TFlatButtonUnit, WinSpool, Printers, StdCtrls, WinSkinData; 
 
type 
  TMainForm = class(TForm) 
    StatusBar1: TStatusBar; 
    Panel1: TPanel; 
    Panel2: TPanel; 
    DBGridEh1: TDBGridEh; 
    MC: TMSConnection; 
    InfoQuery: TMSQuery; 
    DataSource1: TDataSource; 
    FlatButton1: TFlatButton; 
    FlatButton2: TFlatButton; 
    FlatButton3: TFlatButton; 
    FlatButton4: TFlatButton; 
    GroupBox1: TGroupBox; 
    Label1: TLabel; 
    Label2: TLabel; 
    Timer1: TTimer; 
    WorkQuery: TMSQuery; 
    SkinData1: TSkinData; 
    ListBox1: TListBox; 
    Label3: TLabel; 
    Button1: TButton; 
    procedure FlatButton4Click(Sender: TObject); 
    procedure Timer1Timer(Sender: TObject); 
    procedure SaveData; 
    procedure FlatButton2Click(Sender: TObject); 
    procedure FlatButton1Click(Sender: TObject); 
    procedure FlatButton3Click(Sender: TObject); 
    procedure FormCreate(Sender: TObject); 
    procedure Button1Click(Sender: TObject); 
  private 
    CurrentMachineName, CurrentUser, CurrentDocName, CurrentPrinter, CurrentPages: string; 
    CurrentMachineAdd, CurrentName, CurrentSize, CurrentCopy, CurrentPageSort: string; 
    procedure PrintDetect; 
  public 
    { Public declarations } 
    procedure SpollerStatus(var Msg: TWMSpoolerStatus); message WM_SPOOLERSTATUS; 
  end; 
 
var 
  MainForm: TMainForm; 
 
implementation 
 
uses EnterEmployee, EnterDeptInfo, ReportOfPrintRec; 
 
{$R *.dfm} 
 
procedure TMainForm.SpollerStatus(var Msg: TWMSpoolerStatus); 
begin 
  PrintDetect; 
end; 
 
procedure TMainForm.PrintDetect; 
var 
  tbt: Byte; 
  PrinterNeeded, PrinterReturned, JobNeeded, JobReturned: DWORD; 
  PrinterResultBuffer, JobResultBuffer: Pointer; 
  PrinterCount, JobCount: Integer; 
  PrinterInfo: PPrinterInfo1; //打印机队列 
  JobInfo: PJobInfo2; //打印任务队列 
  hPrinter: THandle; 
begin 
  PrinterNeeded := 0; 
  PrinterReturned := 0; 
  EnumPrinters(PRINTER_ENUM_lOCAL, '', 1, @tbt, 0, PrinterNeeded, PrinterReturned); 
  if PrinterNeeded <> 0 then 
  begin 
    PrinterCount := PrinterNeeded; 
    GetMem(PrinterResultBuffer, PrinterCount); 
    EnumPrinters(PRINTER_ENUM_lOCAL, '', 1, PrinterResultBuffer, PrinterNeeded, PrinterNeeded, PrinterReturned); 
    PrinterInfo := PrinterResultBuffer; 
    //当前打印机 
    JobNeeded := 0; 
    OpenPrinter(PrinterInfo^.pName, hPrinter, nil); 
    EnumJobs(hPrinter, 0, 1, 2, @tbt, 0, JobNeeded, JobReturned); 
    if JobNeeded <> 0 then 
    begin 
      JobCount := JobNeeded; 
      GetMem(JobResultBuffer, JobCount); 
      try 
        //列举当前正在打印的任务 
        EnumJobs(hPrinter, 0, 1, 2, JobResultBuffer, JobNeeded, JobNeeded, JobReturned); 
        JobInfo := JobResultBuffer; 
        Label2.Caption := '计算机名:' + (JobInfo^.pMachineName) + 
          ' 用户名:' + string(JobInfo^.pUserName) + 
          ' 文档名:' + string(JobInfo^.pDocument); 
 
        if CurrentDocName <> JobInfo^.pDocument then 
        begin 
          if CurrentDocName <> '' then 
          begin 
            SaveData; 
            CurrentMachineName := JobInfo^.pMachineName; 
            CurrentUser := JobInfo^.pUserName; 
            CurrentDocName := JobInfo^.pDocument; 
            CurrentPrinter := JobInfo^.pPrinterName; 
            CurrentPages := InttoStr(JobInfo^.TotalPages); 
            CurrentSize := InttoStr(JobInfo^.Size); 
            CurrentCopy := InttoStr(JobInfo^.JobId); 
            CurrentPageSort := JobInfo^.pDatatype; 
          end; 
        end; 
      finally 
        FreeMem(JobResultBuffer, JobCount); 
      end; 
    end 
    else 
    begin 
      if CurrentDocName <> '' then 
      begin 
        SaveData; 
        CurrentMachineName := ''; 
        CurrentUser := ''; 
        CurrentDocName := ''; 
        CurrentPrinter := ''; 
        CurrentPages := ''; 
        Label2.Caption := ''; 
        CurrentPageSort := ''; 
      end; 
    end; 
    ClosePrinter(hPrinter); 
  end; 
end; 
 
procedure TMainForm.FlatButton4Click(Sender: TObject); 
begin 
  Close; 
end; 
 
procedure TMainForm.Timer1Timer(Sender: TObject); 
begin 
  Button1.Click; 
end; 
 
procedure TMainForm.SaveData; 
var 
  SQL_Str: string; 
  DeptName: string; 
begin 
  SQL_Str := 'Select * From 员工信息表 Where (计算机名称 = ' + #39 + CurrentMachineName + #39 + ')'; 
  WorkQuery.Active := False; 
  WorkQuery.SQL.Clear; 
  WorkQuery.SQL.Add(SQL_Str); 
  WorkQuery.Open; 
  if (WorkQuery.RecordCount > 0) then 
  begin 
    CurrentName := WorkQuery.fieldbyname('员工姓名').AsString; 
    CurrentMachineAdd := WorkQuery.fieldbyname('计算机地址').AsString; 
    DeptName := WorkQuery.fieldbyname('部门名称').AsString; 
  end; 
  SQL_Str := 'Select Top 1 * From 打印记录表 Order By 记录号 DESC '; 
  WorkQuery.Active := False; 
  WorkQuery.SQL.Clear; 
  WorkQuery.SQL.Add(SQL_Str); 
  WorkQuery.Open; 
  WorkQuery.Append; 
  WorkQuery.fieldbyname('计算机地址').AsString := CurrentMachineAdd; 
  WorkQuery.fieldbyname('计算机名称').AsString := CurrentMachineName; 
  WorkQuery.fieldbyname('用户姓名').AsString := CurrentUser; 
  WorkQuery.fieldbyname('文件名称').AsString := CurrentDocName; 
  WorkQuery.fieldbyname('文件大小').AsString := CurrentSize; 
  WorkQuery.fieldbyname('部门名称').AsString := DeptName; 
  WorkQuery.fieldbyname('姓名').AsString := CurrentName; 
  WorkQuery.fieldbyname('使用打印机').AsString := CurrentPrinter; 
  WorkQuery.fieldbyname('页数').AsString := CurrentPages; 
  WorkQuery.fieldbyname('份数').AsString := CurrentCopy; 
  WorkQuery.fieldbyname('日期').AsDateTime := Now; 
  WorkQuery.Post; 
  CurrentUser:=''; 
  CurrentName:=''; 
  InfoQuery.Active := False; 
  InfoQuery.Active := True; 
 
end; 
 
procedure TMainForm.FlatButton2Click(Sender: TObject); 
begin 
  EnterEmployeeForm := TEnterEmployeeForm.Create(Self); 
  EnterEmployeeForm.ShowModal; 
end; 
 
procedure TMainForm.FlatButton1Click(Sender: TObject); 
begin 
  EnterDeptInfoForm := TEnterDeptInfoForm.Create(Self); 
  EnterDeptInfoForm.ShowModal; 
end; 
 
procedure TMainForm.FlatButton3Click(Sender: TObject); 
begin 
  ReportOfPrintRecForm := TReportOfPrintRecForm.Create(Self); 
  ReportOfPrintRecForm.ShowModal; 
end; 
 
procedure TMainForm.FormCreate(Sender: TObject); 
var 
  I: Integer; 
begin 
  I := 0; 
  while I < Printer.Printers.Count do 
  begin 
    ListBox1.Items.Add(Printer.Printers.Strings[I]); 
    I := I + 1; 
  end; 
  ListBox1.ItemIndex := 0; 
end; 
 
procedure TMainForm.Button1Click(Sender: TObject); 
var 
  tbt: Byte; 
  PrinterNeeded, PrinterReturned, JobNeeded, JobReturned: DWORD; 
  PrinterResultBuffer, JobResultBuffer: Pointer; 
  PrinterCount, JobCount: Integer; 
  PrinterInfo: PPrinterInfo1; //打印机队列 
  JobInfo: PJobInfo2; //打印任务队列 
  hPrinter: THandle; 
begin 
  PrinterNeeded := 0; 
  PrinterReturned := 0; 
  EnumPrinters(PRINTER_ENUM_lOCAL, '', 1, @tbt, 0, PrinterNeeded, PrinterReturned); 
  if PrinterNeeded <> 0 then 
  begin 
    PrinterCount := PrinterNeeded; 
    GetMem(PrinterResultBuffer, PrinterCount); 
    EnumPrinters(PRINTER_ENUM_lOCAL, '', 1, PrinterResultBuffer, PrinterNeeded, PrinterNeeded, PrinterReturned); 
    PrinterInfo := PrinterResultBuffer; 
    //当前打印机 
    JobNeeded := 0; 
    OpenPrinter(PrinterInfo^.pName, hPrinter, nil); 
    EnumJobs(hPrinter, 0, 1, 2, @tbt, 0, JobNeeded, JobReturned); 
    if JobNeeded <> 0 then 
    begin 
      JobCount := JobNeeded; 
      GetMem(JobResultBuffer, JobCount); 
      try 
        //列举当前正在打印的任务 
        EnumJobs(hPrinter, 0, 1, 2, JobResultBuffer, JobNeeded, JobNeeded, JobReturned); 
        JobInfo := JobResultBuffer; 
        Label2.Caption := '计算机名:' + (JobInfo^.pMachineName) + 
          '】用户名:【' + string(JobInfo^.pUserName) + 
          '】文档名:〖' + string(JobInfo^.pDocument) + '〗'; 
        if ((JobInfo^.pDocument <> '') and (CurrentDocName <> JobInfo^.pDocument)) then 
        begin 
          CurrentMachineName := JobInfo^.pMachineName; 
          CurrentUser := JobInfo^.pUserName; 
          CurrentDocName := JobInfo^.pDocument; 
          CurrentPrinter := JobInfo^.pPrinterName; 
          CurrentPages := InttoStr(JobInfo^.TotalPages); 
          CurrentSize := InttoStr(JobInfo^.Size); 
          CurrentCopy := InttoStr(JobInfo^.PagesPrinted); 
          CurrentPageSort := JobInfo^.pDatatype; 
          SaveData; 
        end; 
      finally 
        FreeMem(JobResultBuffer, JobCount); 
      end; 
    end; 
    EnumPrinters(PRINTER_ENUM_lOCAL, '', 1, @tbt, 1, PrinterNeeded, PrinterReturned); 
  if PrinterNeeded <> 0 then 
  begin 
    PrinterCount := PrinterNeeded; 
    GetMem(PrinterResultBuffer, PrinterCount); 
    EnumPrinters(PRINTER_ENUM_lOCAL, '', 1, PrinterResultBuffer, PrinterNeeded, PrinterNeeded, PrinterReturned); 
    PrinterInfo := PrinterResultBuffer; 
    //当前打印机 
    JobNeeded := 0; 
    OpenPrinter(PrinterInfo^.pName, hPrinter, nil); 
    EnumJobs(hPrinter, 0, 1, 2, @tbt, 0, JobNeeded, JobReturned); 
    if JobNeeded <> 0 then 
    begin 
      JobCount := JobNeeded; 
      GetMem(JobResultBuffer, JobCount); 
      try 
        //列举当前正在打印的任务 
        EnumJobs(hPrinter, 0, 1, 2, JobResultBuffer, JobNeeded, JobNeeded, JobReturned); 
        JobInfo := JobResultBuffer; 
        Label2.Caption := '计算机名:' + (JobInfo^.pMachineName) + 
          '】用户名:【' + string(JobInfo^.pUserName) + 
          '】文档名:〖' + string(JobInfo^.pDocument) + '〗'; 
        if ((JobInfo^.pDocument <> '') and (CurrentDocName <> JobInfo^.pDocument)) then 
        begin 
          CurrentMachineName := JobInfo^.pMachineName; 
          CurrentUser := JobInfo^.pUserName; 
          CurrentDocName := JobInfo^.pDocument; 
          CurrentPrinter := JobInfo^.pPrinterName; 
          CurrentPages := InttoStr(JobInfo^.TotalPages); 
          CurrentSize := InttoStr(JobInfo^.Size); 
          CurrentCopy := InttoStr(JobInfo^.PagesPrinted); 
          CurrentPageSort := JobInfo^.pDatatype; 
          SaveData; 
        end; 
      finally 
        FreeMem(JobResultBuffer, JobCount); 
      end; 
    end; 
    ClosePrinter(hPrinter); 
  end; 
end; 
 
end.