Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Text to be placed on canvas determined by mouse position

Posted on 2002-03-06
5
Medium Priority
?
230 Views
Last Modified: 2010-04-04
At runtime the user will input some text into  edit1. Then the user must click a button to embed the text into  paintbox1 which then saves as a bitmap to be stored in the db.

The question I ask is how to place the text into the paintbox - based on where the mouse cursor is positioned by the user. (Really Really want something like the textbox found in word with out the frame)

The user could place many diferent lines of text on the same image.

Below is some of the code I am currently messing around with.

procedure TForm1.Button2Click(Sender: TObject);
var  X, Y: Integer;
     MousePt : TPoint;
begin
     Application.ProcessMessages;
     Sleep(200);
     UnDoBmp.Assign(PB5Bmp);
MousePt := Point(PaintBox1.ClientRect.Right div 2, PaintBox1.ClientRect.Bottom div 2);

     RectWidth := (Edit1.Width div 2);
     RectHeight := (Edit1.Height div 2);

     MousePt := PaintBox1.ClientToScreen(MousePt);
     Mouse.CursorPos := MousePt;

     DragRect := Rect(MousePt.X - RectWidth, MousePt.Y - RectHeight,  MousePt.X - RectWidth + (RectWidth * 2), MousePt.Y - RectHeight + (RectHeight * 2));

     PaintBox1.Cursor := crDrag;
     PaintBox1.Canvas.Rectangle(DragRect);
     Texting := True

end;



On paintbox1click

if Texting then //this is set as boolean in public dec
     begin
          begin
          PaintBox1.Canvas.Rectangle(DragRect);
         // Paintbox1.Canvas.TextOut(Width div 2, Height div 2, edit1.Text);// This line works but places in the middle of paintbox therefore no good for me//
          Paintbox1.Canvas.TextOut(DragRect.Left, DragRect.Top,edit1.text);
          end;
          paintbox1.Cursor := crdefault;
          Texting := false;
  end;


please please help

V
0
Comment
Question by:Vanessa
  • 2
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
zebada earned 400 total points
ID: 6845531
I have written a small paint program - it does not have the text feature but if you add the following lines of code to the TMDIChild.doTextDown method it will do what you want.

You can download the source code here:
http://www.blacky.co.nz/free/paint.zip

Change this method in the "Child" unit.

procedure TMDIChild.doTextDown(X,Y: integer);
var
  t: string;
begin
  t := InputBox('Enter text to be displayed','Text:','');
  if ( t='' ) then
    exit;
  img.Picture.Bitmap.Canvas.TextOut(X,Y,t);
end;

0
 
LVL 34

Expert Comment

by:Slick812
ID: 6854148
hello, you use the OnClick event of the paint box, but this does NOT give you the mouse position, you can use the MouseDown or MouseUp event to get the cursor position


procedure TForm1.PaintBox3MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Texting then
  begin
  PaintBox1.Canvas.Font.Color := clRed;
  Paintbox1.Canvas.TextOut(X, Y,edit1.text);
  Texting := False;
  end;
end;

Does this help? Let me know if you need more.
0
 

Author Comment

by:Vanessa
ID: 6858189
Slick812 and Zebada
Both of you have been very helpful. THANKS.
One more question -  the text appears on the canvas with a grey background 90% of the time.  Is there something I can do to stop this altogether ?

V

0
 
LVL 6

Expert Comment

by:zebada
ID: 6858207
Canvas.Brush.Style := bsClear;
Prior to TextOut().

0
 

Author Comment

by:Vanessa
ID: 6858220
Thanks Zebada

One question, the text on the canvas has a grey background 90% of the time. Can you suggest a solution.

V
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Integration Management Part 2
Suggested Courses

916 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