?
Solved

Delphi paintbox

Posted on 2007-10-04
12
Medium Priority
?
4,486 Views
Last Modified: 2013-11-23
Hi  guys

I'm making a line graph application using delphi. I draw a line and ellipse in the paintbox. My question is how can i identify if the mouse over is in the ellipse. I want to show a hint and the text is not fix. Meaning every ellipse has a different hint text. I place the record in a list. So it would be best if an id would also included in the ellipse. Is this possible?

Hope you guys get it.
0
Comment
Question by:tirbanqs
  • 7
  • 4
12 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 20012821
maybe

instead of drawing place TShapes

just as an idea

meikl ;-)

0
 

Author Comment

by:tirbanqs
ID: 20012847
nice idea.
is it ok if i have plenty of tshapes. i have plenty of ellipse on the chart.
0
 
LVL 17

Expert Comment

by:mokule
ID: 20012915
Normally You could do it like this

var
  h: hrgn;
begin
 h := CreateEllipticRgn(10,10,50,50);
 if PtInRegion(h,mouse.CursorPos.x,mouse.CursorPos.Y) then
   begin
   ...
   end;
 DeleteObject(h);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:tirbanqs
ID: 20012955
But how can i add an id or any identification on it?
The hint text are on the list.

btw, how can i add your in the paintbox...
0
 
LVL 17

Expert Comment

by:mokule
ID: 20012982
You should have a list ellipsis coordinates and their identifiers.

This code can be in OnMouseMove event.
0
 

Author Comment

by:tirbanqs
ID: 20013004
Can you give me an example.
I'm new to this programming stuff.
I really don't know how to implement your code... sorry
0
 

Author Comment

by:tirbanqs
ID: 20013114
I'll revise my question so it will be easier to understand.

how can i identify that the mouse cursor is over the ellipse? and how can i put an id or identification for each ellipse? because each ellipse hint has different values/text.

help!!! :)
0
 
LVL 17

Accepted Solution

by:
mokule earned 375 total points
ID: 20013132
Ok
don't be so nervous.
Give me a while.
here is  full code of a simple example

pas file
-------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TMyEllipse = record
    Ident, Left, Top, Right, Bottom: integer;
    end;
  PTMyEllipse = ^TMyEllipse;
  TForm1 = class(TForm)
    PaintBox1: TPaintBox;
    Button1: TButton;
    Label1: TLabel;
    procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure PaintBox1Paint(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    List: TList;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  i: integer;
  h: hrgn;
  e: TMyEllipse;
begin
  for i := list.Count-1 downto 0 do
    begin
    e := TMyEllipse(List.Items[i]^);
    h := CreateEllipticRgn(e.Left,e.Top,e.Right,e.Bottom);
    if ptInRegion(h,X,Y) then
      begin
      label1.Caption := IntToStr(e.Ident);
      DeleteObject(h);
      exit;
      end;
    DeleteObject(h);
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Item: PTMyEllipse;
  n: integer;
begin
  New(Item);
  n := List.Count+1;
  Item^.Ident := n;
  Item^.Left := 10*n;
  Item^.Top := 10*n;
  Item^.Right := 30*n;
  Item^.Bottom := 20*n;
  List.Add(Item);
  PaintBox1.Refresh;
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
var
  i: integer;
  e: TMyEllipse;
begin
  PaintBox1.Canvas.Brush.Color := clYellow;
//  PaintBox1.Canvas.Brush.Style := bsClear;
  PaintBox1.Canvas.Pen.Color := clGreen;
  for i := 0 to list.Count-1 do
    begin
    e := TMyEllipse(List.Items[i]^);
    PaintBox1.Canvas.Ellipse(e.Left,e.Top,e.Right,e.Bottom);
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  List := TList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  list.Free;
end;

end.
-------------
and dfm file
-------------
object Form1: TForm1
  Left = 234
  Top = 88
  Width = 870
  Height = 640
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object PaintBox1: TPaintBox
    Left = 16
    Top = 20
    Width = 425
    Height = 373
    OnMouseMove = PaintBox1MouseMove
    OnPaint = PaintBox1Paint
  end
  object Label1: TLabel
    Left = 460
    Top = 56
    Width = 32
    Height = 13
    Caption = 'Label1'
  end
  object Button1: TButton
    Left = 460
    Top = 20
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
end
0
 

Author Comment

by:tirbanqs
ID: 20019472
Thank you mokule and meikl.
0
 

Author Comment

by:tirbanqs
ID: 20019498
Btw mokule, is it advisable to dispose the pointer when destroying the list? or when freeing the list, is the memory allocated freed?
0
 
LVL 17

Expert Comment

by:mokule
ID: 20020079
You're right. You should dispose pointers before list free.
The correct way is:

procedure TForm1.FormDestroy(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to List.Count-1 do
    Dispose(List.Items[i]);
  List.Free;
end;
0
 

Author Comment

by:tirbanqs
ID: 20020097
Thank you mokule.
I implemented your code in my program. It works perfectly.
Thanks again.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question