unit U_WindowFormdesign;

interface

uses
  Windows, SysUtils, Messages, Classes, Controls, StdCtrls, ComCtrls, Forms,
  ExtCtrls, ComObj, DXCORE, ActiveX, cxMemo, cxButtons, Graphics, IniFiles, DBClient,
  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,dxLayoutControl,
  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;
    client1:TClientDataSet ;

 //   procedure SetCxGridColumnBestWidth(cxGrid: TcxGridDBTableView);
  //  function SaveFormGridInfo(mCxGridView: TcxGridDBTableView; ADOCmd: TADOQuery; mdesignCode: string; mGridName: string; mGridType: integer): boolean;
    procedure SetFormOperRight(mParent: TwinControl; mRightCaptions: string);
    function GetMaxId(): integer;
    procedure cxGridViewKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

    procedure cxGridViewSelectionChanged(Sender: TcxCustomGridTableView);
    function InitCtrlData(mCtrl: TControl; mSql: string; mdefault: string): boolean;
    procedure OpenGridDesignWin(mCxGridView: TcxGridDBTableView);
    function getChgDefaultValue(mValue: string): string;
    procedure dxLayoutControl1DblClick(Sender: TObject);
    procedure setGridClick(Sender: TObject);
    function getColumnRelation(tv1:TcxGridDBTableView):TclientDataSet;
    procedure cxGridViewCustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
  public
    fProcedureName:string;
    fFormRightButtons:string;
    function FormStyleInit(mForm: Tform; mFormId: integer; ADOTmp: TADOQuery; ADOQCmd: TADOQuery; mStyleCode: string = '01'; mRightCaptions: string = ''): boolean;
    function FormStyleInit1(mForm: Tform; mFormId: integer; ADOTmp: TADOQuery; ADOQCmd: TADOQuery; mStyleCode: string = '01'; mRightCaptions: string = ''): boolean;
    function FormStyleInit10(mForm: Tform; mFormId: integer; ADOTmp: TADOQuery; ADOQCmd: TADOQuery; mStyleCode: string = '01'; mRightCaptions: string = ''): boolean;
    procedure QueryDataPlus(ADOTmp: TADOQuery);
    procedure OpenGridDesignWin10(mdesignCode:string;mGridName:string;mCxGridView: TcxGridDBTableView);
    procedure initUserFormButtons(toolBar1:TToolBar;toolBar2:TToolBar=nil);
    function geUsertFormButtons(ADOQueryTmp:TADOQuery;mFormId:integer;mFormCaption:string;moduleId:string;moduleSubId:string):string;
  end;

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

implementation

uses
  U_DataLink, U_globalVar,U_cxGridCustomSet,U_FormLayOutDesign;

/// //////////////////////////////////////////////////////
// 窗口样式,其它界面信息设置；
/// //////////////////////////////////////////////////////
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;
  setGridButton: TToolButton;
begin
  if gIsYsUse then  exit;
  ADOQueryCmd := ADOQCmd;
  ADOQueryTmp := ADOTmp;
  fDllName := gDllFileName;
  fFormName := mForm.name;
  fFormId := mFormId;
  fFormRightButtons:='';
   //gIsCanDesign :=IsHasDesignRight(ADOTmp,PWideChar(dCode)) ;
//
  //if trim(mStyleCode) = '' then
  //  mStyleCode := '01';

    if gFontSize>=9 then
    begin
      fFontSize:= gFontSize ;
    end
    else
      fFontSize:=9;
//    else
//    begin
//      fFontSize:= getSystemFontSize(ADOTmp,PWideChar(mStyleCode));
//      if fFontSize < 9 then
//       fFontSize := 12;
//      gFontSize:=fFontSize;
//    end;

  mForm.Font.Size := fFontSize;
  // 窗口背景色

  try
  for i := 0 to mForm.ComponentCount - 1 do
  begin
    if  not(mForm.Components[i] is  Twincontrol) then
    begin
      continue;
    end;
    if  ((mForm.Components[i] is TcxTextEdit and TcxTextEdit(mForm.Components[i]).ParentFont)
        or (mForm.Components[i] is TcxComboBox and TcxComboBox(mForm.Components[i]).ParentFont)
        or (mForm.Components[i] is TcxdateEdit) and TcxdateEdit(mForm.Components[i]).ParentFont ) then
      Continue;

     if mForm.Components[i] is TcxTextEdit  then
     begin
       if (mForm.Components[i].Tag<999) and  not TcxTextEdit(mForm.Components[i]).ParentFont then
         TcxTextEdit(mForm.Components[i]).ParentFont:=True;
        continue;
     end;
     if  (mForm.Components[i] is TcxComboBox)  then
     begin
       if (mForm.Components[i].Tag<999) and  not TcxComboBox(mForm.Components[i]).ParentFont then
        TcxComboBox(mForm.Components[i]).ParentFont:=True;
        continue;
     end;
      if mForm.Components[i] is TcxButtonEdit then
     begin
       if (mForm.Components[i].Tag<999) and not TcxButtonEdit(mForm.Components[i]).ParentFont then
        TcxButtonEdit(mForm.Components[i]).ParentFont:=True;
        continue;
     end;
       if mForm.Components[i] is TcxdateEdit then
      begin
        if mForm.Components[i].Tag<999 then
         TcxdateEdit(mForm.Components[i]).ParentFont:=True;
        continue;
      end;
       if mForm.Components[i] is TcxCurrencyEdit then
      begin
        if mForm.Components[i].Tag<999 then
         TcxCurrencyEdit(mForm.Components[i]).ParentFont:=True;
        continue;
      end;
//      if mForm.Components[i] is TfrmFrameDateSel then
//      begin
//        TfrmFrameDateSel(mForm.Components[i]).Font.Size:= fFontSize;
//        TfrmFrameDateSel(mForm.Components[i]).cbbType.ParentFont:= true;
//        TfrmFrameDateSel(mForm.Components[i]).BegDate.ParentFont:=true;
//        TfrmFrameDateSel(mForm.Components[i]).endDate.ParentFont:=true;
//       // TfrmFrameDateSel(mForm.Components[i]).TimeType.ParentFont:=true;
//        continue;
//      end;
     if mForm.Components[i] is TcxMemo then
     begin
       if mForm.Components[i].Tag<999 then
          TcxMemo(mForm.Components[i]).ParentFont:=True;
        continue;
     end;
    // Tcxgroup
    if mForm.Components[i] is TcxgroupBox then
    begin
       if mForm.Components[i].Tag<999 then
         TcxgroupBox(mForm.Components[i]).ParentFont:=True;
     // if mIsCustomBackColor then
     //   TcxgroupBox(mForm.Components[i]).Color := clPanelBack;
     // TcxgroupBox(mForm.Components[i]).Ctl3D := mCtl3D;
     // mdesignCode := fFormName + '_' + intTostr(fFormId) + '_' + TcxgroupBox(mForm.Components[i]).name;

      continue;
    end;

    if mForm.Components[i] is TToolBar then
    begin
      if mForm.Components[i].Tag>=999 then exit;
     if mForm.Components[i].Tag<999 then
       TToolBar(mForm.Components[i]).ParentFont:=True;
      TToolBar(mForm.Components[i]).AutoSize := true;
      TToolBar(mForm.Components[i]).Flat := true;
      //
      if gIsCanDesign then
      begin
        if mForm.Components[i].FindComponent('TgridLiSet')=nil then
        begin
          TToolBar(mForm.Components[i]).Wrapable:=false;
           setGridButton := TToolButton.Create(mForm); //
           setGridButton.Left := TToolBar(mForm.Components[i]).ButtonWidth * TToolBar(mForm.Components[i]).ButtonCount;
           setGridButton.Parent := TToolBar(mForm.Components[i]);
           setGridButton.ImageIndex := 16; //）
           setGridButton.Caption:='表格列设置';
           setGridButton.Style := tbsButton;
           setGridButton.AutoSize:=true;

           setGridButton.OnClick := setGridClick; //
         end;
       end;
      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;
      if mRightCaptions<>'' then
        SetFormOperRight(TToolBar(mForm.Components[i]), PWideChar(mRightCaptions));
      continue;
    end;
    if mForm.Components[i] is TcxTabControl then
    begin
     if mForm.Components[i].Tag<999 then
      TdxLayoutControl(mForm.Components[i]).ParentFont:=true;
      continue;
    end;
    if mForm.Components[i] is TcomBoBox then
    begin
     if mForm.Components[i].Tag<999 then
      TcomBoBox(mForm.Components[i]).ParentFont:=true;
      continue;
    end;
     if mForm.Components[i] is TdxCustomLayoutControl then
    begin
      if mForm.Components[i].Tag<999 then
       TdxLayoutControl(mForm.Components[i]).ParentFont:=true;
        if mForm.Components[i].Tag<>1000 then
        begin
         TdxLayoutControl(mForm.Components[i]).OnDblClick :=  dxLayoutControl1DblClick  ;
         readLayOut(TdxLayoutControl(mForm.Components[i]),ADOTmp,PWideChar( fDllFileName+'|'+mForm.name+'|'+TdxLayoutControl(mForm.Components[i]).Name+'.ini'));
        end;
      continue;
    end;
    // Tpanel
    if mForm.Components[i] is Tpanel then
    begin
      if mForm.Components[i].Tag<999 then
        Tpanel(mForm.Components[i]).ParentFont:=True;

      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;

      continue;
    end;
    // TScrollBox
    if mForm.Components[i] is TScrollBox then
    begin
       if mForm.Components[i].Tag<999 then
       TScrollBox(mForm.Components[i]).ParentFont:=true;

      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;

      continue;
    end;
    // Tcxgroup
    if mForm.Components[i] is TgroupBox then
    begin
      if mForm.Components[i].Tag<999 then
        TgroupBox(mForm.Components[i]).ParentFont:=true;
      if mIsCustomBackColor then
        TgroupBox(mForm.Components[i]).Color := clPanelBack;
      TgroupBox(mForm.Components[i]).Ctl3D := mCtl3D;

      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;

    // TcxPageControl
    if mForm.Components[i] is TcxPageControl then
    begin
      if mForm.Components[i].Tag<999 then
        TcxPageControl(mForm.Components[i]).ParentFont:=True;
      if mIsCustomBackColor then
        TcxPageControl(mForm.Components[i]).Color := clPanelBack;

      continue;
    end;

    if mForm.Components[i] is Tcxtimeedit then
    begin
      if mForm.Components[i].Tag<999 then
        Tcxtimeedit(mForm.Components[i]).ParentFont:=true;
      Tcxtimeedit(mForm.Components[i]).Style.BorderStyle := ebsFlat;
      continue;
    end;
    // TcxGrid  and gIsCustomDrawGridHeader
    if mForm.Components[i] is TcxGrid then
    begin
      if mForm.Components[i].Tag>=999 then   Continue;
       TcxGrid(mForm.Components[i]).ParentFont:=true;

      //TcxGrid(mForm.Components[i]).LookAndFeel.ScrollbarMode := sbmClassic;

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


   //   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 := fDllFileName +'|'+mForm.name+ '|' + intTostr(fFormId) + '|' + TcxGrid(mForm.Components[i]).name;


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

      continue;
    end;

  end;
  except
     showMessage('窗口初始化异常!');
  end;
end;
/// //////////////////////////////////////////////////////
// 窗口样式,其它界面信息设置；
/// //////////////////////////////////////////////////////
function TWindowFormDeSign.FormStyleInit1(mForm: Tform; mFormId: integer; ADOTmp: TADOQuery; ADOQCmd: TADOQuery; mStyleCode: string = '01'; mRightCaptions: string = ''): boolean;
var
  i: integer;
  mCtl3D, mToolBarList: boolean;
  mIsCustomBackColor: boolean;
//  mIsDrawGridHeader: boolean;
  mdesignCode: string;
  setGridButton: TToolButton;
  comp: TComponent;
  view: TcxGridDBTableView;
  grid: TcxGrid;
  toolBar: TToolBar;
  layoutControl: TdxLayoutControl;
begin
  if gIsYsUse and (fFormRightButtons='') then  exit;

  ADOQueryCmd := ADOQCmd;
  ADOQueryTmp := ADOTmp;
  fDllName := gDllFileName;
  fFormName := mForm.name;
  fFormId := mFormId;

  try

  for i := 0 to mForm.ComponentCount - 1 do
  begin
    comp := mForm.Components[i];
    if  not ( comp is TcxGrid) and not( comp is TToolBar) and  not( comp is TdxCustomLayoutControl) then Continue;

    if comp is TcxGrid then
    begin
       grid := TcxGrid(comp);
      if grid.Tag>=999 then   Continue;
      grid.BeginUpdate();
      // TcxGrid(mForm.Components[i]).ParentFont:=true;
      view := TcxGridDBTableView(grid.Views[0]);
      //自动响应过滤
      view.dataController.filter.AutoDataSetFilter := true;
      view.optionsCustomize.columnfiltering := true;
      view.OnSelectionChanged := cxGridViewSelectionChanged;
      view.OnCustomDrawIndicatorCell := cxGridViewCustomDrawIndicatorCell;
      view.optionsCustomize.columnfiltering := true;
      mdesignCode := fDllFileName +'|'+mForm.name+ '|' + intTostr(fFormId) + '|' + grid.name;
      grid.EndUpdate ;
      CreateCxGridColumn( view,ADOQueryTmp,PWideChar(mdesignCode));

      //continue;
    end
    //
    else if mForm.Components[i] is TToolBar then
    begin
      toolBar := TToolBar(comp);
      if toolBar.Tag>=999 then exit;
      if toolBar.Tag<999 then
        toolBar.ParentFont:=True;
      toolBar.AutoSize := true;
      toolBar.Flat := true;
      //
      if gIsCanDesign then
      begin
        if (mForm.Components[i].FindComponent('TgridLiSet')=nil) or (mForm.Components[i].FindComponent('TgridSet')=nil) then
        begin
          toolBar.Wrapable:=false;
           setGridButton := TToolButton.Create(mForm); //
           setGridButton.Left := toolBar.ButtonWidth * toolBar.ButtonCount;
           setGridButton.Parent := toolBar;
           setGridButton.ImageIndex := 16; //）
           setGridButton.Caption:='表格列设置';
         //  setGridButton.Name:='TgridLiSet';
           setGridButton.Style := tbsButton;
           setGridButton.AutoSize:=true;

           setGridButton.OnClick := setGridClick; //
         end;
       end;
      if mIsCustomBackColor then
        toolBar.Color := clToolBarBack;
      if toolBar.Tag = 0 then
      begin
        if ADOTmp.IsEmpty then
          toolBar.List := false
        else
          toolBar.List := mToolBarList;
      end;
      //

      if fFormRightButtons<>'' then
        initUserFormButtons(toolBar);
      //continue;
    end
    else if mForm.Components[i] is TdxLayoutControl then
    begin
      layoutControl := TdxLayoutControl(comp);
      if layoutControl.Tag<999 then
       layoutControl.ParentFont:=true;
        if layoutControl.Tag<>1000 then
        begin
         layoutControl.OnDblClick :=  dxLayoutControl1DblClick  ;
         readLayOut(layoutControl,ADOTmp,PWideChar( fDllFileName+'|'+mForm.name+'|'+layoutControl.Name+'.ini'));
        end;
      continue;
    end;
  end;

  except
     showMessage('窗口初始化异常!');
  end;
end;

/// //////////////////////////////////////////////////////
// 窗口样式,其它界面信息设置；
/// //////////////////////////////////////////////////////
function TWindowFormDeSign.FormStyleInit10(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;
  setGridButton: TToolButton;
begin
  ADOQueryCmd := ADOQCmd;
  ADOQueryTmp := ADOTmp;
  fDllName := gDllFileName;
  fFormName := mForm.name;
  fFormId := mFormId;
  exit;


   if gFontSize>=9 then
   begin
      fFontSize:= gFontSize ;
   end
   else
   begin
      fFontSize:= 9;//

   end;

  mForm.Font.Size := fFontSize;

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.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;

/////////////////////////////////////////////
procedure TWindowFormDeSign.dxLayoutControl1DblClick(Sender: TObject);
begin
  layoutDesign(TdxLayoutControl(Sender),ADOQueryCmd,PWideChar(dcode));
end;

/// ////////////////////////////////////////////////////////////////
// 打开表格设计窗口
// 0: mGridType 查询; 1:输入
/// /////////////////////////////////////////////////////////////////
procedure TWindowFormDeSign.OpenGridDesignWin(mCxGridView: TcxGridDBTableView);
var
  mGridType: integer;
  mdesignCode: string;
  mGridName: string;
  k: integer;
  client1:TClientDataSet;
begin
  /// ////////////////////////////////////
  // 权限控制

  mGridName := TcxGrid(TcxGridLevel(mCxGridView.level).Control).name;
  mdesignCode :=fDllFileName+'|'+ fFormName + '|' + intTostr(fFormId) + '|' + mGridName;

  if not gIsCanDesign  then
    exit;

   ShowCxGridDesign(application, mCxGridView, getColumnRelation(mCxGridView),AdOQueryCmd,fFormName ,mGridName,fDllFileName,fformId,0,fProcedureName);
   CreateCxGridColumn( mCxGridView,ADOQueryTmp,PWideChar(mdesignCode));
end;

/// ////////////////////////////////////////////////////////////////
// 打开表格设计窗口 新的版本
/// /////////////////////////////////////////////////////////////////
procedure TWindowFormDeSign.OpenGridDesignWin10(mdesignCode:string;mGridName:string;mCxGridView: TcxGridDBTableView);
begin
   ShowCxGridDesign(application, mCxGridView, getColumnRelation(mCxGridView),AdOQueryCmd,fFormName ,mGridName,fDllFileName,fformId,0,fProcedureName);
   CreateCxGridColumn( mCxGridView,ADOQueryTmp,PWideChar(mdesignCode));
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;




/// //////////////////////////////////////////////////////////////
// 设置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) = '查询') or (trim(mOperRight) = '查询模块') then
        begin
          if pos(mCaption, '查询,刷新,过滤,高级过滤,高级查询') > 0 then // ,查看,详细
          begin
            TToolBar(mParent).Buttons[i].Enabled:=true;
            TToolBar(mParent).Buttons[i].Visible := true;
          end
          else
          begin
            TToolBar(mParent).Buttons[i].Enabled:=false;
            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].Enabled:=true;
            TToolBar(mParent).Buttons[i].Visible := true;
          end
          else
          begin
            TToolBar(mParent).Buttons[i].Enabled:=false;
            TToolBar(mParent).Buttons[i].Visible := false;
          end;
        end;
      end;
      exit;
    end;
    /// ////////////////////////////////////////////////
    if (trim(mOperRight) = '查询') or (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].Enabled := true;
            Controls[i].Visible := true;
          end
          else
          begin
            Controls[i].Enabled := false;
            Controls[i].Visible := false;
          end;
        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
      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;
  Sender.BeginUpdate() ;

  aCV := ACanvas;
  AViewInfo.GetBestFitWidth  ;
  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),
    aCV.Font, aCV.font.Color, aCV.Brush.color);
  Sender.EndUpdate;
  ADone := True;

end;




/////////////////////////////////////////////////////////////////
///
function TWindowFormDeSign.getColumnRelation(tv1:TcxGridDBTableView):TclientDataSet;
var
  i:Integer;
begin
 if client1=nil then
 begin
    client1 := TClientDataSet.Create(nil);
    client1.FieldDefs.Add('fieldName', ftString, 100);
    client1.FieldDefs.Add('caption', ftString,100);
    client1.FieldDefs.Add('ValueType', ftString,100);

    client1.CreateDataSet;
 end
 else
   client1.EmptyDataSet;

   for i := 0 to tv1.ColumnCount - 1 do
   begin
      client1.Append;
      client1.FieldByName('fieldName').Value:=tv1.Columns[i].DataBinding.FieldName;
      client1.FieldByName('caption').Value:=tv1.Columns[i].Caption;
      client1.FieldByName('ValueType').Value:=tv1.Columns[i].DataBinding.ValueType;
      client1.Post;
    end;
    result:=client1;
end;

/////////////////////////////////////////////
procedure TWindowFormDeSign.setGridClick(Sender: TObject);
var
  i:integer;
  view:TcxGridDBTableView;
begin
  view:=nil;
  with TToolButton( Sender ).Parent.Parent do
  begin
    for i:=0 to ComponentCount-1 do
    begin
      if Components[i] is TcxGrid then
      begin
        if TcxGrid(Components[i]).IsFocused then
        begin
           view:= TcxGridDBTableView(TcxGrid(Components[i]).Views[0]);
        end;
     end;
    end;
  end;
  //
   if view<>nil then
   begin
     OpenGridDesignWin(view);
   end
   else
   begin
     showMessage('请先选择要设计的表格!');
   end;
end;

//////////////////////////////////////////////////////////////////
/// 设置窗口按钮
procedure TWindowFormDeSign.initUserFormButtons(toolBar1:TToolBar;toolBar2:TToolBar=nil);
begin
   SetFormOperRight(toolBar1, PWideChar(fFormRightButtons));
   if toolBar2<>nil then
    SetFormOperRight(toolBar2, PWideChar(fFormRightButtons));
end;
///////////////////////////////////////////////////////////////////
///
function TWindowFormDeSign.geUsertFormButtons(ADOQueryTmp:TADOQuery;mFormId:integer;mFormCaption:string;moduleId:string;moduleSubId:string):string;
begin
    try
      result:='';
      fFormRightButtons :='';
      with ADOQueryTmp do
      begin
        close;
        sql.Clear;
        sql.Add('select * from SY_ModuleSub');
        if moduleId='' then
        begin
          sql.Add('where formFile='+quotedstr(fDllFileName));
          sql.Add('and formName='+quotedstr(mFormCaption));
          sql.Add('and formId='+intTostr(mFormId));
        end
        else
        begin
          sql.Add('where moduleId='+quotedstr(moduleId));
          sql.Add('and moduleSubId='+quotedstr(moduleSubId));
        end;
        open;
        if isEmpty then
        begin
         // application.MessageBox('未找到对应的模块!','警告信息',0);
          exit;
        end;
        if ADOQueryTmp.FindField('formButtons')=nil then exit;

        moduleId:=trim(fieldByName('moduleId').AsString) ;
        moduleSubId:=trim(fieldByName('moduleSubId').AsString) ;
        if trim(fieldByName('formButtons').AsString)='' then
        begin
          exit;
        end;
      end;
      with ADOQueryTmp do
      begin
        close;
        sql.Clear;
        sql.Add('exec p_get_user_moduleButtons');
        sql.Add('@moduleId='+quotedstr(moduleId));
        sql.Add(',@moduleSubId='+quotedstr(moduleSubId));
        sql.Add(',@UserId='+quotedstr(dCode));
        open;
       fFormRightButtons :=   trim(fieldByName('rightButtons').AsString) ;
       result:= fFormRightButtons;
      end;
    finally

    end;
end;
end.

