hello
how to add a background
thenk you
// MY: add a new prop to SG
property Background: TPicture read fBackground write SetBackground;
// new prop's setter
procedure TSimpleGraph.SetBackground(Value: TPicture);
begin
if fBackground <> Value then
fBackground.Assign(Value);
end;
procedure TSimpleGraph.DrawBackground(Canvas: TCanvas);
var
DC: HDC;
Rect: TRect;
X, Y: Integer;
DotColor: Integer;
ClipRgn: HRGN; // MY
Bitmap: TBitmap; // MY
Graphic: TGraphic; // MY
begin
// 1st - draw background color
Canvas.Brush.Style := bsSolid;
Canvas.Brush.Color := Color;
Rect := Canvas.ClipRect;
Canvas.FillRect(Rect);
// 2nd - MY: draw background bitmap
if Background.Graphic <> nil then
begin
Graphic := Background.Graphic;
if (Graphic is TMetafile) or (Graphic is TIcon) or Graphic.Transparent then
Canvas.FillRect(Rect);
Canvas.StretchDraw(Rect, Graphic);
Canvas.Brush.Style := bsClear;
end;
// 3rd - draw grid (if enabled)
if ShowGrid then
begin
DotColor := ColorToRGB(GridColor);
DC := Canvas.Handle;
Y := 0;
while Y < Rect.Bottom do
begin
X := 0;
while X < Rect.Right do
begin
SetPixel(DC, X, Y, DotColor);
Inc(X, GridSize);
end;
Inc(Y, GridSize);
end;
end;
end;
property Background: TPicture read fBackground write SetBackground;
procedure TMainForm.SimpleGraphBeforeDraw(Graph: TSimpleGraph;
Canvas: TCanvas);
procedure DrawTile(ABitmap: TBitmap);
var
X, Y: integer;
begin
Y := 0;
while Y < SimpleGraph.VisibleBounds.Bottom do
begin
X := 0;
while X < SimpleGraph.VisibleBounds.Right do
begin
Canvas.Draw(X, Y, ABitmap);
Inc(X, ABitmap.Width);
end;
Inc(Y, ABitmap.Height);
end;
end;
begin
if chkTiledBackground.Checked then
DrawTile(Image1.Picture.Bitmap)
else
Canvas.StretchDraw(SimpleGraph.VisibleBounds,Image1.Picture.Bitmap);
end;
type
TSimpleGraphHack = class(TSimpleGraph);
procedure TMainForm.SimpleGraphBeforeDraw(Graph: TSimpleGraph;
Canvas: TCanvas);
begin
//
// Drawing the background image
//
TSimpleGraphHack(Graph).DrawGrid(Canvas);
end;
Kambiz wrote:Nice job lbc!
The gird paints before OnBeforeDraw event. When the graph has a background image, I don't see any reason for having the grid.
Anyway, using the following trick, user can draw the grid.
procedure TMainForm.SimpleGraphBeforeDraw(Graph: TSimpleGraph;
Canvas: TCanvas);
procedure DrawTile(ABitmap: TBitmap);
var
X, Y: integer;
begin
Y := 0;
while Y < SimpleGraph.VisibleBounds.Bottom do
begin
X := 0;
while X < SimpleGraph.VisibleBounds.Right do
begin
Canvas.Draw(X, Y, ABitmap);
Inc(X, ABitmap.Width);
end;
Inc(Y, ABitmap.Height);
end;
end;
begin
if chkTiledBackground.Checked then
DrawTile(Image1.Picture.Bitmap)
else
Canvas.StretchDraw(SimpleGraph.VisibleBounds,Image1.Picture.Bitmap);
end;
and it puts a mark blue on Inc(UpdateCount) in the code of simplegraphe;' Debugger Exception Notification Project MDIAPP.exe raised exception class EAccessViolation with message ' Access violation At address 0049126C in module ' MDIAPP.exe '. Write of address 00000324'. Process stopped. Use Step gold Run to continuous. OK Help '
Return to DELPHI AREA Projects
Users browsing this forum: No registered users and 3 guests