我做了一个数据库程序,用DBGRID控件显示数据,可是如何控制单格显示的颜色啊?就是我在DBGRID控件上做一个popmenu在所选择的格上点鼠标点命令,所选的单格的颜色变色!帮帮忙!好着急!!分不够再给!!!

解决方案 »

  1.   

    你想表达什么??改变选种之后的record的颜色还是改变某些行的颜色??
      

  2.   

    if table1.RecNo=2*n then
        begin
          oldpm:= dbgrid1.Canvas.pen.mode;
          oldcolor:= dbgrid1.Canvas.Brush.color;
          dbgrid1.Canvas.Brush.color:=claqua;
          dbgrid1.Canvas.pen.mode:=pmmask;
          dbgrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
          dbgrid1.Canvas.Brush.color:=oldcolor;
          dbgrid1.Canvas.pen.mode:=oldpm;
        end;
      

  3.   

    只能在下列事件重画DBGrid单元各
    procedure TForm.DBGridDrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    begin
     with DBGridtemp do
     if datasource.dataset.fieldbyname('FISSee').asboolean=true then//记录变色条件
     begin
       Canvas.font.color:=clblue;
       DefaultDrawColumnCell(rect,datacol,column,state);
     end;
    end;
      

  4.   

    给你一个新的控件(DBGRID),支持彩色显示
    unit drdbgrid;
    (*
     COPYRIGHT (c) RUSSIAN SYSTEM DEVELOPMENTS 1997
     RSD consulting service:
     http://www.rsd.pp.ru
    *)interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Grids, DBGrids, DB;type  TDrawFieldCellEvent = procedure (Sender: TObject; Field : TField;
        Var Color : TColor; Var Font : TFont) of object;  TDrawDBGrid = class(TDBGrid)
      private
        FDrawFieldCellEvent : TDrawFieldCellEvent;
      protected
        procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
             AState: TGridDrawState); override;
      public
        constructor Create(AOwner : TComponent); override;
        destructor Destroy;  override;
      published
        property OnDrawFieldCellEvent : TDrawFieldCellEvent read FDrawFieldCellEvent
                                      write FDrawFieldCellEvent;
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('RSD_FAQ', [TDrawDBGrid]);
    end;var
      DrawBitmap: TBitmap;
      UserCount: Integer;procedure UsesBitmap;
    begin
      if UserCount = 0 then begin
        DrawBitmap := TBitmap.Create;
      end;
      Inc(UserCount);
    end;procedure ReleaseBitmap;
    begin
      Dec(UserCount);
    end;function Max(X, Y: Integer): Integer;
    begin
      Result := Y;
      if X > Y then Result := X;
    end;procedure WriteText(ACanvas: TCanvas; ARect: TRect; DX, DY: Integer;
      const Text: string; Alignment: TAlignment);
    const
      AlignFlags : array [TAlignment] of Integer =
        ( DT_LEFT or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
          DT_RIGHT or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
          DT_CENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX );
    var
      B, R: TRect;
      I, Left: Integer;
    begin
      I := ColorToRGB(ACanvas.Brush.Color);
      if GetNearestColor(ACanvas.Handle, I) = I then
      begin
        case Alignment of
          taLeftJustify:
            Left := ARect.Left + DX;
          taRightJustify:
            Left := ARect.Right - ACanvas.TextWidth(Text) - 3;
        else
          Left := ARect.Left + (ARect.Right - ARect.Left) shr 1
            - (ACanvas.TextWidth(Text) shr 1);
        end;
        ExtTextOut(ACanvas.Handle, Left, ARect.Top + DY, ETO_OPAQUE or
          ETO_CLIPPED, @ARect, PChar(Text), Length(Text), nil);
      end
      else begin
        with DrawBitmap, ARect do
        begin
          Width := Max(Width, Right - Left);
          Height := Max(Height, Bottom - Top);
          R := Rect(DX, DY, Right - Left - 1, Bottom - Top - 1);
          B := Rect(0, 0, Right - Left, Bottom - Top);
        end;
        with DrawBitmap.Canvas do
        begin
          Font := ACanvas.Font;
          Font.Color := ACanvas.Font.Color;
          Brush := ACanvas.Brush;
          Brush.Style := bsSolid;
          FillRect(B);
          SetBkMode(Handle, TRANSPARENT);
          DrawText(Handle, PChar(Text), Length(Text), R, AlignFlags[Alignment]);
        end;
        ACanvas.CopyRect(ARect, DrawBitmap.Canvas, B);
      end;
    end;
    constructor TDrawDBGrid.Create(AOwner : TComponent);
    begin
      inherited Create(AOwner);
      UsesBitmap;
    end;destructor TDrawDBGrid.Destroy;
    begin
      ReleaseBitmap;
      inherited Destroy;
    end;procedure TDrawDBGrid.DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);
    var
      OldActive: Integer;
     // Indicator: Integer;
      Highlight: Boolean;
      Value: string;
      DrawColumn: TColumn;
     // FrameOffs: Byte;
      cl : TColor;
      fn : TFont;
    begin
      if csLoading in ComponentState then
      begin
        Canvas.Brush.Color := Color;
        Canvas.FillRect(ARect);
        Exit;
      end;
      if (gdFixed in AState) and (ACol - IndicatorOffset < 0) then begin
        inherited DrawCell(ACol, ARow, ARect, AState);
        exit;
      end;  if (dgTitles in Options) And (ARow = 0) then begin
        inherited DrawCell(ACol, ARow, ARect, AState);
        exit;
      end;  if(dgTitles in Options) then
        Dec(ARow);
      Dec(ACol, IndicatorOffset);
      
      if (gdFixed in AState) and ([dgRowLines, dgColLines] * Options =
        [dgRowLines, dgColLines]) then
      begin
        InflateRect(ARect, -1, -1);
     //   FrameOffs := 1;
      end ;
      //else
       // FrameOffs := 2;  with Canvas do
      begin
        DrawColumn := Columns[ACol];
        Font := DrawColumn.Font;
        Brush.Color := DrawColumn.Color;
        if (DataLink = nil) or not DataLink.Active then
          FillRect(ARect)
        else
        begin
          Value := '';
          OldActive := DataLink.ActiveRecord;
          try
            DataLink.ActiveRecord := ARow;
            if Assigned(DrawColumn.Field) then begin
              Value := DrawColumn.Field.DisplayText;
              if Assigned(FDrawFieldCellEvent) then  begin
                cl := Brush.Color;
                fn := Font;
                FDrawFieldCellEvent(self, DrawColumn.Field, cl, fn);
                Brush.Color := cl;
                Font := fn;
              end;  
            end;        Highlight := HighlightCell(ACol, ARow, Value, AState);
            if Highlight then
            begin
              Brush.Color := clHighlight;
              Font.Color := clHighlightText;
            end;
            if DefaultDrawing then
              DefaultDrawColumnCell(ARect, ACol, DrawColumn, AState);
            if Columns.State = csDefault then
              DrawDataCell(ARect, DrawColumn.Field, AState);
            DrawColumnCell(ARect, ACol, DrawColumn, AState);
          finally
            DataLink.ActiveRecord := OldActive;
          end;
          if DefaultDrawing and (gdSelected in AState)
            and ((dgAlwaysShowSelection in Options) or Focused)
            and not (csDesigning in ComponentState)
            and not (dgRowSelect in Options)
            and (UpdateLock = 0)
            and (ValidParentForm(Self).ActiveControl = Self) then
            Windows.DrawFocusRect(Handle, ARect);
        end;
      end;
      
      if (gdFixed in AState) and ([dgRowLines, dgColLines] * Options =
        [dgRowLines, dgColLines]) then
      begin
        InflateRect(ARect, 1, 1);
        DrawEdge(Canvas.Handle, ARect, BDR_RAISEDINNER, BF_BOTTOMRIGHT);
        DrawEdge(Canvas.Handle, ARect, BDR_RAISEDINNER, BF_TOPLEFT);
      end;
    end;end.