add a background to simplegraph

Please post bug reports, feature requests, or any question regarding the DELPHI AREA projects here.

add a background to simplegraph

Postby Mirage » November 15th, 2005, 9:27 am

hello
how to add a background
thenk you
User avatar
Mirage
Junior Member
Junior Member
 
Posts: 44
Joined: October 26th, 2005, 11:41 am

Postby lbc » November 15th, 2005, 10:50 am

Hello Mirage
i managed to add a graph background property in a previous version of simplegraph (1.57)
unfortunately I haven't had the time to insert into the new SG versions (basically, as far as i can see. now there isn't anymore the simplegraph.drawbackround procedure for example)

anyway hope that helps as a general guideline to add a graph background picture

Code: Select all
// 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;

lbc
Junior Member
Junior Member
 
Posts: 48
Joined: February 4th, 2004, 7:50 am
Location: Italy

Postby Kambiz » November 15th, 2005, 1:24 pm

OnBeforeDraw event can be used for this purpose. VisibleBounds property determines which portion of the graph should be painted.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Postby Mirage » November 15th, 2005, 3:55 pm

Hello

Thank you Ibc

I have a problem when I add this code:
Code: Select all

property Background: TPicture read fBackground write SetBackground;



it returns me:

[Error] SimpleGraph.pas(794): Field or method identifier expected

Thanks in advance[/code]
User avatar
Mirage
Junior Member
Junior Member
 
Posts: 44
Joined: October 26th, 2005, 11:41 am

Postby lbc » November 15th, 2005, 4:03 pm

Hi Mirage
you are welcome
Have you added the new property to the published section of SimpleGraph (given, as i said, it's the old version 1.57 as then some change has been inserted in the draw procedures) and the procedure SetBackground(Value: TPicture) in the private section?

private
....
procedure SetBackground(Value: TPicture);
....

published
....

property Background: TPicture read fBackground write SetBackground;

...

Anyway now I think it's much better doing as Kambiz has kindly suggested, that is using the OnBeforeDraw event (and stretching the picture inside the VisibleBounds)
lbc
Junior Member
Junior Member
 
Posts: 48
Joined: February 4th, 2004, 7:50 am
Location: Italy

Postby lbc » November 16th, 2005, 5:57 pm

Hello
I have exprimented a little how to add a background (one single bitmap or many bitmap "tiled together") using the OnBeforeDraw event
here is my code
(chkTiledBackground is a checkbox that makes the background tiled, Image1 is a simple image dropped on the main form and with visible = false)


Code: Select all
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;


For Kambiz:
it seems to me that the grid isn't visible anymore (see pictures below) even with ShowGrid = true. Maybe it would be useful to show the grid over the background.

hope this helps
Attachments
da_bkg.jpg
Stretched Background
da_bkg.jpg (66.97 KiB) Viewed 5650 times
da_bkg_tiled.jpg
Tiled Background
da_bkg_tiled.jpg (91.69 KiB) Viewed 5649 times
lbc
Junior Member
Junior Member
 
Posts: 48
Joined: February 4th, 2004, 7:50 am
Location: Italy

Postby Kambiz » November 16th, 2005, 7:42 pm

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.

Code: Select all
type
  TSimpleGraphHack = class(TSimpleGraph);

procedure TMainForm.SimpleGraphBeforeDraw(Graph: TSimpleGraph;
  Canvas: TCanvas);
begin
  //
  // Drawing the background image
  //
  TSimpleGraphHack(Graph).DrawGrid(Canvas);
end;
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Postby lbc » November 17th, 2005, 8:22 am

Kambiz wrote:Nice job lbc!


thank you :oops: :D

The gird paints before OnBeforeDraw event. When the graph has a background image, I don't see any reason for having the grid.

I personally agree with you, but maybe (with reference for example to the simpegraph demo) a user could be wondering why enabling/disabling the grid doens't work anymore when there is a background
another situation that comes into my mind is for example when you put a "pale" image into the background just for "decorative purposes" and maybe still having the grid could be appreciated

Anyway, using the following trick, user can draw the grid.



Excellent trick, thank you
so this solves the above for anyone interested in showing the grid over the bkgnd
:D
lbc
Junior Member
Junior Member
 
Posts: 48
Joined: February 4th, 2004, 7:50 am
Location: Italy

Postby Mirage » November 18th, 2005, 10:02 am

Hello
Thank you Ibc and Kambiz

I have to add the TMainForm.SimpleGraphBeforeDraw(Graph procedure: TSimpleGraph; Canvas: TCanvas); But nothing occurs, the image is not to post. Is what it is necessary to add property Background: TPicture read fBackground Write SetBackground; for the simplegraph
User avatar
Mirage
Junior Member
Junior Member
 
Posts: 44
Joined: October 26th, 2005, 11:41 am

Postby lbc » November 18th, 2005, 11:03 am

Hello Mirage
no need for the background property anymore (at least if you are using the OnBeforeDraw event.

in order to get the backgrund shown review/follow these steps:

- drop a Timage on your form
- set the picture property to a BITMAP on your harddisk
- set its Visibile property to false (else also that image would be displayed)
- if you can't display correctly set the Transparent property of the TImage to false
- drop a checkbox and name it "chkTiledBackground" (this is needed to swithc between tiled and streched background)

- now insert the following code associated with the OnBeforeDraw event:

Code: Select all
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;

now you should see the background like in the previous pictures

enjoy :)
lbc
Junior Member
Junior Member
 
Posts: 48
Joined: February 4th, 2004, 7:50 am
Location: Italy

Postby Mirage » November 18th, 2005, 2:42 pm

thank you for your Ibc assistance

Please I have small a problem when I call an image with the simplegraph it walks well.
I added a simple program for the image processing.
When I insert this image on SimpleGraph, it does not accept this image treated.
and it appears a message
' 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 '
and it puts a mark blue on Inc(UpdateCount) in the code of simplegraphe;
What means all that. Thank you in advance
User avatar
Mirage
Junior Member
Junior Member
 
Posts: 44
Joined: October 26th, 2005, 11:41 am

Postby lbc » November 18th, 2005, 4:21 pm

Hi Mirage
as far as i can see it seems an access violation due to an attempt to write in some forbidden address, or it could also be related to the fact your are using a MDI application (more "child windows" open inside one "main" window)

Just as an idea:

- try to work with a SDI application (1 single window only)

- is the image processed by your other program a standard Bitmap (.BMP)?

Infact in the sample code above I have considered puttinga bitmap in the TImage.picture property
lbc
Junior Member
Junior Member
 
Posts: 48
Joined: February 4th, 2004, 7:50 am
Location: Italy

How can I add a big picture to background?

Postby Anonymous » May 17th, 2006, 3:06 am

How can I add a big picture to background? then I can scroll it,thanks
Anonymous
 


Return to DELPHI AREA Projects

Who is online

Users browsing this forum: No registered users and 3 guests

cron