線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:5122
推到 Plurk!
推到 Facebook!

StringGrid控制cell的顏色還要控制文字反白光棒能與cell底色不同

尚未結案
A022
一般會員


發表:18
回覆:29
積分:9
註冊:2002-06-21

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-12-22 23:06:51 IP:211.76.xxx.xxx 未訂閱
請問:StringGrid控制cell的顏色還要控制文字反白能與cell底色不同,也就是說有cell底色,font前景色之外,還在font上有個實心的光棒(Rectangle),既不會讓cell底色蓋掉光棒顏色又不會讓光棒顏色蓋住font的顯示-這三個效果要同時達到,要如何達成?謝謝!
elvis1000
一般會員


發表:6
回覆:50
積分:16
註冊:2004-12-18

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-12-23 00:50:15 IP:220.134.xxx.xxx 未訂閱
只要把Options內的goDrawFocusSelected設為true,就算是編輯模式下也會自己反白,你的意思是要自己重畫一個select嗎? ----------------------- God bless you! I am Dark_Angel.
------
-----------------------
God bless you!
I am Dark_Angel.
luckfox
一般會員


發表:34
回覆:40
積分:24
註冊:2002-10-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-12-23 15:02:17 IP:61.217.xxx.xxx 未訂閱
您好,以下雖然是C Code,但是原理應該一樣,不知道能否解決您的問題
 
//  在Design 時期填入 StringGrid1->Options<Row ) StringGrid1->Canvas->Font->Color=clRed;         StringGrid1->Canvas->FillRect(Rect);
     StringGrid1->Canvas->TextOut(Rect.Left 2, Rect.Top 2,
                                   StringGrid1->Cells[ACol][ARow]);    }
void __fastcall TForm1::FormCreate(TObject *Sender)
{
   int i,j;       for(i=0;iRowCount;i  )
   {
     for(j=0;jColCount;j  ) StringGrid1->Cells[i][j]=IntToStr(i) IntToStr(j);
     StringGrid1->Color=clGreen;       }
}
發表人 - luckfox 於 2004/12/23 15:12:14
A022
一般會員


發表:18
回覆:29
積分:9
註冊:2002-06-21

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-12-23 15:54:58 IP:211.76.xxx.xxx 未訂閱
elvis1000和luckfox兩位好:因為我想控制每個cell的顏色,Stringgrid的canvas的brush已是背景色了,cell上的font是前景色,現在還想在font上畫光棒,等於是在前景色上面再加前景色,如果用Canvas的Rectangle()在font上畫光棒,會用brush的顏色填充,這樣子就和brush顏色一樣,光棒就沒效果了!用FillRect()似乎也一樣,而我的Stringgrid是alClient大小,form開到最大,cell內的font也有顏色,因此上述方法大概無效,棘手啊!謝謝兩位!
jumo
一般會員


發表:33
回覆:65
積分:24
註冊:2002-04-17

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-12-24 16:55:34 IP:203.66.xxx.xxx 未訂閱
參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
A022
一般會員


發表:18
回覆:29
積分:9
註冊:2002-06-21

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-01-04 20:16:07 IP:211.76.xxx.xxx 未訂閱
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
謝謝jumo提供的方法那可以解決部分的問題,只是一來許多的Panel不太好管理二來程式的需求cell是做監視之用-有狀況時光棒才反白,cell才變色,其他的照常顯示.cell中顯示的字串還有向左,右對齊,置中對齊,換行顯示等等的需求,我之前忘記說還需要讓cell閃爍,這真像是不可能的任務.我想抓stringgrid整個當window,用WindowFromDC(),GetWindowRect(),再用windows.FillRect()到文字顯示的位置但是只要是文字顯示的位置,效果就出不來,cell內其他位置就還有fillrect()的效果 FlashWindow(Ex)()這兩個API函式當然不夠用.另外有網友提供的TStringAlignGrid元件似乎可以讓cell換行顯示像TMemo一樣,只是不知道對cell顏色的控制到何種程度,也許讓文字顯示在Panel.Caption上是一種方法,但並未解決所有問題,該怎麼辦呢?謝謝囉!
A022
一般會員


發表:18
回覆:29
積分:9
註冊:2002-06-21

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-01-04 20:18:10 IP:211.76.xxx.xxx 未訂閱
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
謝謝jumo提供的方法那可以解決部分的問題,只是一來許多的Panel不太好管理二來程式的需求cell是做監視之用-有狀況時光棒才反白,cell才變色,其他的照常顯示.cell中顯示的字串還有向左,右對齊,置中對齊,換行顯示等等的需求,我之前忘記說還需要讓cell閃爍,這真像是不可能的任務.我想抓stringgrid整個當window,用WindowFromDC(),GetWindowRect(),再用windows.FillRect()到文字顯示的位置但是只要是文字顯示的位置,效果就出不來,cell內其他位置就還有fillrect()的效果 FlashWindow(Ex)()這兩個API函式當然不夠用.另外有網友提供的TStringAlignGrid元件似乎可以讓cell換行顯示像TMemo一樣,只是不知道對cell顏色的控制到何種程度,也許讓文字顯示在Panel.Caption上是一種方法,但並未解決所有問題,該怎麼辦呢?謝謝囉!
A022
一般會員


發表:18
回覆:29
積分:9
註冊:2002-06-21

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-01-04 20:20:09 IP:211.76.xxx.xxx 未訂閱
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
謝謝jumo提供的方法那可以解決部分的問題,只是一來許多的Panel不太好管理二來程式的需求cell是做監視之用-有狀況時光棒才反白,cell才變色,其他的照常顯示.cell中顯示的字串還有向左,右對齊,置中對齊,換行顯示等等的需求,我之前忘記說還需要讓cell閃爍,這真像是不可能的任務.我想抓stringgrid整個當window,用WindowFromDC(),GetWindowRect(),再用windows.FillRect()到文字顯示的位置但是只要是文字顯示的位置,效果就出不來,cell內其他位置就還有fillrect()的效果 FlashWindow(Ex)()這兩個API函式當然不夠用.另外有網友提供的TStringAlignGrid元件似乎可以讓cell換行顯示像TMemo一樣,只是不知道對cell顏色的控制到何種程度,也許讓文字顯示在Panel.Caption上是一種方法,但並未解決所有問題,該怎麼辦呢?謝謝囉!
A022
一般會員


發表:18
回覆:29
積分:9
註冊:2002-06-21

發送簡訊給我
#9 引用回覆 回覆 發表時間:2005-01-04 20:21:56 IP:211.76.xxx.xxx 未訂閱
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
謝謝jumo提供的方法那可以解決部分的問題,只是一來許多的Panel不太好管理二來程式的需求cell是做監視之用-有狀況時光棒才反白,cell才變色,其他的照常顯示.cell中顯示的字串還有向左,右對齊,置中對齊,換行顯示等等的需求,我之前忘記說還需要讓cell閃爍,這真像是不可能的任務.我想抓stringgrid整個當window,用WindowFromDC(),GetWindowRect(),再用windows.FillRect()到文字顯示的位置但是只要是文字顯示的位置,效果就出不來,cell內其他位置就還有fillrect()的效果 FlashWindow(Ex)()這兩個API函式當然不夠用.另外有網友提供的TStringAlignGrid元件似乎可以讓cell換行顯示像TMemo一樣,只是不知道對cell顏色的控制到何種程度,也許讓文字顯示在Panel.Caption上是一種方法,但並未解決所有問題,該怎麼辦呢?謝謝囉!
A022
一般會員


發表:18
回覆:29
積分:9
註冊:2002-06-21

發送簡訊給我
#10 引用回覆 回覆 發表時間:2005-01-04 20:24:38 IP:211.76.xxx.xxx 未訂閱
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
謝謝jumo提供的方法那可以解決部分的問題,只是一來許多的Panel不太好管理二來程式的需求cell是做監視之用-有狀況時光棒才反白,cell才變色,其他的照常顯示.cell中顯示的字串還有向左,右對齊,置中對齊,換行顯示等等的需求,我之前忘記說還需要讓cell閃爍,這真像是不可能的任務.我想抓stringgrid整個當window,用WindowFromDC(),GetWindowRect(),再用windows.FillRect()到文字顯示的位置但是只要是文字顯示的位置,效果就出不來,cell內其他位置就還有fillrect()的效果 FlashWindow(Ex)()這兩個API函式當然不夠用.另外有網友提供的TStringAlignGrid元件似乎可以讓cell換行顯示像TMemo一樣,只是不知道對cell顏色的控制到何種程度,也許讓文字顯示在Panel.Caption上是一種方法,但並未解決所有問題,該怎麼辦呢?謝謝囉!
A022
一般會員


發表:18
回覆:29
積分:9
註冊:2002-06-21

發送簡訊給我
#11 引用回覆 回覆 發表時間:2005-01-04 20:32:06 IP:211.76.xxx.xxx 未訂閱
引言: 參考看看下面的程式
//==========================================
object Form1: TForm1
  Left = 192
  Top = 107
  Width = 640
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = CHINESEBIG5_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = '新細明體'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 408
    Top = 8
    Width = 39
    Height = 13
    Caption = '作用格'
  end
  object Label2: TLabel
    Left = 408
    Top = 56
    Width = 26
    Height = 13
    Caption = '座標'
  end
  object Label3: TLabel
    Left = 424
    Top = 32
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label4: TLabel
    Left = 528
    Top = 32
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label5: TLabel
    Left = 424
    Top = 80
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label6: TLabel
    Left = 528
    Top = 80
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object Label7: TLabel
    Left = 408
    Top = 104
    Width = 26
    Height = 13
    Caption = '其他'
  end
  object Label8: TLabel
    Left = 424
    Top = 128
    Width = 26
    Height = 13
    Caption = '字體'
  end
  object Label9: TLabel
    Left = 528
    Top = 128
    Width = 26
    Height = 13
    Caption = '背景'
  end
  object StringGrid1: TStringGrid
    Left = 6
    Top = 8
    Width = 393
    Height = 393
    ColCount = 15
    DefaultColWidth = 25
    DefaultRowHeight = 25
    RowCount = 15
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goEditing]
    ScrollBars = ssNone
    TabOrder = 0
    OnClick = StringGrid1Click
    OnDrawCell = StringGrid1DrawCell
  end
  object Panel1: TPanel
    Left = 456
    Top = 24
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 1
    OnClick = Panel1Click
  end
  object Panel2: TPanel
    Left = 560
    Top = 24
    Width = 64
    Height = 24
    Color = 16744448
    TabOrder = 2
    OnClick = Panel1Click
  end
  object Panel3: TPanel
    Left = 456
    Top = 72
    Width = 64
    Height = 24
    Color = clGray
    TabOrder = 3
    OnClick = Panel1Click
  end
  object Panel4: TPanel
    Left = 560
    Top = 72
    Width = 64
    Height = 24
    Color = 8421631
    TabOrder = 4
    OnClick = Panel1Click
  end
  object Panel5: TPanel
    Left = 456
    Top = 120
    Width = 64
    Height = 24
    Color = clBlack
    TabOrder = 5
    OnClick = Panel1Click
  end
  object Panel6: TPanel
    Left = 560
    Top = 120
    Width = 64
    Height = 24
    Color = clWhite
    TabOrder = 6
    OnClick = Panel1Click
  end
  object ColorDialog1: TColorDialog
    Left = 48
    Top = 120
  end
end
//====================================
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, ExtCtrls, StdCtrls;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    ColorDialog1: TColorDialog;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Panel5: TPanel;
    Panel6: TPanel;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Panel1Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (gdFocused in State) then
    begin
       StringGrid1.Canvas.Brush.Color := Panel2.Color;
       StringGrid1.Canvas.Font.Color := Panel1.Color;
    end
  else
    if (StringGrid1.Col=ACol) or (StringGrid1.Row=ARow) then
      begin
       StringGrid1.Canvas.Brush.Color := Panel4.Color;
       StringGrid1.Canvas.Font.Color := Panel3.Color;
      end
    else
      begin
       StringGrid1.Canvas.Brush.Color := Panel6.Color;
       StringGrid1.Canvas.Font.Color := Panel5.Color;
      end;      If (ACol>(StringGrid1.FixedCols-1)) and (ARow>(StringGrid1.FixedRows-1)) then begin
    StringGrid1.canvas.fillRect(Rect);
    StringGrid1.canvas.TextOut(Rect.Left,Rect.Top,StringGrid1.Cells[ACol,ARow]);
  end;
end;    procedure TForm1.Panel1Click(Sender: TObject);
begin
  if ColorDialog1.Execute then begin
    TPanel(Sender).Color:=ColorDialog1.Color;
    StringGrid1.Repaint;
  end;
end;    procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  StringGrid1.Repaint;
end;    end.    
又是網路問題,請站長刪去多餘的回應,謝謝!
A022
一般會員


發表:18
回覆:29
積分:9
註冊:2002-06-21

發送簡訊給我
#12 引用回覆 回覆 發表時間:2005-03-17 23:11:01 IP:211.76.xxx.xxx 未訂閱
或許只能用jumo的方法,不然只有override Paint()一途,這個工程可不小.設定Clipping Region不知是否可行?但我想如果可行也要配合override Paint()才有效.所以還是先給jumo一分吧!
A022
一般會員


發表:18
回覆:29
積分:9
註冊:2002-06-21

發送簡訊給我
#13 引用回覆 回覆 發表時間:2005-03-25 21:57:38 IP:211.76.xxx.xxx 未訂閱
引言: 我試過可以了!是在OnDrawcell()中建立內部procedure,內容大約如下: procedure TAlignGridfrm.ColorAlignGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); procedure Hilight; var DrawRect:TRect; begin DrawRect:= Rect; SetRect(DrawRect,...,...,...,...);//座標省略 Windows.InvertRect(ColorAlignGrid1.Canvas.Handle,DrawRect); end; var X,Y:Integer ; begin With Sender as TStringGrid,Canvas do begin case ACol of 0:begin SetTextAlign(Canvas.Handle,TA_RIGHT); X:=Rect.Right-2; Y:=Rect.Top 2 ; end; 1:begin case ARow of 1:begin SetTextAlign(Canvas.Handle,TA_CENTER Or TA_BOTTOM); X:=(Rect.Left Rect.Right) DIV 2; Y:=(Rect.Top Rect.Bottom ) DIV 2; end else begin SetTextAlign(Canvas.Handle,TA_CENTER); X:=(Rect.Left Rect.Right) DIV 2; Y:= Rect.Top 2 ; end; end; //case ARow end end else begin //else ACol case ARow of 3:begin SetTextAlign(Canvas.Handle,TA_LEFT Or TA_BOTTOM); X:= Rect.Left 2; Y:= Rect.Bottom-2 ; end; else begin SetTextAlign(Canvas.Handle,TA_LEFT); X:= Rect.Left 2; Y:= Rect.Top 2 ; end; end; end; //case end end; //end with canvas.TextRect (Rect,X,Y{Rect.Top 2},ColorAlignGrid1.cells[ACol,ARow]); Hilight;{在此處呼叫Hilight()} {若有多個CELL反白,各cell各設一變數,若為true則針對該cell呼叫hilight(),故若此處有多次hilight()呼叫不用訝異} end; end; 因為沒有GIF或JPG圖檔,故執行結果無法上傳.Sorry!
A022
一般會員


發表:18
回覆:29
積分:9
註冊:2002-06-21

發送簡訊給我
#14 引用回覆 回覆 發表時間:2005-03-30 12:58:47 IP:211.76.xxx.xxx 未訂閱
引言:
引言: 我試過可以了!是在OnDrawcell()中建立內部procedure,內容大約如下: procedure TAlignGridfrm.ColorAlignGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); procedure Hilight; var DrawRect:TRect; begin DrawRect:= Rect; SetRect(DrawRect,...,...,...,...);//座標省略 Windows.InvertRect(ColorAlignGrid1.Canvas.Handle,DrawRect); end; var X,Y:Integer ; begin With Sender as TStringGrid,Canvas do begin case ACol of 0:begin SetTextAlign(Canvas.Handle,TA_RIGHT); X:=Rect.Right-2; Y:=Rect.Top+2 ; end; 1:begin case ARow of 1:begin SetTextAlign(Canvas.Handle,TA_CENTER Or TA_BOTTOM); X:=(Rect.Left+Rect.Right) DIV 2; Y:=(Rect.Top+Rect.Bottom ) DIV 2; end else begin SetTextAlign(Canvas.Handle,TA_CENTER); X:=(Rect.Left+Rect.Right) DIV 2; Y:= Rect.Top+2 ; end; end; //case ARow end end else begin //else ACol case ARow of 3:begin SetTextAlign(Canvas.Handle,TA_LEFT Or TA_BOTTOM); X:= Rect.Left+2; Y:= Rect.Bottom-2 ; end; else begin SetTextAlign(Canvas.Handle,TA_LEFT); X:= Rect.Left+2; Y:= Rect.Top+2 ; end; end; end; //case end end; //end with canvas.TextRect (Rect,X,Y{Rect.Top+2},ColorAlignGrid1.cells[ACol,ARow]); Hilight;{在此處呼叫Hilight()} {若有多個CELL反白,各cell各設一變數,若為true則針對該cell呼叫hilight(),故若此處有多次hilight()呼叫不用訝異} end; end; 因為沒有GIF或JPG圖檔,故執行結果無法上傳.Sorry!
我把執行結果Post上來.
wameng
版主


發表:31
回覆:1336
積分:1188
註冊:2004-09-16

發送簡訊給我
#15 引用回覆 回覆 發表時間:2005-03-30 20:55:56 IP:61.31.xxx.xxx 未訂閱
歸納您的問題: 1. 如何取得反白顏色。 Function InvertColor(IColor:TColor):TColor; var R,G,B :byte; iRGB :Longint; begin iRGB :=ColorToRGB(IColor); R :=(iRGB and $FF) xor 255 ; G :=(iRGB and $FF00 shr 8) xor 255; B :=(iRGB and $FF0000 shr 16) xor 255; Result := RGB(R,G,B); end; 可以取得相反顏色,但跟反白顏色是有點不同的。 2.考慮文字底色與Cell底色必須不同。 如果再不需要編輯文字的狀態下! 建議先處理 Cell底色與文字顏色後。 利用 InvertRect API 做文字區塊 反相。 這樣就可以達成您的要求! 3.從著色步驟來看。 a. Brush.Color := Cell底色 b. FillRect CellRect的範圍 C. Brush.color := 文字底色 D. Font.Color := 文字顏色 E. Textout 畫文字
A022
一般會員


發表:18
回覆:29
積分:9
註冊:2002-06-21

發送簡訊給我
#16 引用回覆 回覆 發表時間:2005-04-07 21:14:54 IP:211.76.xxx.xxx 未訂閱
引言: 歸納您的問題: 1. 如何取得反白顏色。 Function InvertColor(IColor:TColor):TColor; var R,G,B :byte; iRGB :Longint; begin iRGB :=ColorToRGB(IColor); R :=(iRGB and $FF) xor 255 ; G :=(iRGB and $FF00 shr 8) xor 255; B :=(iRGB and $FF0000 shr 16) xor 255; Result := RGB(R,G,B); end; 可以取得相反顏色,但跟反白顏色是有點不同的。 2.考慮文字底色與Cell底色必須不同。 如果再不需要編輯文字的狀態下! 建議先處理 Cell底色與文字顏色後。 利用 InvertRect API 做文字區塊 反相。 這樣就可以達成您的要求! 3.從著色步驟來看。 a. Brush.Color := Cell底色 b. FillRect CellRect的範圍 C. Brush.color := 文字底色 D. Font.Color := 文字顏色 E. Textout 畫文字
咦!wameng老大!我就是用InvertRect()來實現這個需求的,我post的程式碼裡面就有,只是用的元件是繼承Stringgrid的,另外建立一個event handler呼叫TStringgrid的Drawcell()
wameng
版主


發表:31
回覆:1336
積分:1188
註冊:2004-09-16

發送簡訊給我
#17 引用回覆 回覆 發表時間:2005-04-08 13:50:32 IP:61.222.xxx.xxx 未訂閱
老實說!我還不清楚你要做些什麼! ...... 最近的感言:『學的越多,卻越來越笨。』
系統時間:2024-11-28 0:31:01
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!