全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2490
推到 Plurk!
推到 Facebook!

TDBGrid 如何把現在那一列用不同顏色顯示?

尚未結案
cancer
高階會員


發表:58
回覆:319
積分:190
註冊:2004-07-31

發送簡訊給我
#1 引用回覆 回覆 發表時間:2011-01-26 17:09:02 IP:211.75.xxx.xxx 未訂閱
各位好,

客戶要求 TDBGrid 打字中的那一列用不同顏色示,OnDrawColumnCell 可以改變底色,但我不知道現在是在第幾列?
當然,這可以用 Protected Hack 方法取得 TDBGrid 的 Row 屬性,但我是用 Delhi 2006,已經不容許 Protected Hack 了,請問還有其他方法可以知道使用者是在 TDBGrid 哪一列打字?
tobylin
一般會員


發表:1
回覆:15
積分:18
註冊:2009-12-25

發送簡訊給我
#2 引用回覆 回覆 發表時間:2011-01-27 09:53:37 IP:60.249.xxx.xxx 未訂閱
DBGrid增強顯示,
只要在程式裡加上粗體字,
****************************************
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, DBTables, uDBGRID;
Type
TDBGrid = uDBGrid.TDBGrid;

type
TForm1 = class(TForm)
Database1: TDatabase;
. . .
. .
************************************************************************
unit uDBGrid;
interface
uses Windows, DBGrids, Classes, Messages, Types,Grids,Graphics,Forms,
DB, SysUtils, Controls;
type
TDBGrid = class(DBGrids.TDBGrid)
private
FVertScrollBar: Boolean;
FOldGridWnd: TWndMethod;
FOddRowColor: TColor;
FEvenRowColor: TColor;
FActiveRowColor: TColor;
FOriginalOptions: TDBGridOptions;
FAllowAppend: Boolean;
procedure NewGridWnd(var aMessage: TMessage);
protected
procedure ColEnter; override;
procedure ColExit; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure DrawCell(aCol, aRow: Longint; aRect: TRect;
aState: TGridDrawState); override;
procedure Scroll(Distance: Integer); override;
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property AllowAppend: Boolean read FAllowAppend write FAllowAppend default True;
property InplaceEditor;
property OddRowColor: TColor read FOddRowColor write FOddRowColor default clCream;
property EvenRowColor: TColor read FEvenRowColor write FEvenRowColor default clMoneyGreen;
property ActiveRowColor: TColor read FActiveRowColor write FActiveRowColor default $00E7CFFF;
property VertScrollBar: Boolean read FVertScrollBar write FVertScrollBar default True;
end;
implementation
{ TDBGrid }
constructor TDBGrid.Create(AOwner: TComponent);
begin
inherited;
FOldGridWnd := WindowProc;
WindowProc := NewGridWnd;
FOddRowColor := clCream;
FEvenRowColor := $00FFF2E8;
FActiveRowColor := $00F2E8FF;
FVertScrollBar := True;
AllowAppend := False;
end;

destructor TDBGrid.Destroy;
begin
WindowProc := FOldGridWnd;
inherited Destroy;
end;
procedure TDBGrid.DrawCell(aCol, aRow: Integer; aRect: TRect;
aState: TGridDrawState);
function RowIsMultiSelected: Boolean;
var
Index: Integer;
begin
Result := (dgMultiSelect in Options) and Datalink.Active and
SelectedRows.Find(Datalink.Datasource.Dataset.Bookmark, Index);
end;
var
lOldActiveRecord: Integer;
lDrawColumn: TColumn;
begin
if not Assigned(DataSource) then exit;
if not Assigned(DataSource.DataSet) then exit;
if not DataSource.DataSet.Active then exit;
if (dgTitles in Options) and (ARow = 0)
or (gdFixed in AState) and (ACol - IndicatorOffset < 0) then
begin
inherited DrawCell(ACol, ARow, ARect, AState);
Exit;
end;
if (dgTitles in Options) then Dec(ARow);
Dec(ACol, IndicatorOffset);
if (gdFixed in AState) and ([dgRowLines, dgColLines] * Options =
[dgRowLines, dgColLines]) then
InflateRect(ARect, -1, -1)
else
with Canvas do
begin
lDrawColumn := Columns[ACol];
if not lDrawColumn.Showing then Exit;
Font := lDrawColumn.Font;
Brush.Color := lDrawColumn.Color;
Font.Color := lDrawColumn.Font.Color;
if Odd(ARow) then Brush.Color := FEvenRowColor
else Brush.Color := FOddRowColor;
if (aCol = 0)
and (lDrawColumn.Field.FieldKind = fkCalculated) then
Brush.Color := clBtnFace;

if not Assigned(DataLink) or not DataLink.Active then
FillRect(ARect)
else
begin
if ARow >= 0 then
begin
lOldActiveRecord := DataLink.ActiveRecord;
try
DataLink.ActiveRecord := ARow;
if (DataLink.ActiveRecord = Row - 1) or RowIsMultiSelected then
begin
Font.Color := clBlack;
Brush.Color := FActiveRowColor;
end;
if DefaultDrawing then
DefaultDrawColumnCell(ARect, ACol, LDrawColumn, AState);
if Columns.State = csDefault then
DrawDataCell(ARect, lDrawColumn.Field, AState);
DrawColumnCell(ARect, ACol, lDrawColumn, AState);
finally
DataLink.Activerecord := lOldActiveRecord;
end; // end try
end; // end if aRow > 0

if DefaultDrawing and (gdSelected in AState) and
((dgAlwaysShowSelection in Options) or Focused)
and not (csDesigning in Componentstate)
and not (dgRowSelect in Options)
and (ValidParentForm(self).ActiveControl = self) then
begin
Windows.DrawFocusRect(Handle, ARect);
Canvas.Brush.COlor := clNavy;
Canvas.FillRect(ARect);
Canvas.Font.Color := clWhite;
Canvas.Font.Style := [fsBold];
DefaultDrawColumnCell(ARect, ACol, lDrawColumn, AState);
end;
end;
end;

if (gdFixed in AState) and ([dgRowLines, dgColLines] * Options =
[dgRowLines, dgColLines]) then
begin
InflateRect(ARect, -2, -2);
DrawEdge(Canvas.Handle, ARect, BDR_RAISEDINNER, BF_BOTTOMRIGHT);
DrawEdge(Canvas.Handle, ARect, BDR_SUNKENINNER, BF_TOPLEFT);
end;
end;

procedure TDBGrid.NewGridWnd(var aMessage: TMessage);
var
IsNeg: Boolean;
begin
if (aMessage.Msg = WM_MOUSEWHEEL)
and DataSource.DataSet.Active then
begin
IsNeg := short(aMessage.WParamHi) < 0;
if IsNeg then
DataSource.DataSet.MoveBy(1)
else
DataSource.DataSet.MoveBy(-1)
end
else
FOldGridWnd(aMessage);
end;
procedure TDBGrid.Paint;
begin
if not FVertScrollBar then
SetScrollRange(Self.Handle, SB_VERT, 0, 0, False);
inherited Paint;
end;
procedure TDBGrid.Scroll(Distance: Integer);
begin
inherited Scroll(Distance);
Refresh;
end;
procedure TDBGrid.KeyDown(var Key: Word; Shift: TShiftState);
var
lStateChange: TNotifyEvent;
begin
inherited;
if (Key = VK_Space)
and not SelectedField.ReadOnly
and (SelectedField is TBooleanField) then
begin
lStateChange := DataSource.OnStateChange;
DataSource.OnStateChange := nil;
try
DataLink.DataSet.Edit;
SelectedField.AsBoolean := not SelectedField.AsBoolean;
DataLink.DataSet.Post;
finally
DataSource.OnStateChange := lStateChange;
end;
end;

if (not FAllowAppend) and (DataLink.DataSet.Eof) and (Key = VK_DOWN) then
begin
Key := 0;
DataLink.DataSet.Cancel;
end;
end;

procedure TDBGrid.ColEnter;
begin
inherited;
if SelectedField is TBooleanField then
begin
FOriginalOptions := Options;
Options := Options - [dgEditing];
end;
end;
procedure TDBGrid.ColExit;
begin
inherited;
if SelectedField is TBooleanField then
Options := FOriginalOptions
end;
end.

cancer
高階會員


發表:58
回覆:319
積分:190
註冊:2004-07-31

發送簡訊給我
#3 引用回覆 回覆 發表時間:2011-01-27 16:16:57 IP:211.75.xxx.xxx 未訂閱
看起來只有另久寫元件才行,您的方法幾乎就是這樣,請問一下,
Type
TDBGrid = uDBGrid.TDBGrid;
這樣可以蓋掉 Delphi 本身提供的 TDBGrid ?
系統時間:2024-05-16 23:01:27
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!