[Delphi]在DBGrid 畫上 可視元件(這裡用Button來實作)

[Delphi]在DBGrid 畫上 可視元件(這裡用Button來實作)

效果image

 

[進行實作]

   1:  //重新繼承TInfoDBGrid
   2:    TInfoDBGrid=class(InfoCtrls.TInfoDBGrid)
   3:      Private
   4:        FRowHeight : Integer ;
   5:      Protected
   6:        Procedure SetRowHeight(Value:Integer);
   7:      Public
   8:        // The inherited method is declared as protected.
   9:        // Used Reintroduce to hide compiler warnings.
  10:        Function CellRect(ACol,ARow : Longint):TRect; Reintroduce;
  11:        // Expose Row and Col properties
  12:        Property Row ;
  13:        Property Col ;
  14:        //讓dbgrid支援滑鼠滑輪
  15:        function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean; override;
  16:      Published
  17:        Property RowHeight : Integer Read FRowHeight Write SetRowHeight ;
  18:    End;
  19:  //實作內容
  20:  { TInfoDBGrid 重新繼承}
  21:  function TInfoDBGrid.CellRect(ACol, ARow: Integer): TRect;
  22:  begin
  23:    Result := Inherited CellRect(ACol, ARow);
  24:  end;
  25:   
  26:  procedure TInfoDBGrid.SetRowHeight(Value: Integer);
  27:  begin
  28:     If FRowHeight <> Value Then Begin
  29:        FRowHeight := Value ;
  30:        DefaultRowHeight := FRowHeight ;
  31:        // Force Grid to update the RowCount.
  32:        // The method I need to call is
  33:        // UpdateRowCount, but it's declared
  34:        // as private in the parent.  This
  35:        // calls it by making the grid think it has
  36:        // been resized.
  37:        If Self.DataLink.Active Then Begin
  38:           Perform(WM_SIZE,0,0);
  39:       End;
  40:     End;
  41:  end;
  42:   
  43:  function TInfoDBGrid.DoMouseWheel(Shift: TShiftState; WheelDelta: Integer;MousePos: TPoint): Boolean;
  44:  begin
  45:    if WheelDelta > 0 then
  46:      DataSource.DataSet.Prior;
  47:    if wheelDelta < 0 then
  48:      DataSource.DataSet.Next;
  49:  end;

1.image

2.為grdView新增兩個虛擬欄位,用來畫上BUTTONimage image

3.在grdView的OnDrawColumnCell編寫事件image

   1:  procedure TfmXFlowFillForm.grdViewDrawColumnCell(Sender: TObject;
   2:    const Rect: TRect; DataCol: Integer; Column: TColumn;
   3:    State: TGridDrawState);
   4:    //依欄位名,回傳Column-Index
   5:    function ColIndexOf(const DBGrid : TInfoDBGrid; const fieldName : string):Integer ;
   6:    var
   7:       iColumn : integer;
   8:       idx : integer;
   9:    begin
  10:       icolumn := 0;
  11:       for idx:= 0 to DBGrid.Columns.Count -1 do
  12:       begin
  13:         if DBGrid.Columns[idx].FieldName = fieldName then
  14:         begin
  15:           iColumn := idx;
  16:           Break;
  17:         end;
  18:       end;
  19:       Result:= iColumn;
  20:    end;
  21:    //功能同 dbgrid.selectedIndex
  22:      
  23:    Var DataRect : TRect ;
  24:  Begin
  25:    //資料為空時,Column、button進行隱藏※要自己找時機點 “顯示button”
  26:     if grdView.DataSource.DataSet.IsEmpty then begin
  27:       btnView.Visible:= False;
  28:       btnWithDraw.Visible:= btnView.Visible;
  29:       grdView.Columns[ColIndexOf(grdView,'FnView')].Visible:= btnView.Visible;
  30:       grdView.Columns[ColIndexOf(grdView,'FnWithDraw')].Visible:= btnView.Visible;
  31:     end;
  32:    
  33:     //取出grdView-Column"FnView"的座標
  34:     With grdView Do Begin
  35:        DataRect := CellRect(ColIndexOf(grdView,'FnView')+1,Row);
  36:     End;
  37:     // 指定button的parent(grdView)
  38:     If btnView.Parent <> grdView Then
  39:        btnView.Parent := grdView ;
  40:     // 設定button的座標
  41:     with btnView do begin
  42:       Top := DataRect.Top ;
  43:       Left := (DataRect.Right - Width) ;
  44:       Height := (DataRect.Bottom-DataRect.Top);
  45:       Width := (DataRect.Right-DataRect.Left);
  46:     end;
  47:   
  48:    
  49:  //取出grdView-Column"FnWithDraw"的座標
  50:     With grdView Do Begin
  51:        DataRect := CellRect(ColIndexOf(grdView,'FnWithDraw')+1,Row);
  52:     End;
  53:     // 指定button的parent(grdView)
  54:     If btnWithDraw.Parent <> grdView Then
  55:        btnWithDraw.Parent := grdView ;
  56:     // 設定button的座標
  57:     with btnWithDraw do begin
  58:       Top := DataRect.Top ;
  59:       Left := (DataRect.Right - Width) ;
  60:       Height := (DataRect.Bottom-DataRect.Top);
  61:       Width := (DataRect.Right-DataRect.Left);
  62:     end;
  63:   
  64:  end;

完…