Create object in Tsimplegraph

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

Create object in Tsimplegraph

Postby jacek » November 25th, 2008, 7:49 pm

Helo!

Can anybody help me to do such a thing:
I would like to create for example node which will be including the TButton or other object inside, and then will be possible to use its properties.

thank You
regards
Jacek
jacek
Member
Member
 
Posts: 1
Joined: November 25th, 2008, 8:45 am

Re: Create object in Tsimplegraph

Postby Kambiz » November 26th, 2008, 11:24 am

You just need to write handler for three events.

In the following code snippet, I've attached a button to center of each rectangular node.

Code: Select all
// create the button when a rectangular node is inserted
procedure TForm1.SimpleGraph1ObjectInsert(Graph: TSimpleGraph;
  GraphObject: TGraphObject);
var
  btn: TButton;
begin
  if GraphObject is TRectangularNode then
  begin
    // create the button
    btn := TButton.Create(nil);
    // set button's parent to simplegraph
    btn.Parent := Graph;
    // save a reference to button for recall
    GraphObject.Tag := Integer(btn);
    // position the button
    SimpleGraph1NodeMoveResize(Graph, TGraphNode(GraphObject));
  end;
end;

// destroy the button when the node is deleted
procedure TForm1.SimpleGraph1ObjectRemove(Graph: TSimpleGraph;
  GraphObject: TGraphObject);
var
  btn: TButton;
begin
  // if any button is attached
  if GraphObject.Tag <> 0 then
  begin
    // get the button
    btn := TButton(GraphObject.Tag);
    // remove reference
    GraphObject.Tag := 0;
    // free the button
    btn.Free;
  end;
end;

// reposition/resize the button when its node moved or resized
procedure TForm1.SimpleGraph1NodeMoveResize(Graph: TSimpleGraph;
  Node: TGraphNode);
var
  btn: TButton;
  Pos: TPoint;
  Size: TPoint;
begin
  // if any button is attached
  if Node.Tag <> 0 then
  begin
    // get the button
    btn := TButton(Node.Tag);
    // convert node's coordinates from graph to simplegraph
    Pos := Graph.GraphToClient(Node.Left, Node.Top);
    Size := Graph.GraphToClient(Node.Width, Node.Height);
    // Set button's size as half size of the node and center it inside the node
    Size.X := Size.X div 2;
    Size.Y := Size.Y div 2;
    Inc(Pos.X, Size.X div 2);
    Inc(Pos.Y, Size.Y div 2);
    btn.SetBounds(Pos.X, Pos.Y, Size.X, Size.Y);
  end;
end;
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm

Re: Create object in Tsimplegraph

Postby kaouane » January 12th, 2011, 9:58 pm

It's fantastic Kambiz, but it make problem when zoomed :(
kaouane
Active Member
Active Member
 
Posts: 14
Joined: January 12th, 2011, 5:02 pm
Location: Algiers, Algeria

Re: Create object in Tsimplegraph

Postby Kambiz » January 13th, 2011, 11:21 am

You should resize the associated control on OnZoomChange event too.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm

Re: Create object in Tsimplegraph

Postby kaouane » January 13th, 2011, 4:58 pm

Excuse me Kambize, but I don't know how can I do it.
If you can, please indicate me. Thank you.
kaouane
Active Member
Active Member
 
Posts: 14
Joined: January 12th, 2011, 5:02 pm
Location: Algiers, Algeria

Re: Create object in Tsimplegraph

Postby Kambiz » January 13th, 2011, 9:53 pm

Here is the code for the above example:

Code: Select all
procedure TForm1.SimpleGraph1ZoomChange(Sender: TObject);
begin
  SimpleGraph1.ForEachObject(AdjustNodeBounds, 0);
end;

function TForm1.AdjustNodeBounds(GraphObject: TGraphObject; UserData: Integer): Boolean;
begin
  if (GraphObject.Tag <> 0) and (GraphObject is TGraphNode) then
    SimpleGraph1NodeMoveResize(SimpleGraph1, TGraphNode(GraphObject));
  Result := True;
end;
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm

Re: Create object in Tsimplegraph

Postby kaouane » January 13th, 2011, 11:57 pm

Sincerely, you are a big boss;
Thank you very much Kambiz, I am very glad to be a member for your website;
kaouane
Active Member
Active Member
 
Posts: 14
Joined: January 12th, 2011, 5:02 pm
Location: Algiers, Algeria

Re: Create object in Tsimplegraph

Postby Kambiz » January 14th, 2011, 9:40 pm

Thank you!
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm


Return to DELPHI AREA Products

Who is online

Users browsing this forum: No registered users and 2 guests