unit U_WindowFormdesign;

interface

uses
  Windows, SysUtils, Messages, Classes, Controls, StdCtrls, ComCtrls, Forms,
  ExtCtrls, ComObj, DXCORE, ActiveX, cxMemo, cxButtons, Graphics, IniFiles,
  Dialogs, Menus, DB, ADODB, cxLabel, BtnEdit, RTComboBox, cxTextEdit, cxMRUEdit,
  cxDBData, cxGridLevel, cxButtonEdit, cxCurrencyEdit, cxGridTableView, cxGrid,
  cxGridCustomTableView, cxGraphics, cxCalendar, cxCheckBox, cxGridDBTableView,
  cxGridDBBandedTableView, cxStyles, cxPc, cxGroupBox, U_cxGridCustomCss,
  cxCustomData, cxClasses, cxTimeEdit, cxEdit, cxLookAndFeels,
  cxLookAndFeelPainters, cxGridCustomView, Buttons, cxDropDownEdit;

type
  TWindowFormDeSign = class(TObject)
  private
    fFontSize: integer;
    fWhatDesign: string;
    fFormCaption: string;
    fDllName: string;
    fFormName: string;
    fFormId: integer;
    fIsShowColumnfilter: boolean;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    function IsHasDesignRight(ADOTmp: TADOQuery): boolean;
    procedure SetCxGridColumnBestWidth(cxGrid: TcxGridDBTableView);
    function SaveFormGridInfo(mCxGridView: TcxGridDBTableView; ADOCmd: TADOQuery; mdesignCode: string; mGridName: string; mGridType: integer): boolean;
    function InitContainerControls(mdesignCode: string; containerCtrl: TwinControl): boolean;
    procedure SetFormOperRight(mParent: TwinControl; mRightCaptions: string);
    function GetMaxId(): integer;
    procedure onDesignDblClick(Sender: TObject);
    procedure cxGridViewKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

    procedure cxGridViewSelectionChanged(Sender: TcxCustomGridTableView);
    procedure CustomDrawcxGridHeader(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridColumnHeaderViewInfo; var ADone: boolean);
    procedure CreateCxGridColumn(tv1: TcxGridDBTableView; ADOQueryTmp: TADOQuery; mdesignCode, mGridName: string; HasFooter: boolean = true);
    function SaveContainerControInfo(mParentCtrl: TwinControl; ADOCmd: TADOQuery; mdesignCode: string; mDesigntype: integer): boolean;
    function InitCtrlData(mCtrl: TControl; mSql: string; mdefault: string): boolean;
    procedure OpenGridDesignWin(mCxGridView: TcxGridDBTableView);
    procedure OpenContainerDesign(containerCtrl: TwinControl);
    procedure ReadINIFileSetInfo(mfontSize: integer);
    function getChgDefaultValue(mValue: string): string;
    procedure cxGridViewCustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
  public
    function FormStyleInit(mForm: Tform; mFormId: integer; ADOTmp: TADOQuery; ADOQCmd: TADOQuery; mStyleCode: string = '01'; mRightCaptions: string = ''): boolean;
    procedure QueryDataPlus(ADOTmp: TADOQuery);

  end;

const
  // panel背景
  clPanelBack = TColor($00FAF8F7);
  clToolBarBack = TColor($00FAF8F7);
  // 窗口背景色
  clFormBack = TColor($00FAF8F7);

implementation

uses
  U_DataLink, U_globalVar;

/// //////////////////////////////////////////////////////
// 窗口样式,其它界面信息设置；
/// //////////////////////////////////////////////////////
function TWindowFormDeSign.FormStyleInit(mForm: Tform; mFormId: integer; ADOTmp: TADOQuery; ADOQCmd: TADOQuery; mStyleCode: string = '01'; mRightCaptions: string = ''): boolean;
var
  i: integer;
  cxStyle_GridOddRow: TcxStyle;
  cxStyle_GridEvenRow: TcxStyle;
  cxStyle_active: TcxStyle;
  cxStyle_gridGroupBox: TcxStyle;
  cxStyle_gridFoot: TcxStyle;
  cxStyle_selection: TcxStyle;
  mvalue: TcxStyleValues;
  mCtl3D, mToolBarList: boolean;
  mIsCustomBackColor: boolean;
  mIsDrawGridHeader: boolean;
  mdesignCode: string;
begin
  ADOQueryCmd := ADOQCmd;
  ADOQueryTmp := ADOTmp;
  fDllName := gDllFileName;
  fFormName := Tform(mForm).name;
  fFormId := mFormId;
  if fFontSize < 9 then
    fFontSize := 9;
  if trim(mStyleCode) = '' then
    mStyleCode := '01';
  /// /////////////////////////////
  // 设置项目
  with ADOTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from SD_ErpStyleParam');
    sql.Add('where StyleCode=' + quotedStr(mStyleCode));
    open;
    if RecordCount > 0 then
    begin
      fWhatDesign := trim(fieldByName('WhatDesign').AsString);
      mCtl3D := fieldByName('IsCtl3D').AsBoolean;
      mToolBarList := fieldByName('IsToolBarList').AsBoolean;
      mIsCustomBackColor := fieldByName('IsCustomBackColor').AsBoolean;
      mIsDrawGridHeader := fieldByName('IsDrawGridHeader').AsBoolean;
      fIsShowColumnfilter := fieldByName('IsShowColumnfilter').AsBoolean;
      if fieldByName('fontSize').AsInteger > 9 then
      begin
        fFontSize := fieldByName('fontSize').AsInteger;
      end;
    end;

  end;

  ReadINIFileSetInfo(fFontSize);
   //mForm.Font.name := '宋体';
  mForm.Font.Size := fFontSize;
  // 窗口背景色
  if mIsCustomBackColor then
    mForm.Color := clFormBack;

  for i := 0 to mForm.ComponentCount - 1 do
  begin
    if (fFontSize > 9) then
    begin
      if mForm.Components[i] is Tlabel then
      begin
        if Tlabel(mForm.Components[i]).Left >= fFontSize then
          Tlabel(mForm.Components[i]).Left := Tlabel(mForm.Components[i]).Left - fFontSize;
        continue;
      end;
      if mForm.Components[i] is TdatetimePicker then
      begin
        if TdatetimePicker(mForm.Components[i]).Width <= 90 then
          TdatetimePicker(mForm.Components[i]).Width := TdatetimePicker(mForm.Components[i]).Width + ffontsize * 2;
        continue;
      end;
    end;
    if mForm.Components[i] is TToolBar then
    begin
      TToolBar(mForm.Components[i]).AutoSize := true;
      TToolBar(mForm.Components[i]).Flat := true;
      if mIsCustomBackColor then
        TToolBar(mForm.Components[i]).Color := clToolBarBack;
      if mForm.Components[i].Tag = 0 then
      begin
        if ADOTmp.IsEmpty then
          TToolBar(mForm.Components[i]).List := false
        else
          TToolBar(mForm.Components[i]).List := mToolBarList;
      end;
      SetFormOperRight(TToolBar(mForm.Components[i]), mRightCaptions);
      continue;
    end;
    if mForm.Components[i] is TcxTabControl then
    begin
      // TcxTabControl(mForm.Components[i]).Color :=  clToolBarBack  ;
      continue;
    end;
    if mForm.Components[i] is TcomBoBox then
    begin
      // TcomBoBox(mForm.Components[i]).Color :=  clToolBarBack  ;
      continue;
    end;
    // Tpanel
    if mForm.Components[i] is Tpanel then
    begin
      Tpanel(mForm.Components[i]).OnDblClick := onDesignDblClick;
      if mIsCustomBackColor and (Tpanel(mForm.Components[i]).Tag < 99) then
        Tpanel(mForm.Components[i]).Color := clPanelBack;
      Tpanel(mForm.Components[i]).Ctl3D := mCtl3D;
      mdesignCode := fFormName + '_' + intTostr(fFormId) + '_' + Tpanel(mForm.Components[i]).name;
      InitContainerControls(mdesignCode, Tpanel(mForm.Components[i]));

      continue;
    end;
    // Tpanel
    if mForm.Components[i] is TScrollBox then
    begin
      TScrollBox(mForm.Components[i]).OnDblClick := onDesignDblClick;
      if mIsCustomBackColor and (Tpanel(mForm.Components[i]).Tag < 99) then
        Tpanel(mForm.Components[i]).Color := clPanelBack;
      Tpanel(mForm.Components[i]).Ctl3D := mCtl3D;

      mdesignCode := fFormName + '_' + intTostr(fFormId) + '_' + TScrollBox(mForm.Components[i]).name;
      InitContainerControls(mdesignCode, TScrollBox(mForm.Components[i]));

      continue;
    end;
    // Tcxgroup
    if mForm.Components[i] is TgroupBox then
    begin
      TgroupBox(mForm.Components[i]).OnDblClick := onDesignDblClick;
      if mIsCustomBackColor then
        TgroupBox(mForm.Components[i]).Color := clPanelBack;
      TgroupBox(mForm.Components[i]).Ctl3D := mCtl3D;
      mdesignCode := fFormName + '_' + intTostr(fFormId) + '_' + TgroupBox(mForm.Components[i]).name;
      InitContainerControls(mdesignCode, TgroupBox(mForm.Components[i]));

      continue;
    end;
    // TcxPageControl
    if mForm.Components[i] is TcxPageControl then
    begin
      // TcxPageControl(mForm.Components[i]).OnDblClick:= onDesignDblClick ;
      if mIsCustomBackColor then
        TcxPageControl(mForm.Components[i]).Color := clPanelBack;
      // mDesignCode:=fFormName+'_'+intTostr(fFormid)+'_'+ TcxPageControl(mForm.Components[i]).Name ;
      // InitContainerControls(mDesignCode, TcxPageControl(mForm.Components[i]));
      continue;
    end;
    // Tcxgroup
    if mForm.Components[i] is TcxgroupBox then
    begin
      TcxgroupBox(mForm.Components[i]).OnDblClick := onDesignDblClick;
      if mIsCustomBackColor then
        TcxgroupBox(mForm.Components[i]).Color := clPanelBack;
      TcxgroupBox(mForm.Components[i]).Ctl3D := mCtl3D;
      mdesignCode := fFormName + '_' + intTostr(fFormId) + '_' + TcxgroupBox(mForm.Components[i]).name;
      InitContainerControls(mdesignCode, TcxgroupBox(mForm.Components[i]));
      continue;
    end;
    if mForm.Components[i] is Tcxtimeedit then
    begin
      Tcxtimeedit(mForm.Components[i]).Style.BorderStyle := ebsFlat;
      continue;
    end;
    // TcxGrid  and gIsCustomDrawGridHeader
    if mForm.Components[i] is TcxGrid then
    begin
      TcxGrid(mForm.Components[i]).Font.Size := fFontsize;
      TcxGrid(mForm.Components[i]).LookAndFeel.ScrollbarMode := sbmClassic;

      cxStyle_GridOddRow := TcxStyle.Create(mForm.Components[i]);
      cxStyle_GridEvenRow := TcxStyle.Create(mForm.Components[i]);
      cxStyle_active := TcxStyle.Create(mForm.Components[i]);
      cxStyle_selection := TcxStyle.Create(mForm.Components[i]);
      cxStyle_gridGroupBox := TcxStyle.Create(mForm.Components[i]);
      cxStyle_gridFoot := TcxStyle.Create(mForm.Components[i]);
      cxStyle_GridOddRow.Color := $00FDFAF9; // $00FBF6F2;
      cxStyle_GridEvenRow.Color := $20F3FBFB;
      cxStyle_active.Color := $0047D5FE; // $00C4EFFF; $00C6D9F9
      cxStyle_active.Font.Style := [];
      cxStyle_active.Font.name := '宋体';

      cxStyle_gridGroupBox.Color := $20F3FBFB;
      cxStyle_selection.Color := $0047D5FE;
      cxStyle_gridFoot.Color := clPanelBack;
      cxStyle_gridFoot.Font.Color := clBlack;
      //cxStyle_gridFoot.Font.name:='宋体';
      cxStyle_selection.Font.Color := clBlack;
      cxStyle_selection.Font.Style := [];
      cxStyle_selection.Font.name := '宋体';
      //cxStyle_active.Font.color :=   clBlack;

      cxStyle_active.Font.Size := fFontsize;
      cxStyle_gridFoot.Font.Size := ffontsize;
      cxStyle_selection.Font.Size := ffontsize;
      //cxStyle_gridGroupBox.Font.Size:=  ffontsize;
      // cxStyle_gridOddRow.font.Size:=ffontsize;
      // cxStyle_GridEvenRow.Font.Size:=ffontsize;

      // cxStyle_gridFoot.font.Style:=[fsBold];
      if TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.Header <> nil then
        TcxStyle(TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.Header).Font.Size := ffontsize;

      if TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.contentOdd = nil then
        TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.contentOdd := cxStyle_GridOddRow;
      if TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.contentEven = nil then
        TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.contentEven := cxStyle_GridEvenRow;
      if TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.inactive = nil then
        TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.inactive := cxStyle_active
      else
        TcxStyle(TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.inactive).Font.Size := ffontsize;

      if TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.selection = nil then
      begin
        TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.selection := cxStyle_selection;
        TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.selection.AssignedValues := [];
      end
      else
        TcxStyle(TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.selection).Font.Size := ffontsize;

      TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.groupByBox := cxStyle_gridGroupBox;
      TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).styles.Footer := cxStyle_gridFoot;

      //TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0])
      //  .styles.Header := nil;
      TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).optionsView.groupByBox := false;
      if mIsDrawGridHeader and (TcxGrid(mForm.Components[i]).Tag <> 1) then
      begin
        TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).optionsCustomize.columnfiltering := true;

      end;

      //自动响应过滤
      TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).dataController.filter.AutoDataSetFilter := true;

      if mForm.Components[i].Tag < 99 then
        SetCxGridColumnBestWidth(TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]));

      TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).OnKeyDown := cxGridViewKeyDown;
      TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).OnSelectionChanged := cxGridViewSelectionChanged;

      TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).OnCustomDrawIndicatorCell := cxGridViewCustomDrawIndicatorCell;

      TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).optionsCustomize.columnfiltering := true;
      mdesignCode := fFormName + '_' + intTostr(fFormId) + '_' + TcxGridDBTableView(TcxGrid(mForm.Components[i]).Views[0]).name;

     // CreateCxGridColumn(TcxGridDBTableView(TcxGrid(mForm.Components[i])
       // .Views[0]), ADOQueryTmp, mdesignCode, '');

      continue;
    end;

  end;
end;

/// ////////////////////////////////////////////////////////////////////
// 保存控件位置信息
/// ///////////////////////////////////////////////////////////////////////
function TWindowFormDeSign.SaveContainerControInfo(mParentCtrl: TwinControl; ADOCmd: TADOQuery; mdesignCode: string; mDesigntype: integer): boolean;
var
  i: integer;
begin
  result := false;
  try
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * ');
      sql.Add('from SD_FormControl_Design');
      sql.Add('where designCode=' + quotedStr(mdesignCode));
      open;

      if RecordCount <= 0 then
      begin
        append;
        fieldByName('designCode').value := mdesignCode;
        fieldByName('InitType').value := 'systemset';
        fieldByName('Createman').value := DName;
        fieldByName('valid').value := 'Y';
      end
      else
      begin
        edit;
      end;

      fieldByName('Designtype').value := mDesigntype;
      fieldByName('ParentCtrlName').value := mParentCtrl.name;
      fieldByName('ParentCtrlType').value := mParentCtrl.ClassName;

      fieldByName('ParentHeight').value := mParentCtrl.Height;
      fieldByName('DllFileName').value := fDllName;
      fieldByName('formId').value := fFormId;
      fieldByName('formName').value := fFormName;
      post;

    end;

    /// ////////////////
    // 子信息
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * ');
      sql.Add('from SD_FormControl_Design_sub');
      sql.Add('where designCode=' + quotedStr(mdesignCode));
      open;

      for i := mParentCtrl.ControlCount - 1 downto 0 do
      begin
        if not locate('ControlName', mParentCtrl.Controls[i].name, []) then
        begin
          append;
          fieldByName('designCode').value := trim(mdesignCode);
          fieldByName('ControlName').value := mParentCtrl.Controls[i].name;

          if (mParentCtrl.Controls[i] is Tlabel) or (mParentCtrl.Controls[i] is Tcxlabel) then
            fieldByName('DefaultCaption').value := Tlabel(mParentCtrl.Controls[i]).Caption
          else if (mParentCtrl.Controls[i] is TButton) or (mParentCtrl.Controls[i] is TcxButtonEdit) then
            fieldByName('DefaultCaption').value := TButton(mParentCtrl.Controls[i]).Caption
          else if (mParentCtrl.Controls[i] is TRadioButton) then
            fieldByName('DefaultCaption').value := TRadioButton(mParentCtrl.Controls[i]).Caption;

          fieldByName('ControlClass').value := mParentCtrl.Controls[i].ClassName;
          // fieldByName('DefLeftPos').Value :=TWinControl(mParentCtrl.Controls[i]).Left;
          // fieldByName('DefTopPos').Value :=TWinControl(mParentCtrl.Controls[i]).Top;
          // fieldByName('DefWidth').Value :=TWinControl(mParentCtrl.Controls[i]).Width;
          // fieldByName('DefHeight').Value :=TWinControl(mParentCtrl.Controls[i]).Height;
          fieldByName('TabOrder').value := TwinControl(mParentCtrl.Controls[i]).TabOrder;
          // fieldByName('defTabOrder').Value := TWinControl(mParentCtrl.Controls[i]).TabOrder ;
          fieldByName('Visible').value := TwinControl(mParentCtrl.Controls[i]).Visible;

          fieldByName('creator').value := DName;
          fieldByName('Valid').value := 'Y';

        end
        else
        begin
          continue;
          // Edit;
        end;

        fieldByName('ControlLeft').value := TwinControl(mParentCtrl.Controls[i]).Left;
        fieldByName('ControlTop').value := TwinControl(mParentCtrl.Controls[i]).Top;
        fieldByName('ControlWidth').value := TwinControl(mParentCtrl.Controls[i]).Width;
        fieldByName('ControlHeight').value := TwinControl(mParentCtrl.Controls[i]).Height;

        if (mParentCtrl.Controls[i] is Tlabel) or (mParentCtrl.Controls[i] is Tcxlabel) then
          fieldByName('Caption').value := Tlabel(mParentCtrl.Controls[i]).Caption
        else if (mParentCtrl.Controls[i] is TButton) or (mParentCtrl.Controls[i] is TcxButtonEdit) then
          fieldByName('Caption').value := TButton(mParentCtrl.Controls[i]).Caption
        else if (mParentCtrl.Controls[i] is TcheckBox) then
          fieldByName('Caption').value := TcheckBox(mParentCtrl.Controls[i]).Caption
        else if (mParentCtrl.Controls[i] is TcxcheckBox) then
          fieldByName('Caption').value := TcxcheckBox(mParentCtrl.Controls[i]).Caption
        else if (mParentCtrl.Controls[i] is TRadioButton) then
          fieldByName('Caption').value := TRadioButton(mParentCtrl.Controls[i]).Caption;

        if mParentCtrl.Controls[i] is Tlabel then
        begin
          fieldByName('fontSize').value := Tlabel(mParentCtrl.Controls[i]).Font.Size;
          fieldByName('fontColor').value := Tlabel(mParentCtrl.Controls[i]).Font.Color;
        end;

        post;
      end;
    end;
    ADOCmd.close;
    result := true;
  except
    application.MessageBox('保存父容器控件信息时发生错误', '提示信息', 0);
  end;
end;

/// ////////////////////////////////////////////////////////////////////
// 保存窗口表格列的原始信息
/// ///////////////////////////////////////////////////////////////////////
function TWindowFormDeSign.SaveFormGridInfo(mCxGridView: TcxGridDBTableView; ADOCmd: TADOQuery; mdesignCode: string; mGridName: string; mGridType: integer): boolean;
var
  i: integer;
  mfieldName: string;
begin
  result := false;
  try
    /// ////////////////
    /// ////////////////
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from SD_FormGrid_Main A');
      sql.Add('where A.designCode=' + quotedStr(mdesignCode));
      open;
      if RecordCount <= 0 then
      begin
        append;
        fieldByName('designCode').value := mdesignCode;
        fieldByName('GridType').value := mGridType;
        fieldByName('gridName').value := TcxGrid(TcxGridLevel(mCxGridView.level).Control).name;
      end
      else
        edit;

      fieldByName('ProcedureParam').value := '';
      fieldByName('newProcedureName').value := '';
      post;

      close;

    end;
    // 子信息
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from SD_FormGrid_Design');
      sql.Add('where designCode=' + quotedStr(mdesignCode));
      sql.Add('and formId=' + intTostr(fFormId));
      sql.Add('and formName=' + quotedStr(fFormName));
      // sql.Add('and GridName='+QuotedStr(mgridName));
      open;

      for i := mCxGridView.ColumnCount - 1 downto 0 do
      begin
        mfieldName := TcxGridDBColumn(mCxGridView.Columns[i]).DataBinding.FieldName;

        if locate('fieldName', mfieldName, []) then
        begin
          continue;
        end
        else
        begin
          append;
          fieldByName('Id').value := GetMaxId();
          fieldByName('formId').value := fFormId;
          fieldByName('formName').value := fFormName;
          fieldByName('designCode').value := mdesignCode;
          fieldByName('GridName').value := mGridName;

          fieldByName('ColumnName').value := mCxGridView.Columns[i].name;
          fieldByName('Visible').value := mCxGridView.Columns[i].Visible;
          fieldByName('ColumnHide').value := mCxGridView.Columns[i].Hidden;
          fieldByName('oldCaption').value := mCxGridView.Columns[i].Caption;
          fieldByName('Valid').value := 1;
        end;

        fieldByName('GridIndex').value := mGridType;
        fieldByName('FieldName').value := trim(TcxGridDBColumn(mCxGridView.Columns[i]).DataBinding.FieldName);
        fieldByName('Caption').value := mCxGridView.Columns[i].Caption;
        fieldByName('ColumnType').value := trim(TcxGridDBColumn(mCxGridView.Columns[i]).DataBinding.ValueType);

        fieldByName('Width').value := mCxGridView.Columns[i].Width;
        fieldByName('IsOrder').value := 1;
        fieldByName('ColumnFilter').value := mCxGridView.Columns[i].Filtered;
        fieldByName('ColumnOrder').value := mCxGridView.Columns[i].Index;

        post;
      end;

      close;
    end;

    /// /////////////////////////////////////////////
    //
    for i := 0 to mCxGridView.dataController.DataSet.FieldCount - 1 do
    begin
      mfieldName := trim(mCxGridView.dataController.DataSet.Fields[i].FieldName);
      if mfieldName = '' then
        continue;
      with ADOCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from SD_FormGrid_Design');
        sql.Add('where designCode=' + quotedStr(mdesignCode));
        // sql.Add('and GridName='+QuotedStr(mgridName));
        sql.Add('and fieldName=' + quotedStr(mfieldName));
        open;
        if RecordCount <= 0 then
        begin
          append;
          fieldByName('Id').value := GetMaxId();
          fieldByName('formName').value := fFormName;
          fieldByName('formId').value := fFormId;
          fieldByName('GridName').value := mGridName;
          fieldByName('GridIndex').value := mGridType;
          fieldByName('designCode').value := mdesignCode;

          fieldByName('fieldName').value := mfieldName;

          fieldByName('ColumnType').value := mCxGridView.dataController.DataSet.Fields[i].DataType;
          fieldByName('Caption').value := '未启用';
          fieldByName('ColumnName').value := mCxGridView.name + mfieldName;
          fieldByName('Visible').value := false;
          fieldByName('AddFlag').value := true;

          fieldByName('Width').value := 60;
          fieldByName('ColumnOrder').value := 99;

          fieldByName('Valid').value := 0;
          post;
        end;
        close;
      end;
    end;
    result := true;
  except
    application.MessageBox('保存窗口表格信息时发生错误', '提示信息', 0);
  end;
end;

/// /////////////////////////////////////////////////
//
/// ///////////////////////////////////////////////////
/// ////////////////////////////////////////////////////////////////
// 初始化控件的默认数据 设置了取数SQL的控件
/// ////////////////////////////////////////////////////////////////
function TWindowFormDeSign.InitCtrlData(mCtrl: TControl; mSql: string; mdefault: string): boolean;
var
  idx: integer;
begin
  result := false;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add(mSql);
      open;
    end;

    if mCtrl is TcomBoBox then
    begin
      if ADOQueryTmp.RecordCount > 0 then
        TcomBoBox(mCtrl).Clear;
      while not ADOQueryTmp.Eof do
      begin
        TcomBoBox(mCtrl).Items.Add(trim(ADOQueryCmd.Fields[0].AsString));
        ADOQueryCmd.Next;
      end;

      if mdefault <> '' then
      begin
        idx := TcomBoBox(mCtrl).Items.IndexOf(mdefault);
        if idx >= 0 then
        begin
          TcomBoBox(mCtrl).ItemIndex := idx;
        end;
      end;
      exit;
    end;
    /// //////////////
    if mCtrl is TcxComboBox then
    begin
      if ADOQueryCmd.RecordCount > 0 then
      begin
        TcxComboBox(mCtrl).Clear;
        TcxComboBox(mCtrl).Properties.Items.Clear;
      end;
      while not ADOQueryCmd.Eof do
      begin
        TcxComboBox(mCtrl).Properties.Items.Add(trim(ADOQueryCmd.Fields[0].AsString));
        ADOQueryCmd.Next;
      end;
      if mdefault <> '' then
      begin
        idx := TcxComboBox(mCtrl).Properties.Items.IndexOf(mdefault);
        if idx >= 0 then
        begin
          TcxComboBox(mCtrl).ItemIndex := idx;
        end;
      end;
      exit;
    end;

    if mCtrl is Tdatetimepicker then
    begin
      Tdatetimepicker(mCtrl).Date := ADOQueryCmd.Fields[0].AsDateTime;
      exit;
    end;
    if mCtrl is TcxDateEdit then
    begin
      TcxDateEdit(mCtrl).Date := ADOQueryCmd.Fields[0].AsDateTime;
      exit;
    end;
    result := true;
  except
    application.MessageBox('初始化控件数据时发生错误!', '提示信息', 0);
  end;
end;

/// ///////////////////////////////////////////////////////////////////////////////////////
//
/// ///////////////////////////////////////////////////////////////////////////////////////
procedure TWindowFormDeSign.SetCxGridColumnBestWidth(cxGrid: TcxGridDBTableView);
var
  i: integer;
  mCaption: string;
  mMinWidth: integer;
begin
  try
    cxGrid.BeginUpdate;
    for i := 0 to cxGrid.ColumnCount - 1 do
    begin
      if cxGrid.ColumnCount < 6 then
      begin
        cxGrid.Columns[i].Width := 120;
        continue;
      end;
      mCaption := trim(cxGrid.Columns[i].Caption);
      if length(mCaption) <= 2 then
        mMinWidth := (length(mCaption) + 3) * 12
      else if (length(mCaption) > 2) and (length(mCaption) <= 4) then
        mMinWidth := (length(mCaption) + 2) * 12
      else if (length(mCaption) > 4) and (length(mCaption) <= 8) then
        mMinWidth := (length(mCaption) + 1) * 12
      else
        mMinWidth := (length(mCaption)) * 9;
      cxGrid.Columns[i].Width := mMinWidth;
    end;
  finally
    cxGrid.EndUpdate;
  end;
end;

/// /////////////////////////////////////////////////
/// ///////////////////////////////////////////////////
procedure TWindowFormDeSign.onDesignDblClick(Sender: TObject);
begin
  OpenContainerDesign(TwinControl(Sender));
end;

/// ///////////////////////////////////////////////////////////////
//
/// ////////////////////////////////////////////////////////////////

procedure TWindowFormDeSign.CustomDrawcxGridHeader(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridColumnHeaderViewInfo; var ADone: boolean);
var
  ARect: TRect;
  ATextToDraw: string;
  AIsVertical: boolean;
  FColorScheme: TColorSchemes;
  FCustomDrawingStyle: TCustomDrawingStyleArr;
begin
  try
    ATextToDraw := ' ' + AViewInfo.Text;
    ARect := AViewInfo.Bounds;
    AIsVertical := true;
    DrawGradient(ACanvas.Canvas, ARect, ColorScheme[1, 0], ColorScheme[1, 1], 55, AIsVertical);
    ARect.Left := ARect.Right - 9;
    ARect.Right := ARect.Right - 1;
    InflateRect(ARect, -1, -3);

    // 排序标识
    if AViewInfo.Column.SortOrder = soAscending then
      AViewInfo.LookAndFeelPainter.DrawSortingMark(ACanvas, ARect, false)
    else if AViewInfo.Column.SortOrder = soDescending then
      AViewInfo.LookAndFeelPainter.DrawSortingMark(ACanvas, ARect, true);

    // 过滤图标
    if fIsShowColumnfilter then
    begin
      if AViewInfo.Column.Options.Filtering then
      begin
        Sender.LookAndFeelPainter.DrawFilterDropDownButton(ACanvas, ARect, cxbsNormal, AViewInfo.Column.Filtered);
        OffsetRect(ARect, -16, 0);
      end;
    end;

    // 表格头分隔线
    ARect := AViewInfo.Bounds;
    ACanvas.DrawEdge(ARect, false, false, [btop, bright, bBottom]); //

    // 输出文字
    ARect := AViewInfo.TextAreaBounds;
    SetBkMode(ACanvas.Canvas.Handle, TRANSPARENT);
    ACanvas.DrawText(ATextToDraw, ARect, 0);

    ADone := true;
    // FCustomDrawingStyle[Integer(vtMaster), Integer(cdaColumnHeader)] <> cdsDefaultDrawing;

  except
  end;
end;

/// ////////////////////////////////////////
procedure TWindowFormDeSign.cxGridViewKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
  mGridDBTableView: TcxGridDBTableView;
begin
  if Key = vk_f12 then
  begin
    mGridDBTableView := TcxGridDBTableView(TcxGridSite(Sender).ViewInfo.GridView);
    OpenGridDesignWin(mGridDBTableView);
  end;

end;

/// ////////////////////////////////////////////////////////////////
// 打开表格设计窗口
// 0: mGridType 查询; 1:输入
/// /////////////////////////////////////////////////////////////////
procedure TWindowFormDeSign.OpenGridDesignWin(mCxGridView: TcxGridDBTableView);
type
  TMyFunc = function(App: Tapplication; FormH: hwnd; FormID: integer; UID: pchar; UName: pchar; dataBase: pchar; Title: pchar; Parameters1: pchar; Parameters2: pchar): hwnd; stdcall;
var
  Tf: TMyFunc;
  Tp: TFarProc;
  Th: Thandle;
  newh: hwnd;
  mGridType: integer;
  mdesignCode: string;
  mGridName: string;
  k: integer;
begin
  /// ////////////////////////////////////
  // 权限控制
  mdesignCode := fFormName + '_' + intTostr(fFormId) + '_' + mCxGridView.name;
  mGridName := TcxGrid(TcxGridLevel(mCxGridView.level).Control).name;
  if not IsHasDesignRight(ADOQueryCmd) then
    exit;

  /// ////////////////////////////////////////
  // 保存当前控件信息
  if not SaveFormGridInfo(mCxGridView, ADOQueryCmd, mdesignCode, mGridName, mCxGridView.Tag) then
  begin
    exit;
  end;

  if trim(fWhatDesign) = '' then
  begin
    if FileExists(ExtractFilePath(application.ExeName) + 'GridDesign.EXE') then
    begin
      WinExec(PAnsiChar(ExtractFilePath(application.ExeName) + 'GridDesign.exe ' + mdesignCode + ' ' + DName), SW_SHOWNORMAL);
    end;
  end
  else
  begin
    /// ////////////////////////////////////////
    //
    Th := LoadLibrary('RtDesign.dll');
    if Th > 0 then
    begin
      try
        Tp := GetProcAddress(Th, 'GetDllForm');
        if Tp <> nil then
        begin
          Tf := TMyFunc(Tp);
          newh := Tf(application, 0, 2, pchar(dcode), pchar(DName), pchar(ADOQueryCmd.ConnectionString), pchar(fFormCaption + mGridName + '表格设计'), pchar(mdesignCode), pchar(''));
        end
        else
        begin
          ShowMessage('调用RtDesign.dll时发生错误!');
        end;
      finally
        // FreeLibrary(Th);
      end;
    end
    else
    begin
      ShowMessage('找不到RtDesign.dll');
    end;
    CreateCxGridColumn(mCxGridView, ADOQueryCmd, mdesignCode, mGridName);
  end;
end;

/// ////////////////////////////////////////////////////////
// 函数功能：动态创建CxGrid列
/// ////////////////////////////////////////////////////////
procedure TWindowFormDeSign.CreateCxGridColumn(tv1: TcxGridDBTableView; ADOQueryTmp: TADOQuery; mdesignCode, mGridName: string; HasFooter: boolean = true);
var
  ADBColumn: TcxGridDBColumn;
  strField, strCaption: string;
  mColumnName: string;
  mCalcWidth: integer;
  mWidth: integer;
  mAvgWidth, mGridWidth: integer;
begin
  mWidth := 12;
  mGridWidth := TcxGrid(TcxGridLevel(tv1.level).Control).Width;
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from SD_ErpStyleParam');
    sql.Add('where StyleCode=''01''');
    open;
    if RecordCount > 0 then
    begin
      if fieldByName('headStyle').AsInteger = 0 then
      begin
        tv1.styles.Header := nil;
      end;
    end;
    close;
  end;

  try

    tv1.BeginUpdate;

    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select sum(width) as width ');
      sql.Add('from SD_FormGrid_Design A');
      sql.Add('where A.designCode=' + quotedStr(mdesignCode));
      sql.Add('and valid=1');
      open;

      if mGridWidth > fieldByName('width').AsInteger then
      begin
        if tv1.ColumnCount > 0 then
          mAvgWidth := (mGridWidth - fieldByName('width').AsInteger) div tv1.ColumnCount - 5;
      end;

      if mAvgWidth < 0 then
        mAvgWidth := 0;
    end;

    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.* ');
      sql.Add('from SD_FormGrid_Design A');
      sql.Add('where A.designCode=' + quotedStr(mdesignCode));
      // sql.Add('and A.formId='+intTostr(fFormId));
      // sql.Add('and A.formName='+QuotedStr(fFormName));
      sql.Add('order by A.columnOrder');
      open;
      if IsEmpty then
      begin
        // GetBeginColumnWidth(Tv1,mWidth);
        exit;
      end;

      while not Eof do
      begin
        ADBColumn := tv1.GetColumnByFieldName(trim(fieldByName('fieldName').AsString));
        if ADBColumn <> nil then
        begin
          // 作废不显示
          if not fieldByName('valid').AsBoolean then
          begin
            ADBColumn.Visible := false;
            ADBColumn.Hidden := true;
            Next;
            continue;
          end;
          ADBColumn.name := trim(tv1.name) + trim(fieldByName('fieldName').AsString);
          ADBColumn.Visible := fieldByName('Visible').AsBoolean;
          ADBColumn.Hidden := fieldByName('ColumnHide').AsBoolean;

          ADBColumn.Caption := trim(fieldByName('caption').AsString);
          // 宽度
          if tv1.optionsCustomize.columnfiltering and ADBColumn.Options.Filtering then
            mCalcWidth := length(trim(fieldByName('caption').AsString)) * mWidth + 10
          else
            mCalcWidth := length(trim(fieldByName('caption').AsString)) * mWidth;

          // if  fieldByName('width').AsInteger >mCalcWidth  then
          ADBColumn.Width := fieldByName('width').AsInteger + mAvgWidth;
          // else
          // ADBColumn.Width :=mCalcWidth ;
          Next;
          continue;
        end;
        // 判断列是否已存在
        mColumnName := trim(tv1.name) + trim(fieldByName('fieldName').AsString);
        if tv1.FindItemByName(mColumnName) <> nil then
        begin
          Next;
          continue;
        end;
        // 新的列
        ADBColumn := tv1.CreateColumn;
        ADBColumn.DataBinding.FieldName := trim(fieldByName('fieldName').AsString);

        ADBColumn.Caption := trim(fieldByName('Caption').AsString);

        // 宽度
        if tv1.optionsCustomize.columnfiltering and ADBColumn.Options.Filtering then
          mCalcWidth := length(trim(fieldByName('caption').AsString)) * mWidth + 10
        else
          mCalcWidth := length(trim(fieldByName('caption').AsString)) * mWidth;
        if fieldByName('width').AsInteger > mCalcWidth then
          ADBColumn.Width := fieldByName('width').AsInteger
        else
          ADBColumn.Width := mCalcWidth;

        ADBColumn.Visible := fieldByName('visible').AsBoolean;
        ADBColumn.Hidden := not fieldByName('visible').AsBoolean;
        ADBColumn.HeaderAlignmentHorz := taCenter;
        ADBColumn.MinWidth := 100;
        ADBColumn.name := mColumnName;

        ADBColumn.Options.Editing := not fieldByName('ReadOnly').AsBoolean;
        ADBColumn.Options.Focusing := not fieldByName('ReadOnly').AsBoolean;
        /// /////////////
        // 创建footer
        {
          if HasFooter then
          begin
          if fieldByName('FooterFlag').AsBoolean then
          begin
          CreateFooterCol(tv1,ADBColumn,ADBColumn.DataBinding.FieldName
          ,trim(fieldByName('FooterType').AsString));
          end;
          end;
        }

        Next;

      end;
      tv1.optionsView.Footer := HasFooter;

      close;
    end;
  finally
    tv1.EndUpdate;
  end;
end;

/// ///////////////////////////////////////////////////
//
/// ////////////////////////////////////////////////////
function TWindowFormDeSign.IsHasDesignRight(ADOTmp: TADOQuery): boolean;
begin
  result := false;
  try
    /// ////////////////////////////////////
    // 权限控制
    with ADOTmp do
    begin
      close;
      sql.Clear;
      Filtered := false;
      sql.Add('select count(userid) as cnt');
      sql.Add('from sy_user A');
      sql.Add('where userid=' + quotedStr(dcode));
      sql.Add('and canDesign=1');
      sql.Add('and valid=''Y''');
      open;

      if fieldByName('cnt').AsInteger > 0 then
      begin
        result := true;
      end;
      close;
    end;
  except
    application.MessageBox('获取操作权限时发生错误!', '警告信息', 0);
  end;
end;

/// //////////////////////////////////////
//
/// ///////////////////////////////////////
function TWindowFormDeSign.GetMaxId(): integer;
begin
  result := 0;
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      Filtered := false;
      sql.Add('select max(id) as id');
      sql.Add('from SD_FormGrid_Design A');
      open;
      result := fieldByName('id').AsInteger + 1;
    end;
  except
  end;
end;

/// ////////////////////////////////////////////////////////////////
// 显示控件设计窗口
// mWinType：0 输入编辑窗口，5:查询条件窗口
/// /////////////////////////////////////////////////////////////////
procedure TWindowFormDeSign.OpenContainerDesign(containerCtrl: TwinControl);
type
  TMyFunc = function(App: Tapplication; FormH: hwnd; FormID: integer; UID: pchar; UName: pchar; dataBase: pchar; Title: pchar; Parameters1: pchar; Parameters2: pchar): hwnd; stdcall;
var
  Tf: TMyFunc;
  Tp: TFarProc;
  Th: Thandle;
  newh: hwnd;
  mdesignCode: string;
begin
  mdesignCode := fFormName + '_' + intTostr(fFormId) + '_' + containerCtrl.name;
  if not IsHasDesignRight(ADOQueryTmp) then
    exit;

  /// ////////////////////////////////////////
  // 保存当前控件信息

  if not SaveContainerControInfo(containerCtrl, ADOQueryCmd, mdesignCode, containerCtrl.Tag) then
  begin
    exit;
  end;

  if trim(fWhatDesign) = '' then
  begin
    if FileExists(ExtractFilePath(application.ExeName) + 'WinFormDesign.EXE') then
    begin
      WinExec(PAnsiChar(ExtractFilePath(application.ExeName) + 'WinFormDesign.exe ' + mdesignCode + ' ' + DName), SW_SHOWNORMAL);
    end;
  end
  else
  begin

    /// ////////////////////////////////////////
    //
    Th := LoadLibrary('RtDesign.dll');
    if Th > 0 then
    begin
      try
        Tp := GetProcAddress(Th, 'GetDllForm');
        if Tp <> nil then
        begin
          Tf := TMyFunc(Tp);
          newh := Tf(application, 0, 1, pchar(dcode), pchar(DName), pchar(DConString), pchar(fFormCaption + '窗口设计'), pchar(mdesignCode), pchar(''));
        end
        else
        begin
          ShowMessage('调用RtDesign.dll时发生错误!');
        end;
      finally
        // FreeLibrary(Th);
      end;
    end
    else
    begin
      ShowMessage('找不到RtDesign.dll');
    end;
    InitContainerControls(mdesignCode, containerCtrl);
  end;

end;

/// /////////////////////////////////////////////////////////////////
// 根据界面设置信息初始化窗口控件信息
/// /////////////////////////////////////////////////////////////////
function TWindowFormDeSign.InitContainerControls(mdesignCode: string; containerCtrl: TwinControl): boolean;
var
  mParentHeight: integer;
  mContrlName: string;
  i: integer;
begin
  mParentHeight := 0;
  result := false;
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from SD_FormControl_Design');
      sql.Add('where designCode=' + quotedStr(mdesignCode));
      open;

      if RecordCount <= 0 then
      begin
        exit;
      end;
      mParentHeight := fieldByName('ParentHeight').AsInteger;
      containerCtrl.Height := mParentHeight;
    end;
    /// ///////////////////////////////
    // 控件
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from SD_FormControl_Design_sub');
      sql.Add('where designCode=' + quotedStr(mdesignCode));
      sql.Add('and isadd=0');

      open;
    end;
    /// ///////////////////////////
    for i := containerCtrl.ControlCount - 1 downto 0 do
    begin
      mContrlName := containerCtrl.Controls[i].name;
      if ADOQueryTmp.locate('controlName', mContrlName, []) then
      begin
        TwinControl(containerCtrl.Controls[i]).Left := ADOQueryTmp.fieldByName('controlLeft').AsInteger;
        TwinControl(containerCtrl.Controls[i]).Top := ADOQueryTmp.fieldByName('controlTop').AsInteger;
        TwinControl(containerCtrl.Controls[i]).Width := ADOQueryTmp.fieldByName('controlWidth').AsInteger;
        TwinControl(containerCtrl.Controls[i]).Height := ADOQueryTmp.fieldByName('controlHeight').AsInteger;
        TwinControl(containerCtrl.Controls[i]).Visible := ADOQueryTmp.fieldByName('visible').AsBoolean;
        TwinControl(containerCtrl.Controls[i]).TabOrder := ADOQueryTmp.fieldByName('TabOrder').AsInteger;

        if containerCtrl.Controls[i] is Tlabel then
        begin
          Tlabel(containerCtrl.Controls[i]).Caption := trim(ADOQueryTmp.fieldByName('Caption').AsString);
          continue;
        end
        else if containerCtrl.Controls[i] is TcxButton then
        begin
          TcxButton(containerCtrl.Controls[i]).Caption := trim(ADOQueryTmp.fieldByName('Caption').AsString);
          continue;
        end
        else if containerCtrl.Controls[i] is TButton then
        begin
          TButton(containerCtrl.Controls[i]).Caption := trim(ADOQueryTmp.fieldByName('Caption').AsString);
          continue;
        end
        else if containerCtrl.Controls[i] is TRadioButton then
        begin
          TRadioButton(containerCtrl.Controls[i]).Caption := trim(ADOQueryTmp.fieldByName('Caption').AsString);
          continue;
        end;

        if trim(ADOQueryTmp.fieldByName('InitSql').AsString) <> '' then
        begin
          if (containerCtrl.Controls[i] is TcomBoBox) or (containerCtrl.Controls[i] is TcxComboBox) then
          begin
            InitCtrlData(containerCtrl.Controls[i], trim(ADOQueryTmp.fieldByName('InitSql').AsString), trim(ADOQueryTmp.fieldByName('DefaultValue').AsString));
            continue;
          end;
          if (containerCtrl.Controls[i] is Tdatetimepicker) then
          begin
            InitCtrlData(containerCtrl.Controls[i], trim(ADOQueryTmp.fieldByName('InitSql').AsString), '');
            continue;
          end;
          if (containerCtrl.Controls[i] is TcxDateEdit) then
          begin
            InitCtrlData(containerCtrl.Controls[i], trim(ADOQueryTmp.fieldByName('InitSql').AsString), '');
            continue;
          end;

        end;

        /// /////////////////////////////
        if trim(ADOQueryTmp.fieldByName('DefaultValue').AsString) <> '' then
        begin
          if (containerCtrl.Controls[i] is TEdit) then
          begin
            TEdit(containerCtrl.Controls[i]).Text := getChgDefaultValue(trim(ADOQueryTmp.fieldByName('DefaultValue').AsString));
            continue;
          end;
          if (containerCtrl.Controls[i] is TcxTextEdit) then
          begin
            TcxTextEdit(containerCtrl.Controls[i]).Text := getChgDefaultValue(trim(ADOQueryTmp.fieldByName('DefaultValue').AsString));
            continue;
          end;
        end;

      end;
    end;
    result := true;
  except
    application.MessageBox('初始化窗口控件时发生错误!', '警告信息', 0);
  end;

end;

/// //////////////////////////////////////////////////////////////
// 设置toolbar按钮权限参数
/// //////////////////////////////////////////////////////////////
procedure TWindowFormDeSign.SetFormOperRight(mParent: TwinControl; mRightCaptions: string);
var
  i: integer;
  mCaption: string;
  mOperRight: string;
begin
  mOperRight := mRightCaptions;

  if mOperRight = '' then
    exit;

  try
    /// ////////////////////////////////////////////
    if mParent is TToolBar then
    begin
      for i := 0 to TToolBar(mParent).ButtonCount - 1 do
      begin
        mCaption := trim(TToolBar(mParent).Buttons[i].Caption);
        if (mCaption = '关闭') or (mCaption = '退出') or (mCaption = '放弃') then
          exit;
        /// //////////////////////////
        if trim(mOperRight) = '查询' then
        begin
          if pos(mCaption, '查询,刷新,过滤,高级过滤,高级查询') > 0 then // ,查看,详细
          begin
            TToolBar(mParent).Buttons[i].Visible := true;
          end
          else
          begin
            TToolBar(mParent).Buttons[i].Visible := false;
          end;
        end
        else
        begin
          if (pos(mCaption, mOperRight) > 0) or (pos(mCaption, '查询,刷新,过滤,高级过滤,高级查询') > 0) then // 查看,
          begin
            TToolBar(mParent).Buttons[i].Visible := true;
          end
          else
          begin
            TToolBar(mParent).Buttons[i].Visible := false;
          end;
        end;
      end;
      exit;
    end;
    /// ////////////////////////////////////////////////
    if trim(mOperRight) = '查询' then
      exit;
    with mParent do
    begin
      for i := 0 to ControlCount - 1 do
      begin
        mCaption := '';
        if (Controls[i] is TBitBtn) then
          mCaption := trim(TBitBtn(Controls[i]).Caption)
        else if (Controls[i] is TButton) then
          mCaption := trim(TButton(Controls[i]).Caption)
        else if (Controls[i] is TSpeedButton) then
          mCaption := trim(TSpeedButton(Controls[i]).Caption)
        else
          continue;
        if (Controls[i] is TBitBtn) or (Controls[i] is TButton) or (Controls[i] is TSpeedButton) then
        begin
          if (pos(mCaption, mOperRight) > 0) or (mCaption = '查询') or (mCaption = '过滤') or (mCaption = '高级过滤') then
          begin
            Controls[i].Visible := true;
          end
          else
            Controls[i].Visible := false;
        end;
      end;
    end;

  except
    application.MessageBox('初始化窗口按钮权限发生错误', '警告信息', 0);
  end;
end;

/// ////////////////////////////////////////////////////////////
//
/// ////////////////////////////////////////////////////////////
function TWindowFormDeSign.getChgDefaultValue(mValue: string): string;
begin
  if '$$USERNAME' = mValue then
  begin
    result := DName;
  end
  else
    result := mValue;
end;

/// ////////////////////////////////////////////////////
// 查询数据增强
/// /////////////////////////////////////////////////////
procedure TWindowFormDeSign.QueryDataPlus(ADOTmp: TADOQuery);
begin

end;

/////////////////////////////////////////////////////////
///
//////////////////////////////////////////////////////////

procedure TWindowFormDeSign.cxGridViewSelectionChanged(Sender: TcxCustomGridTableView);
var
  i, J, idx: integer;
  FocusedRowIndex: integer;
begin
  if Sender.Tag = 888 then
    exit;
  try
    Sender.BeginUpdate();
    TcxGridDbTableView(Sender).BeginUpdate();

    FocusedRowIndex := TcxGridDbTableView(Sender).Controller.FocusedRowIndex;
    Sender.DataController.GotoFirst;
    if TcxGridDbTableView(Sender).DataController.DataSet.FieldByName('ssel') = nil then
      exit;
    if TcxGridDbTableView(Sender).GetColumnByFieldName('ssel') = nil then
      exit;
    idx := TcxGridDbTableView(Sender).GetColumnByFieldName('ssel').Index;
    for i := 0 to Sender.DataController.RecordCount - 1 do
    begin
      if TcxGridDbTableView(Sender).DataController.GetItemByFieldName('ssel').EditValue = true then
      begin
        TcxGridDbTableView(Sender).DataController.Edit;
        TcxGridDbTableView(Sender).DataController.GetItemByFieldName('ssel').EditValue := false;
        TcxGridDbTableView(Sender).DataController.Post();
      end;
      Sender.DataController.GotoNext;
    end;

    for i := 0 to Sender.DataController.GetSelectedCount - 1 do
    begin
      //J := TcxGridDbTableView( Sender).DataController.GetSelectedRowIndex(i);
      //选择中行的第列的值
      // Sender.DataController.Edit;
      // Sender.DataController.Values[J, idx] := true;
      //  Sender.Controller.EditingController.ShowEdit;
      // Sender.DataController.Edit;
      // TcxGridDbTableView( Sender).DataController.Values[J, idx] := true;
      //  TcxGridDbTableView( Sender).DataController.PostEditingData;
      //   Sender.DataController.SetValue(J, idx,true);   ;
      //   Sender.DataController.Post(false);
      // Sender.DataController.e
      // Sender.DataController.Post(true);
      //  Sender.DataController.PostEditingData;


      TcxGridDbTableView(Sender).Controller.FocusedRow := TcxGridDbTableView(Sender).Controller.SelectedRows[i];
      if TcxGridDbTableView(Sender).DataController.GetItemByFieldName('ssel').EditValue = false then
      begin
        TcxGridDbTableView(Sender).DataController.Edit;
        TcxGridDbTableView(Sender).DataController.GetItemByFieldName('ssel').EditValue := true;
        TcxGridDbTableView(Sender).DataController.Post();

      end;

    end;

  finally
    TcxGridDbTableView(Sender).Controller.FocusedRowIndex := FocusedRowIndex;
    TcxGridDbTableView(Sender).EndUpdate;
    Sender.EndUpdate;

  end;

end;
///////////////////////////////////////////////////////////////
///
///

procedure TWindowFormDeSign.cxGridViewCustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  AIndicatorViewInfo: TcxGridIndicatorRowItemViewInfo;
  ATextRect: TRect;
 // AStyle: TcxStyle;
  aCV: TcxCanvas;
begin
  if not (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
    Exit;
  aCV := ACanvas;
  ATextRect := AViewInfo.ContentBounds;
  AIndicatorViewInfo := AViewInfo as TcxGridIndicatorRowItemViewInfo;
  InflateRect(ATextRect, -2, -1);

  if AIndicatorViewInfo.GridRecord.Selected then  //这个if段是为了在行号处把把选中的行号跟别的区分开，可不用
  begin
    aCV.Font.Style := ACanvas.Font.Style + [fsBold];
    aCV.Font.Color := clRed;
  end
  else
  begin
    aCV.Font.Style := ACanvas.Font.Style - [fsBold];
    aCV.Font.Color := clblack;
  end;

  Sender.LookAndFeelPainter.DrawHeader(ACanvas, AViewInfo.ContentBounds, ATextRect, [], cxBordersAll, cxbsNormal, taCenter, vaCenter, False, False, IntToStr(AIndicatorViewInfo.GridRecord.Index + 1),
//    AStyle.Font, AStyle.TextColor, AStyle.Color);
    aCV.Font, aCV.font.Color, aCV.Brush.color);
  ADone := True;

end;

procedure TWindowFormDeSign.ReadINIFileSetInfo(mfontSize: integer);
var
  programIni: Tinifile;  //配置文件名
  FileName: string;
begin
  FileName := ExtractFilePath(Paramstr(0)) + 'SYSTEMSET.INI';
  if not FileExists(FileName) then
    exit;

  try
    programIni := Tinifile.create(FileName);
    fFontSize := programIni.ReadInteger('窗口设置', '字体大小', mfontSize);
  finally
    programIni.Free;
  end;

end;

end.

