unit U_SelPrintFieldNew;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Grids, DBGrids, RM_FormReport, RM_PDBGrid,
  DB,IniFiles, RM_Common, RM_Class, RM_e_Xls, DBClient;

type
  TfrmSelPrintFieldNew = class(TForm)
    RMPrintDBGrid1: TRMPrintDBGrid;
    Button1: TButton;
    Button2: TButton;
    PrintDataSource: TDataSource;
    PrnGrid: TDBGrid;
    RMXLSExport1: TRMXLSExport;
    ClientDataSet1: TClientDataSet;
    Panel2: TScrollBox;
    Label4: TLabel;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
  private
    procedure  CreateCheckBox();
    procedure  PrintData();
    procedure  ReadINIFile(fieldname:string);
    procedure  WriteINIFile(fieldname:string);
    Function   IsINIFile(fieldname:string):Boolean;
    procedure  GetPrintFields();
    procedure  IsCheck();
    { Private declarations }
  public
    PrintFields,IniName:string;

    { Public declarations }
  end;
var
  frmSelPrintFieldNew: TfrmSelPrintFieldNew;

implementation

{$R *.dfm}
procedure  TfrmSelPrintFieldNew.CreateCheckBox();
var
  i,j,FTop,FLeft,Fdiv,FMod:Integer;// mod 余数，div商
  FCheckBox:TCheckBox;
begin
  for i:=0 to PrnGrid.Columns.Count-1 do
  begin
    Fdiv:=(i+1) div 3;
    FMod:=(i+1) mod 3;
    FCheckBox:=TCheckBox.Create(Self);
    FCheckBox.Caption:=Trim(PrnGrid.Columns[i].Title.Caption);
    FCheckBox.TabOrder:=i;
    FCheckBox.Parent:=Panel2;
    FCheckBox.Checked:=True;
    if FMod>0 then
      FCheckBox.Top:=36*(Fdiv+1)
    else
      FCheckBox.Top:=36*Fdiv;
    if FMod=1 then
      FCheckBox.Left:=29
    else if FMod=2 then
      FCheckBox.Left:=163
    else if FMod=0 then
      FCheckBox.Left:=305;

  end;
end;

procedure TfrmSelPrintFieldNew.Button1Click(Sender: TObject);
begin
  //ShowMessage('10除以3取余'+inttostr(10 mod 3)+',取整'+inttostr(10 div 3));
  PrintData();
  GetPrintFields();
  if IsINIFile(IniName)=True then
  begin
    DeleteFile(IniName);
  end;
    WriteINIFile(IniName);
end;

procedure TfrmSelPrintFieldNew.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmSelPrintFieldNew.FormDestroy(Sender: TObject);
begin
  frmSelPrintFieldNew:=nil;
end;

procedure TfrmSelPrintFieldNew.FormShow(Sender: TObject);
begin
  CreateCheckBox();
  ReadINIFile(IniName);
  IsCheck();
end;
procedure TfrmSelPrintFieldNew.IsCheck();
var
  i:Integer;
  fsj:string;
begin
  with Panel2 do
  begin
    for i:=0 to Panel2.ControlCount-1 do
    begin
      if Controls[i] is TCheckBox then
      begin
        fsj:=Trim(TCheckBox(Controls[i]).Caption);
        if Pos(fsj,PrintFields)>0 then
          TCheckBox(Controls[i]).Checked:=True
        else
         TCheckBox(Controls[i]).Checked:=False;
      end;
    end;
  end;
end;

procedure TfrmSelPrintFieldNew.PrintData();
var
  i,j,k:Integer;
  FFieldName:string;
begin
  j:=0;
  k:=0;
  with Panel2 do
  begin
    for i:=0 to Panel2.ControlCount-1 do
    begin
      if Controls[i] is TCheckBox then
      begin
        if TCheckBox(Controls[i]).Checked=True then
        begin
          j:=1;
          PrnGrid.Columns[TCheckBox(Controls[i]).TabOrder].Visible:=True;
          k:=k+1;
          if k=1 then
          begin
            FFieldName:=PrnGrid.Columns[TCheckBox(Controls[i]).TabOrder].FieldName;
          end;
        end else
        begin
          PrnGrid.Columns[TCheckBox(Controls[i]).TabOrder].Visible:=False;
        end;
      end;
    end;
  end;
  {ClientDataSet1.Last;
  if Trim(ClientDataSet1.FieldByName('flag').AsString)='Y' then
  begin
    ClientDataSet1.Edit;
    ClientDataSet1.FieldByName(FFieldName).Value:='合计';
    ClientDataSet1.Post;
  end;}

  if j=1 then
  begin
    RMPrintDBGrid1.ShowReport ;
  end
  else
  begin
    Application.MessageBox('没有可打印的信息！','提示',0);
    Exit;
  end;
  //Panel2.Visible:=False;
  //RMPrintDBGrid1.ShowReport;

end;

procedure TfrmSelPrintFieldNew.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TfrmSelPrintFieldNew.ReadINIFile(fieldname:string);
var
  programIni:Tinifile;  //配置文件名
  FName:string;
begin
  FName:=ExtractFilePath(Application.ExeName)+'FieldPrintSet\' +trim(fieldname)+'.INI';
  programIni:=Tinifile.create(FName);
  PrintFields:=programIni.ReadString('打印设置','打印字段','');
  programIni.Free;
end;
procedure TfrmSelPrintFieldNew.GetPrintFields();
var
  i:Integer;
begin
  PrintFields:='Begin';
  with Panel2 do
  begin
    for i:=0 to Panel2.ControlCount-1 do
    begin
      if Controls[i] is TCheckBox then
      begin
        if TCheckBox(Controls[i]).Checked then
        begin
          PrintFields:=PrintFields+'/'+TCheckBox(Controls[i]).Caption;
        end;
      end;
    end;
  end;
end;
//////////////////////////////////////////////////////////////////
  //写设置信息到INI文件
  //参数
//////////////////////////////////////////////////////////////////
procedure TfrmSelPrintFieldNew.WriteINIFile(fieldname:string);
var
  programIni:Tinifile;  //配置文件名
  FName:string;
begin

  FName:=ExtractFilePath(Application.ExeName)+'FieldPrintSet\'+trim(fieldname)+'.INI';
  if not DirectoryExists(ExtractFileDir(FName)) then
    CreateDir(ExtractFileDir(FName));
  programIni:=Tinifile.create(FName);
  programIni.WriteString('打印设置','打印字段',PrintFields);
  programIni.Free;
end;
//////////////////////////////////////////////////////////////////
  //判断InI文件是否存在
//////////////////////////////////////////////////////////////////
 Function TfrmSelPrintFieldNew.IsINIFile(fieldname:string):Boolean;
 var
   FName:string;
 begin
   FName:=ExtractFilePath(Application.ExeName)+'FieldPrintSet\'+trim(fieldname)+'.INI';
   if FileExists(FName) then
      Result:=True
   else
      Result:=false;
 end;

procedure TfrmSelPrintFieldNew.CheckBox1Click(Sender: TObject);
var
  i:Integer;
begin
  if CheckBox1.Checked then
  begin
    CheckBox2.Checked:=False;
    with Panel2 do
    begin
      for i:=0 to Panel2.ControlCount-1 do
      begin
        if Controls[i] is TCheckBox then
        begin
          if (TCheckBox(Controls[i]).Name<>'CheckBox1') and (TCheckBox(Controls[i]).Name<>'CheckBox2') then
          begin
            TCheckBox(Controls[i]).Checked:=True;
          end;
        end;
      end;
    end;
  end;
  
end;

procedure TfrmSelPrintFieldNew.CheckBox2Click(Sender: TObject);
var
  i:Integer;
begin
  if CheckBox2.Checked then
  begin
    CheckBox1.Checked:=False;
    with Panel2 do
    begin
      for i:=0 to Panel2.ControlCount-1 do
      begin
        if Controls[i] is TCheckBox then
        begin
          if (TCheckBox(Controls[i]).Name<>'CheckBox1') and (TCheckBox(Controls[i]).Name<>'CheckBox2') then
          begin
            TCheckBox(Controls[i]).Checked:=False;
          end;
        end;
      end;
    end;
  end;
  
end;

end.
