2017-05-24 40 views
0

請,我需要幫助帆布用途: 當用戶在DBGrid中的標題欄移動鼠標, 標題描述德爾福XE 3. 這個問題會消失在Delphi 7使用Canvas.TextOut在DBGrid中 - 德爾福XE 3

不occurr按照下面的代碼:

unit Unit1; 

interface 

uses 

Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, 
Graphics, Controls, Forms, Dialogs, Data.DB, Datasnap.DBClient, 
Grids, DBGrids, Types, StdCtrls; 

type 
TAccessDBGrid = class(TCustomGrid); 

type 
TForm1 = class(TForm) 

DataSource1: TDataSource; 
grid1: TDBGrid; 
cdsTabela: TClientDataSet; 
cdsTabelacodigo_1: TIntegerField; 
cdsTabelacodigo_2: TIntegerField;`enter code here` 

procedure grid1DrawColumnCell(Sender: TObject; const Rect: TRect; 
DataCol: Integer; Column: TColumn; State: TGridDrawState); 

procedure FormCreate(Sender: TObject); 

procedure FormResize(Sender: TObject); 

procedure grid1TitleClick(Column: TColumn); 

private 
    { Private declarations } 
public 
    { Public declarations } 
end; 

var 
Form1: TForm1; 

implementation 
    {$R *.dfm} 

procedure TForm1.FormCreate(Sender: TObject); 
    begin 
     cdsTabela.CreateDataSet; 
    end; 

procedure TForm1.FormResize(Sender: TObject); 
    begin 
     grid1.Refresh; 
    end; 

procedure TForm1.grid1DrawColumnCell(Sender: TObject; const Rect: TRect; 
DataCol: Integer; Column: TColumn; State: TGridDrawState); 
var 
S1 : string; 

begin 

with TAccessDBGrid(grid1) do 
begin 
    RowHeights[0] := 29; 
    Canvas.Brush.Color := clBtnFace; 
    case Column.Index of 
    0: 
    begin 
     Column.Title.Caption := ''; 
     S1 := 'Code'; 
    end; 
    1: 
    begin 
     Column.Title.Caption := ''; 
     S1 := 'Description'; 
    end; 
end; 

TDBGrid(Sender).Canvas.Font.Color:= clBlack; 
Canvas.TextOut(Rect.Left + 3, 19, S1); 
end; 
end; 

procedure TForm1.grid1TitleClick(Column: TColumn); 
begin 
ShowMessage('Title Click! '); 
end; 

end 

欲瞭解更多信息,請參閱我已經發布了答案。

+0

你怎麼能指望改變行高後,你已經開始畫行是去工作?我沒有這臺機器上的D7,但我不認爲它在那裏工作。改爲在FormCreate事件中設置該行的高度。爲什麼你在調用'Canvas.TextOut'時對'19'進行硬編碼,而不是使用'Rect.Top'中的偏移量?當你改變而不是使用'19'時會發生什麼?你的代碼沒有意義;您將刪除每一行上的Title.Caption,並在每行的每個單元格中從S1中繪製相同的文本。此代碼從未工作。 –

+0

@KenWhite:你說「這段代碼從來沒有工作過」是完全正確的。它在我的D7中肯定無法正常工作。 – MartynA

回答

1

您發佈的答案中包含的額外信息使得您更容易理解您正在嘗試做什麼,這很好。請在未來的問題中嘗試做到這一點,因爲它更有可能會更快地得到更好的答案。

無論如何,我建議你用Mike Shkolnik的TSMDBGrid替換你的TDBGrid。請參閱:http://www.scalabium.com/smdbgrid.htm並觀看動畫。

他的網格中包含一個OnDrawColumnTitle事件,我認爲這會比通過TDBGrid實現想要的要容易得多。我從你的評論中收集到你已經遵循了這個建議,併成功地實現了你正在嘗試做的事情。

原來的答覆如下:

我同意肯·懷特說,但除此之外,我覺得你的代碼 是錯誤的。要明白我的意思,試試這個:

  1. 保存一個IDE調試佈局,其中的代碼編輯器不 您Form1的重疊。這樣做的目的是讓Form1不會被強制重新繪製IDE在斷點上存儲的 。

  2. DefaultDrawing設置爲False。這一點是,它設置爲 真正隱藏你的Grid1DrawColumnCell是多麼破碎。

  3. 在您的Grid1DrawColumnCell的第一行上設置斷點,即 with TAccessDBGrid(grid1)

  4. 編譯並運行。請注意,調試器在屏幕上正在繪製表單時在斷點處停止幾次 次,但在此之後,它根本不會在您的BP上停止。因此,一旦表單在屏幕上,您的自定義繪畫就不會出現,直到您導致網格刷新爲止。使用你的表單的OnResize處理程序。

  5. 那麼,一旦將鼠標移動到網格上,將導致列標題標題爲空。答案是,你這樣做!要知道爲什麼......

  6. 在VCL.DBGrids.Pas,發現DrawTitleCell在TCustomDBGrid.DrawCell並把BP在 WRITETEXT(帆布,TextRect,LFrameOffs,LFrameOffs,標題,對齊,...

  7. 再次運行應用程序到您的Grid1DrawColumnCell已執行 並在WriteText BP組調試器停止在步驟6.評估Caption ,你會看到,它是空的(因爲你Grid1DrawColumnCell已經清除了它,當然)點。我想你可以在你的DrawColumnCell退出之前將你的S1的值賦給列標題的標題,但是它的繪圖 仍然是一團糟,你會看到如果你嘗試

    case Column.Index of 
    0: begin 
    // Column.Title.Caption := ''; 
        S1 := 'Code'; 
        Column.Title.Caption := S1; 
    end; 
    1: begin 
    // Column.Title.Caption := ''; 
        S1 := 'Description'; 
        Column.Title.Caption := S1; 
    end; 
    

QED

所以基本上你與你Grid1DrawColumnCell浪費你的時間。如果你想知道是否可以自定義繪製列標題/標題,如果是的話,我建議你在做一些研究之後問一個新的q。如果你沒有找到任何東西,你可以考慮派生一個TCustomDBGrid後代並覆蓋它的DrawCell;這樣,您可以更好地控制整個繪圖過程,並且可以更接近您嘗試實現的任何內容。

順便說一句,以上是基於應用程序正在編譯德爾福Seatlle,這是比XE3更新,但我懷疑這有什麼區別。

+0

我添加了一個答案來解釋我的問題。 – alice

+0

謝謝Martyn,您建議使用TSMDBGrid而不是TDBGrid解決了我的問題,它的功能更強大! 我創建了一個2列的樂隊,現在工作!恭喜! – alice

0

我用,而不是Title.Caption Canvas.TextOut原因是 我需要寫2行中的標題:

 Tests 
Test One Test Two 

,因爲我想在寫我,而不是使用Rect.Top「19」標題。 如果我使用Rect.Top,文本在列

的版本區域繪製Canvas.TextOut(Rect.Left + 3,19,S1);

跟隨波紋管我真正的代碼:

在Grid1DrawColumnCell:

with TAccessDBGrid(grid1) do 
begin 
    RowHeights[0] := 37; 
    Canvas.Brush.Color := clBtnFace; 

    case Column.Index of 
    0: begin 
     Column.Title.Caption := ''; 
     S1 := 'Code'; 
    end; 
    1: begin 
     Column.Title.Caption := ''; 
     Canvas.Font.Color := clBlack; 

     S1 := 'Tests'; 

     Canvas.Brush.Style := bsSolid; 
     Canvas.Brush.Color := clBtnFace; 
     Canvas.FillRect(Types.Rect(Rect.Left, 0, Rect.Left + 120, 18)); 
     Canvas.TextOut(Rect.Left + 3, 1, S1); 
     Canvas.Brush.Style := bsSolid; 

     S1 := 'Test One'; 
    end; 
    2: begin 
     Column.Title.Caption := ''; 
     S1 := 'Test Two'; 
    end; 
end; 

    Canvas.Font.Color:= clBlack; 
    Canvas.TextOut(Rect.Left + 3, 19, S1); 
end; 
+0

好吧,如果你在第一時間發佈你的真實代碼,它會有所幫助! – MartynA

+0

對不起,我認爲我會簡化代碼,讓你明白它。 但我不能。 拜託,你能幫我嗎? – alice