Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Paint a border around the selection in a TStringGrid Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
04-Nov-02
Category
VCL-General
Language
Delphi 2.x
Views
68
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

Is there any way to draw just borders around the selected cell(s) or row of a 
string grid? Similar to what you can do in Excel?

Answer:

Using the OnDrawCell event you have full control over how a cell is drawn since you 
do all the work, including drawing a cells background and content yourself.

Paint a thick border around the selection in a grid:

1   unit Unit1;
2   
3   interface
4   
5   uses
6     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7     StdCtrls, ComCtrls, Grids;
8   
9   type
10    TForm1 = class(TForm)
11      StatusBar1: TStatusBar;
12      StringGrid1: TStringGrid;
13      procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
14        Rect: TRect; State: TGridDrawState);
15      procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
16        Shift: TShiftState; X, Y: Integer);
17      procedure StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X, Y:
18        Integer);
19    private
20      { Private declarations }
21      FLastCell: TGridCoord;
22    public
23      { Public declarations }
24    end;
25  
26  var
27    Form1: TForm1;
28  
29  implementation
30  
31  {$R *.DFM}
32  
33  procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
34    Rect: TRect; State: TGridDrawState);
35  
36    procedure DrawLine(onCanvas: TCanvas; x1, y1, x2, y2: Integer);
37    begin
38      onCanvas.MoveTo(x1, y1);
39      onCanvas.LineTo(x2, y2);
40    end;
41  
42  begin
43    if gdFixed in State then
44      Exit;
45    with Sender as TStringgrid do
46    begin
47      Canvas.Brush.Color := $C0FFFF;
48      Canvas.Brush.Style := bsSolid;
49      Canvas.FillRect(Rect);
50      if gdSelected in State then
51      begin
52        Canvas.Pen.Width := 1;
53        Canvas.Pen.Color := clBlue;
54        Canvas.Pen.Style := psSolid;
55        if aCol = Selection.Left then
56        begin
57          DrawLine(Canvas, rect.left, rect.top, rect.left, rect.bottom);
58          DrawLine(Canvas, rect.left + 1, rect.top, rect.left + 1, rect.bottom);
59        end;
60        if aCol = Selection.Right then
61        begin
62          DrawLine(Canvas, rect.right, rect.top, rect.right, rect.bottom);
63          DrawLine(Canvas, rect.right - 1, rect.top, rect.right - 1, rect.bottom);
64        end;
65        if arow = Selection.Top then
66        begin
67          DrawLine(Canvas, rect.left, rect.top, rect.right, rect.top);
68          DrawLine(Canvas, rect.left, rect.top + 1, rect.right, rect.top + 1);
69        end;
70        if arow = Selection.Bottom then
71        begin
72          DrawLine(Canvas, rect.left, rect.bottom, rect.right, rect.bottom);
73          DrawLine(Canvas, rect.left, rect.bottom - 1, rect.right, rect.bottom - 1);
74        end;
75        Canvas.Font.Color := clBlack;
76      end;
77      InflateRect(rect, -2, -2);
78      Canvas.TextRect(rect, rect.left, rect.top, Cells[acol, arow]);
79    end;
80  end;
81  
82  type
83    tgridcracker = class(tstringgrid);
84  
85  procedure TForm1.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
86    Shift: TShiftState; X, Y: Integer);
87  begin
88    (Sender as TStringgrid).MouseToCell(X, Y, FLastCell.X, FLastCell.Y);
89  end;
90  
91  procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X, Y:
92    Integer);
93  var
94    currentcell: TGridCoord;
95    i, k: Integer;
96  begin
97    if ssLeft in Shift then
98    begin
99      (Sender as TStringgrid).MouseToCell(X, Y, currentcell.X, currentcell.Y);
100     if (FLastCell.X <> CurrentCell.X) or (FLastCell.Y <> CurrentCell.Y) then
101     begin
102       with TGridCracker(Sender) do
103         for i := Selection.Left to Selection.Right do
104           for k := selection.top to Selection.bottom do
105             InvalidateCell(i, k);
106     end;
107   end;
108 end;
109 
110 end.


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC