Solved

Text to be placed on canvas determined by mouse position

Posted on 2002-03-06
5
215 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 100 total points
Comment Utility
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 33

Expert Comment

by:Slick812
Comment Utility
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
Comment Utility
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
Comment Utility
Canvas.Brush.Style := bsClear;
Prior to TextOut().

0
 

Author Comment

by:Vanessa
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now