I am using Developer Express components - TdxDBgrid as Grid and TdxMemData as dataset.
There are around 10 columns displayed in grid.
For the second column, I am trying to change starting point where column value displayed. I am trying to do it using ACanvas.TextRect. But the changes are not getting affected. Anybody having idea how to change starting position of data in Column for any grid.
thanks
I'm not sure if what you're asking is how to shift the x-position where the text starts, but if it is, try something like this:
procedure TForm1.dxDBGrid1Column2CustomDrawCell(Sender: TObject;
ACanvas: TCanvas; ARect: TRect; ANode: TdxTreeListNode;
AColumn: TdxTreeListColumn; ASelected, AFocused, ANewItemRow: Boolean;
var AText: String; var AColor: TColor; AFont: TFont;
var AAlignment: TAlignment; var ADone: Boolean);
var
XOffset : Integer;
begin
XOffset := 20;
ACanvas.FillRect(ARect);
ACanvas.TextOut(ARect.Left + XOffset, ARect.Top, AText);
ADone := True;
end;
Obviously that doesn't deal with details like how to draw selected and focused columns, etc, but you should get the idea and you can look at the DevEx source for those.
Related
I have an 8 x 16 DrawGrid in Delphi XE5 that I would like to randomly fill with nine images I've stored in C:\Users\Sean Ewing\Documents\My Documents\Delphi Tutorials\Other\Math-O-Sphere\Win32\Debug\img. I'm currently trying to get one image to load to make sure I'm doing it correctly. Here is the code I've used to do this:
procedure TForm1.grdPlayFieldDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
spherePlus: TBitmap;
begin
spherePlus.LoadFromFile(ExtractFilePath(Application.ExeName) + '\img\Sphere +1.bmp');
grdPlayField.Canvas.Draw(0, 0, spherePlus);
end;
The code compiles fine, and based on what I've read in the Embarcadero wiki this is correct, but I get an error at runtime when it's time to load the DrawGgrid. Where did I go wrong?
You need to first create the bitmap before you can use it:
procedure TForm1.grdPlayFieldDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
spherePlus: TBitmap;
begin
spherePlus := TBitmap.Create;
try
spherePlus.LoadFromFile(ExtractFilePath(Application.ExeName) +
'\img\Sphere +1.bmp');
grdPlayField.Canvas.Draw(0, 0, spherePlus);
finally
spherePlus.Free;
end;
end;
The other thing you should be aware of is that the Rect parameter you receive in the event is the area that needs to be painted, so you'll want to use Canvas.StretchDraw and pass it that rectangle. It won't help with the current issue, but you'll need it when you move to the next step. You can identify the exact cell that's being drawn with the ACol and ARow parameters, so you can use that information to load a specific image for a column, for instance, or to output text for a column or row.
// Load specific image for the cell passed in ACol and ARow,
// and then draw it to the appropriate area using the Rect provided.
grdPlayField.Canvas.StretchDraw(Rect, spherePlus);
How can I make all my grids look the same way all over my forms?
I want to implement an alternate row color that must be applied on all grids of my project. Is it possible without adding the same DrawColumnCell event code for every grid?
I want to avoid adding the same code for each of my grids. I have like 30 grids in my project and multiplied by 13 rows of code it just adds a lot of code lines to my project making it "unfriendly".
I am looking for a solution that will only add 13 lines of code to the project, not 390 lines.
My formatting code looks like this (for example):
procedure TDBGrid.DBGrid1DrawColumnCell(Sender: TObject;const Rect: TRect;DataCol: Integer;Column: TColumn;State: TGridDrawState) ;
var
grid : TDBGrid;
row : integer;
begin
grid := sender as TDBGrid;
row := grid.DataSource.DataSet.RecNo;
if Odd(row) then
grid.Canvas.Brush.Color := clSilver
else
grid.Canvas.Brush.Color := clDkGray;
grid.DefaultDrawColumnCell(Rect, DataCol, Column, State) ;
end;
Probably I would need to extend the DBGrid somehow, but I do not know exactly how nor how to look for a solution for this on google
I tried to hack the DBGRid inside each form like this:
type
TDBGrid = class(DBGrids.TDBGrid)
protected
procedure DrawColumnCell(const Rect: TRect; DataCol: Integer;Column: TColumn; State: TGridDrawState); override;
end;
...
procedure TDBGrid.DrawColumnCell(const Rect: TRect; DataCol: Integer;Column: TColumn; State: TGridDrawState) ;
var
grid : TDBGrid;
row : integer;
begin
row := 2;//grid.DataSource.DataSet.RecNo;
if Odd(row) then
Canvas.Brush.Color := clSilver
else
Canvas.Brush.Color := clDkGray;
DefaultDrawColumnCell(Rect, DataCol, Column, State) ;
end;
I can do this but I cannot access the sender, so I can access the dataset and know which record to color and which not (odd and even).
And this is a poor approach anyways since I will have to do it on every form, so it's not really a solution
Any ideas?
Thank you
If you put something like this in your datamodule, and assign it to the OnDrawColumnCell of every DBGrid, it seems to work (see notes that follow):
procedure TDataModule1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
const
RowColors: array[Boolean] of TColor = (clSilver, clDkGray);
var
OddRow: Boolean;
begin
// Safety check, although it really isn't needed; no other control accepts
// this event handler definition, AFAIK, so the only way to call it with the
// wrong Sender type would be to do so in your own code manually. In my own
// code, I'd simply leave out the check and let the exception happen; if I
// was stupid enough to do so, I'd want my hand slapped rudely.
if (Sender is TDBGrid) then
begin
OddRow := Odd(TDBGrid(Sender).DataSource.DataSet.RecNo);
TDBGrid(Sender).Canvas.Brush.Color := RowColors[OddRow];
TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end;
A couple of notes:
First, you should avoid using TDataSet.RecNo in the first place, because post-BDE datasets don't typically have this value available. Accessing it (particularly on large or query-based datasets) causes a major performance hit to your application. Of course, not using it means that you can't use this solution. A better solution would be to use a handler for the dataset's BeforeScroll or AfterScroll event that toggled a boolean available to this code instead, and use that instead of the test for Odd(RecNo), or if the dataset is only used for displaying in the DBGrid, use the TDataSet.Tag in the AfterScroll event to track the row's odd/even state using
OddRow := Boolean(DataSet.Tag);
DataSet.Tag := Ord(not OddRow);
Add DBGrids to the uses clause of your datamodule, and manually declare the above event in the published section so that it's available to all units that use the datamodule. You can then assign it in the Object Inspector Events tab as usual from those units.
This does not properly handle the TGridDrawState (nor does your initial code). You'll need to add handling for that yourself, as that wasn't what you asked here.
Depending on which color you want for odd and even rows, you may want to reverse the order of the colors in RowColors.
I prefer the repeated typecasts so that it's clear what the code is doing. If it bothers you, you can simply declare a local variable instead:
var
OddRow: Boolean;
Grid: TDBGrid;
begin
if (Sender is TDBGrid) then
begin
Grid := TDBGrid(Sender);
OddRow := Odd(Grid.DataSource.DataSet.RecNo);
...
end;
end;
This works for Delphi XE7
type
TDBGrid=Class(Vcl.DBGrids.TDBGrid)
procedure WMVScroll(var Message: TWMVScroll); message WM_VSCROLL;
end;
procedure TDBGrid.WMVScroll(var Message: TWMVScroll);
begin
Self.Invalidate;
inherited;
end;
procedure TForm1. DBGrid1MouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
if Sender is TDBGrid then
(Sender as TDBGrid).Invalidate;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
const
MyRowColors : array[Boolean] of TColor = (clLime, clMoneyGreen);
var
RowNo : Integer;
OddRow : Boolean;
S : string;
begin
if Sender is TDBGrid then begin
with (Sender as TDBGrid) do begin
if (gdSelected in State) then begin
// Farbe für die Zelle mit dem Focus
// color of the focused row
Canvas.Brush.Color := clblue;
end
else begin
// count := trunc((Sender as TDBGrid).Height div (Rect.Bottom - Rect.Top));
// RowNo := (Sender as TDBGrid).Height div Rect.Top;
RowNo := Rect.Top div (Rect.Bottom - Rect.Top);
OddRow := Odd(RowNo);
Canvas.Brush.Color := MyRowColors[OddRow];
// Font-Farbe immer schwarz
// font color always black
Canvas.Font.Color := clBlack;
Canvas.FillRect(Rect);
// Denn Text in der Zelle ausgeben
// manualy output the text
if Column.Field <> nil then begin
S := Column.Field.AsString;
Canvas.TextOut(Rect.Left + 2, Rect.Top + 1, S);
// Canvas.TextOut(Rect.Left + 2, Rect.Top + 1, 'Column.Field.AsString');
end;
end;
end
end;
end;
I am using the version of TeeChart that ships with Rad Studio XE3.
TeeChart provides a TChartSeries event which fires when the mouse pointer moves over a series line. I use this event to display the name of the series under the pointer.
The problem is, give a series line 1 pixel wide, it’s difficult to get the pointer exactly over the line. Is there some way to add ‘padding’ to the event so it fires X number of pixels to each side of the line?
Or is there some other way to accomplish this?
I'm adding a new property to Line (TLineSeries) and FastLine (TFastLineSeries) classes to accomplish this.
Series1.ClickTolerance := 4; // <-- number of pixels around mouse XY
The default value is zero (mouse XY should be exactly over the line), like the current behavior.
As a workaround, if you are using TLineSeries, pointers can be displayed at line point positions, and the internal "clicked" function will consider pointer size:
Series1.Pointer.Visible:=True;
And for more custom control, the code below is very similar to the internal code use to detect mouse clicks. The Tolerance constant specifies the number of extra pixels to consider "in the line".
procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
const
Tolerance=4;
var Clicked,
t : Integer;
Position,
P,Old : TPoint;
begin
Clicked:= -1;
Position.X:=X;
Position.Y:=Y;
for t:=Series1.FirstValueIndex to Series1.LastValueIndex do
begin
P.X:=Series1.CalcXPos(t);
P.Y:=Series1.CalcYPos(t);
if t>Series1.FirstValueIndex then
if PointInLine(Position,P.X,P.Y,Old.X,Old.Y,Tolerance) then
begin
Clicked:=t;
break;
end;
Old:=P;
end;
if Clicked = -1 then
Caption:=''
else
Caption:=IntToStr(Clicked);
end;
You can use the PointInLineTolerance function to check it at OnMouseMove event.
However, you have to loop the series points manually to transform the series values into pixels and pass them to this function.
uses Series;
procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
Chart1.View3D:=false;
for i:=0 to 5 do
Chart1.AddSeries(TLineSeries).FillSampleValues;
end;
procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var series, valueIndex: Integer;
P0, P1: TPoint;
begin
Chart1.Draw;
for series:=0 to Chart1.SeriesCount-1 do
with Chart1[series] do
for valueIndex:=FirstValueIndex to LastValueIndex-1 do
begin
P0.X:=CalcXPos(valueIndex);
P0.Y:=CalcYPos(valueIndex);
P1.X:=CalcXPos(valueIndex+1);
P1.Y:=CalcYPos(valueIndex+1);
if PointInLineTolerance(Point(X, Y), P0.X, P0.Y, P1.X, P1.Y, 5) then
begin
Chart1.Canvas.TextOut(X+5,Y-10,'Series ' + IntToStr(series));
exit;
end;
end;
end;
Previously with the default DBGrid I could alter the value of a cell without altering the data in a database with the following code.
procedure TEMRForm.DBGridCDrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Column.FieldName = 'START_DATE' then
begin
DBGridC.Canvas.FillRect(Rect);
DBGridC.Canvas.TextOut(Rect.Left+2,Rect.Top+2,Column.Field.Text + ' *');
end;
end;
Which worked great, but I am having trouble implementing this same kind of functionality on a cxgrid. Here is my current code which shows no indication of the cell value being changed.
procedure TEMRForm.cxGridCDBTableView1CustomDrawCell(
Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
ARect: Trect;
begin
ARect := AViewInfo.Bounds;
if AViewInfo.Item.Caption = 'Start Date' then
begin
ACanvas.FillRect(ARect);
ACanvas.TextOut(ARect.Left+2,ARect.Top+2,TableC.FieldByName('START_DATE').AsString+' *');
end;
end;
I think the reason why you don't see the drawning done in cxGridCDBTableView1CustomDrawCell() is because you don't set the ADone parameter to true - thus the default painting will "cancel" (overpaint) your's.
However, I think the right way to achieve what youre after is to use column's events OnGetDisplayText and OnGetContentStyle (the later event is subproperty of Styles, ie Column.Styles.OnGetContentStyle).
I have a TStringGrid with several rows in which I implemented some kind of 'read-only' row. More exactly, I don't allow the user to click the penultimate row. If the user clicks the last row nothing happens; the focus won't be moved to the cells of that row.
I have the code (in KeyDown) and everything works smooth. However, if the user clicks the top row and then uses the mouse wheel to scroll down, eventually the focus will move to the penultimate row. Any idea how to prevent this?
Well, you could override DoMouseWheelDown to achieve this.
function TMyStringGrid.DoMouseWheelDown(Shift: TShiftState;
MousePos: TPoint): Boolean;
begin
if Row<RowCount-2 then
//only allow wheel down if we are above the penultimate row
Result := inherited DoMouseWheelDown(Shift, MousePos)
else
Result := False;
end;
But how do you know that there isn't some other way to move the focus to the last row?
In fact a much better solution is to override SelectCell:
function TMyStringGrid.SelectCell(ACol, ARow: Longint): Boolean;
begin
Result := ARow<RowCount-1;
end;
When you do it this way you don't need any KeyDown code, and you don't need to override DoMouseWheelDown. All possible mechanisms to change the selected cell to the final row will be blocked by this.
As #TLama correctly points out, you don't need to sub-class TStringGrid to achieve this. You can use the OnSelectCell event:
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Longint;
var CanSelect: Boolean);
begin
CanSelect := ARow<(Sender as TStringGrid).RowCount-1;
end;
I solved this problem by putting this in the event OnMouseWheelUp:
procedure Tmainform.sgup(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
begin
sg.RowCount := sg.RowCount + 1;
sg.RowCount := sg.RowCount - 1;
end;