Solved

Text to be placed on canvas determined by mouse position

Posted on 2002-03-06
5
216 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
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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

22 Experts available now in Live!

Get 1:1 Help Now