Hello
Is there a way to type into a label?
Thank you
Hello
Is there a way to type into a label?
Thank you
ZitatIs there a way to type into a label?
The TWPRichTextLabel does not have a window, it is just a TGraphic control. So it does not have any focus handling and also does not capture Keyboard events.
Since typing is usually not required into a label there is no clipboard handling either.
BUT - it has the powerful Cursor object which is also used by the TWPRichText and TWPCustomRTFEdit class.
And this makes it actually possible and quite easy to implement the ability to type into a TWPRichText label.
You only need a control which captures the keyboard events. With property KeyPreview = true this can also be a TForm.
You need a PaintBox to paint the caret since the label does not do this. The PaintBox is the top most element.
And You need some event handler.
The demo project can be downloaded here:
http://www.wpcubed.com/ftp/ex/type_in_RTF_label.zip
It is based on this:
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
var WPTools_Label : TWPRichTextLabel;
begin
WPTools_Label := WPRichTextLabel1; // The label which has the "Focus"
// -------------------------------------
WPTools_Label.InputString(Key, true);
WPTools_Label.ReformatAll(true);
WPTools_Label.Height :=
MulDiv(WPTools_Label.TextHeight, WPScreenPixelsPerInch, WPTools_Label.CurrentYPixelsPerInch)+32;
WPTools_Label.Width :=
MulDiv(WPTools_Label.TextWidth, WPScreenPixelsPerInch, WPTools_Label.CurrentXPixelsPerInch)+32;
WPTools_Label.Repaint;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var WPTools_Label : TWPRichTextLabel;
begin
WPTools_Label := WPRichTextLabel1; // The label which has the "Focus"
// -------------------------------------
case Key of
VK_LEFT: WPTools_Label.c_move(wpmCLeft, ssShift in SHift);
VK_UP: WPTools_Label.c_move(wpmCUp, ssShift in SHift);
VK_DOWN: WPTools_Label.c_move(wpmCDown, ssShift in SHift);
VK_RIGHT: WPTools_Label.c_move(wpmCRight, ssShift in SHift);
VK_DELETE: WPTools_Label.InputString(#127, true);
else exit;
end;
Key := 0;
Invalidate;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
WPRichTextLabel1.Cursor.RTFData := WPRichTextLabel1.BodyText;
DoubleBuffered := TRUE;
// Initialization to make it work
KeyPreview := TRUE;
PaintBox1.BringToFront;
PaintBox1.Enabled := false; // Dont want mouse events
end;
// The paintbox is the top moste graphic control
procedure TForm1.PaintBox1Paint(Sender: TObject);
var X,Y,W,H : Integer;
WPTools_Label : TWPRichTextLabel;
begin
WPTools_Label := WPRichTextLabel1; // The label which has the "Focus"
// -------------------------------------
WPTools_Label.GetCursorXYWH(X,Y,W,H);
inc(X, WPTools_Label.Left);
inc(Y, WPTools_Label.Top);
PaintBox1.Canvas.Brush.Color := clBlack;
PaintBox1.Canvas.FillRect(Rect(X,Y,X+W,Y+H));
end;
procedure TForm1.WPRichTextLabel1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
aRTFData: TWPRTFDataBlock;
par: TParagraph;
pos_in_par: Integer;
FrameBorder: TWPFrameBorder;
TextObject: TWPTextObj;
SizerElement: TWPPaintSizerRects;
PageHoverLink: TObject;
WPTools_Label : TWPRichTextLabel;
begin
WPTools_Label := (Sender as TWPRichTextLabel);
WPTools_Label.GetRTFPositionAtXY(0,
MulDiv(x, WPTools_Label.CurrentXPixelsPerInch, WPScreenPixelsPerInch ),
MulDiv(y, WPTools_Label.CurrentYPixelsPerInch, WPScreenPixelsPerInch ),
aRTFData, par, pos_in_par,
FrameBorder, TextObject, SizerElement, PageHoverLink, false, []);
if par<>nil then
begin
if ssShift in Shift then
begin
WPTools_Label.Cursor.SelectText := true;
end else
begin
WPTools_Label.Cursor.SelectText := false;
WPTools_Label.Cursor.HideSelection;
end;
WPTools_Label.Cursor.MoveTo(par, pos_in_par);
Invalidate;
end;
end;
Alles anzeigen
To improve the quality of the text use
WPRichTextLabel1.FormatOptions := [wpfAlwaysFormatWithScreenRes];