filter.pas

来自「IT业进销存管理系统源代码Delphi」· PAS 代码 · 共 764 行 · 第 1/2 页

PAS
764
字号
unit Filter;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, StdCtrls, Buttons,db, DBGrids, IniFiles;

type
  TfrmFilter = class(TForm)
    StringGrid1: TStringGrid;
    btnCancel: TBitBtn;
    cbXm: TComboBox;
    cbBjfs: TComboBox;
    cbZhfs: TComboBox;
    btnOk: TBitBtn;
    btnIni: TBitBtn;
    CheckBox1: TCheckBox;
    btnClear: TBitBtn;
    ListBox1: TListBox;
    btnSelect: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure StringGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure cbXmKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btnOkClick(Sender: TObject);
    procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure cbXmExit(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure btnIniClick(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure ListBox1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnSelectClick(Sender: TObject);
    procedure ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure ListBox1Exit(Sender: TObject);
  private
    { Private declarations }
    slField: TStringList;
    iniPos: TIniFile;
    strFileName: string;
    function IsVisibleField(const f: TField; const b: boolean): boolean;
    function GetFilterString(const AFd: TField; const ARow: Integer): string;
    function FieldOf(Data: TDataSet; FieldCaption: string): TField;
    function IsVisibleNumber(const s: string): string;
    function IsVisibleDate(const s: string): string;
    procedure OpenListBox;
  public
    { Public declarations }
    AFormName: string;
    dsFt: TDataset;
    dbgFt: TDBGrid;
    strFt: string;
  end;

var
  frmFilter: TfrmFilter;

implementation

uses Main, Dm, ComJbzl, ComFun;

{$R *.DFM}

//Form.Create
procedure TfrmFilter.FormCreate(Sender: TObject);
begin
  frmMain.ilsSub.GetIcon(7, self.Icon);
  StringGrid1.Cells[0,0] := '查询项目';
  StringGrid1.Cells[1,0] := '比较方式';
  StringGrid1.Cells[2,0] := '查询内容';
  StringGrid1.Cells[3,0] := '组合方式';
  slField := TStringList.Create;
end;

//Form.Show
procedure TfrmFilter.FormShow(Sender: TObject);
var
  i, j: integer;
  blFromDs: Boolean;
  arrItem: array of array of string;  //[0,0]查询项目 [0,1]比较方式 [0,2]是否日期
  arrIndex: array of integer;
begin
  strFileName := ExtractFilePath(Application.ExeName) + 'Filter\' + Screen.ActiveForm.Name + '.ini';
  strFt := '';
  blFromDs := false;
  if dsFt = nil then
    dsFt := dbgFt.DataSource.DataSet
  else
    blFromDs := true;
  if (AFormName = screen.ActiveForm.Name) and CheckBox1.Checked then
  begin
    with StringGrid1 do
    begin
      Options := Options - [goEditing];
      Col := 2;
      Row := 1;
      SetFocus;
    end;
    exit;
  end
  else begin
    AFormName := screen.ActiveForm.Name;
    CheckBox1.Checked := false;
    slField.Clear;
  end;
  cbXm.Visible := false;
  cbBjfs.Visible := false;
  cbZhfs.Visible := false;
  cbXm.Items.Clear;
  SetLength(arrItem, 0, 0);
  SetLength(arrIndex, 0);
  for i := 0 to dsFt.FieldCount-1 do
    if isVisibleField(dsFt.Fields[i], blFromDs) then
    begin
      cbXm.Items.Add(dsFt.Fields[i].DisplayLabel);
      //StringList
      slField.AddObject(dsFt.Fields[i].DisplayLabel, dsFt.Fields[i]);
      //ArrItem
      SetLength(arrItem, High(arrItem) + 2 , 3);
      arrItem[High(arrItem), 0] := dsFt.Fields[i].DisplayLabel;
      arrItem[High(arrItem), 1] := '';
      if dsFt.Fields[i].DataType in [ftDateTime, ftDate] then
        arrItem[High(arrItem), 2] := 't'
      else
        arrItem[High(arrItem), 2] := 'f';
      //ArrIndex
      SetLength(arrIndex, High(arrIndex) + 2);
      arrIndex[High(arrIndex)] := High(arrIndex);
    end;
  //Set ArrIndex
  with Data.Tmp do
  begin
    Close;
    CommandText := 'select * from AppFilter ' +
      'where uID = ' + IntToStr(pintUserId) + ' and ' +
      'FormCaption = ''' + Application.MainForm.ActiveMDIChild.Name + ''' ' +
      'order by UseCount desc, FieldIndex';
    Open;
    while not Eof do
    begin
      for j := 0 to High(arrIndex) do
        if arrItem[j, 0] = FieldByName('FieldCaption').AsString then
        begin
          arrItem[j, 1] := FieldByName('Bjfs').AsString;
          { 按使用次数排
          if j <> RecNo - 1 then
          begin
            for k := j downto RecNo do
            begin
              arrIndex[k - 1] := arrIndex[k - 1] + arrIndex[k];
              arrIndex[k] := arrIndex[k - 1] - arrIndex[k];
              arrIndex[k - 1] := arrIndex[k - 1] - arrIndex[k]
            end;
          end;
          }
          break;
        end;
      Next;
    end;
  end;
  //Add StringGrid1
  with StringGrid1 do
  begin
    for i := 1 to RowCount - 1 do
      Cells[2, i] := '';
    RowCount := 1;
    for i := 0 to High(arrIndex) do
    begin
      RowCount := RowCount + 1;
      Cells[0, RowCount - 1] := arrItem[arrIndex[i], 0];
      if arrItem[arrIndex[i], 2] = 't' then
      begin
        Cells[1, RowCount - 1] := '大于等于';
        //第二个条件
        RowCount := RowCount + 1;
        Cells[0, RowCount - 1] := arrItem[arrIndex[i], 0];
        Cells[1, RowCount - 1] := '小于等于';
        Cells[2, RowCount - 1] := '';
        Cells[3, RowCount - 2] := '并且';
      end
      else
      begin
        if arrItem[arrIndex[i], 1] = '' then
          Cells[1, RowCount - 1] := '等于'
        else
          Cells[1, RowCount - 1] := arrItem[arrIndex[i], 1];
      end;
      Cells[2, RowCount - 1] := '';
      Cells[3, RowCount - 1] := '并且'
    end;
    FixedRows := 1;
    Col := 2;
    iniPos := TiniFile.Create(strFileName);
    Row := iniPos.ReadInteger('', 'Row', 1);
    iniPos.Free;
    SetFocus;
  end;
end;

//isVisibleField
function TfrmFilter.IsVisibleField(const f: TField; const b: boolean): boolean;
var
  i: integer;
begin
  result := False;
  if (f.Visible) and (f.Tag = 0) and (f.FieldKind = fkData) then
    if not b then
      for i := 0 to dbgFt.Columns.Count - 1 do
      begin
        if (dbgFt.Columns[i].Visible) and (f.FieldName = dbgFt.Columns[i].FieldName) then
        begin
          result := True;
          break;
        end
      end
    else
      Result := True;
end;

//Form.Close
procedure TfrmFilter.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  dsFt := nil;
  dbgFt := nil;
end;

//FormKey.Down
procedure TfrmFilter.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  with StringGrid1 do
  begin
    if Col = 2 then
      Options := Options + [goEditing]
    else
      Options := Options - [goEditing];
    if (Key = VK_F4) and (Col = 2) then
      OpenListBox;
  end;
end;

//StringGrid1.KeyDown
procedure TfrmFilter.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  retCell: TRect;
begin
  if Key = VK_RETURN then
  begin
    cbXm.Visible := false;
    cbBjfs.Visible := false;
    cbZhfs.Visible := false;
    with StringGrid1 do
    begin
      case Col of
        0:
          begin
            btnCancel.Cancel := false;
            retCell := CellRect(Col, Row);
            cbXm.Top := Top + retCell.Top + 1;
            cbXm.Left := Left + retCell.Left;
            cbXm.Itemindex := cbXm.Items.IndexOf(Cells[Col, Row]);
            cbXm.Visible := true;
            cbXm.SetFocus;
          end;
        1:
          begin
            btnCancel.Cancel := false;
            retCell := CellRect(Col, Row);
            cbBjfs.Top := Top + retCell.Top + 1;
            cbBjfs.Left := Left + retCell.Left;
            cbBjfs.Itemindex := cbBjfs.Items.IndexOf(Cells[Col, Row]);
            cbBjfs.Visible := true;
            cbBjfs.SetFocus;
          end;
        2:
          if EditorMode and (Cells[Col, Row] <> '') and (sender <> nil) then btnOk.SetFocus;
        3:
          begin
            btnCancel.Cancel := false;
            retCell := CellRect(Col, Row);
            cbZhfs.Top := Top + retCell.Top + 1;
            cbZhfs.Left := Left + retCell.Left;
            cbZhfs.Itemindex := cbZhfs.Items.IndexOf(Cells[Col, Row]);
            cbZhfs.Visible := true;
            cbZhfs.SetFocus;
          end;
      end;
    end;
  end
  else if Key = VK_DELETE then
    with StringGrid1 do
      if Pos('空值', Cells[1, Row]) = 0 then
        Cells[2, Row] := ''
      else
        Cells[1, Row] := '等于';
end;

//cbXm.Exit
procedure TfrmFilter.cbXmExit(Sender: TObject);
begin
  btnCancel.Cancel := true;
  with StringGrid1 do
  begin
    case Col of
      0:
        begin
          Cells[Col, Row] := cbXm.Text;
          Col := Col + 1;
        end;
      1:
        begin
          Cells[Col, Row] := cbBjfs.Text;
          Col := Col + 1;
        end;
      3:
        begin
          Cells[Col, Row] := cbZhfs.Text;
          if Row = RowCount - 1 then
            btnOk.SetFocus
          else
          begin
            Col := 2;
            Row := Row + 1;
          end;
        end;
    end;
  end;
end;

//cbXm.KeyDown
procedure TfrmFilter.cbXmKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_RETURN) or (Key = VK_ESCAPE) then
  begin
    cbXm.Visible := false;
    cbBjfs.Visible := false;
    cbZhfs.Visible := false;
    StringGrid1.SetFocus;
  end;
end;

//StringGrid1.MouseDown
procedure TfrmFilter.StringGrid1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  wdKey : word;
begin
  wdKey := 13;
  FormKeyDown(nil, wdKey, []);
  StringGrid1KeyDown(nil, wdKey, []);
end;

//StringGrid1.DrawCell
procedure TfrmFilter.StringGrid1DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  i: integer;
begin
  with StringGrid1 do
  begin
    btnSelect.Visible := Col = 2;
    if btnSelect.Visible and (ARow = Row) then
    begin
      btnSelect.Visible := True;
      btnSelect.Top := CellRect(Col, Row).Top + Top + 2;
    end;
  end;
  if not (State = [gdSelected, gdFixed]) and (ARow <> 0) Then

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?