unit U_SelPrintField;

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
  TfrmSelPrintField = class(TForm)
    RMPrintDBGrid1: TRMPrintDBGrid;
    Button1: TButton;
    Button2: TButton;
    PrintDataSource: TDataSource;
    PrnGrid: TDBGrid;
    RMXLSExport1: TRMXLSExport;
    ClientDataSet1: TClientDataSet;
    ScrollBox1: TScrollBox;
    Label4: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button2Click(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
  frmSelPrintField: TfrmSelPrintField;

implementation

{$R *.dfm}
procedure  TfrmSelPrintField.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:=ScrollBox1;
    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 TfrmSelPrintField.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 TfrmSelPrintField.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmSelPrintField.FormDestroy(Sender: TObject);
begin
  frmSelPrintField:=nil;
end;

procedure TfrmSelPrintField.FormShow(Sender: TObject);
begin
  CreateCheckBox();
  ReadINIFile(IniName);
  IsCheck();
end;
procedure TfrmSelPrintField.IsCheck();
var
  i:Integer;
  fsj:string;
begin
  with ScrollBox1 do
  begin
    for i:=0 to ScrollBox1.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 TfrmSelPrintField.PrintData();
var
  i,j,k:Integer;
  FFieldName:string;
begin
  j:=0;
  k:=0;
  with ScrollBox1 do
  begin
    for i:=0 to ScrollBox1.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 TfrmSelPrintField.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TfrmSelPrintField.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 TfrmSelPrintField.GetPrintFields();
var
  i:Integer;
begin
  PrintFields:='Begin';
  with ScrollBox1 do
  begin
    for i:=0 to ScrollBox1.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 TfrmSelPrintField.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 TfrmSelPrintField.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;

end.