TCustomEdit text indentation lazarus / 31-LCL ---------------------------------------------------------------------- scribly · 2013-10-23 18:38 Does anyone know what the best way is to make a TCustomEdit descendant that has no indentation between the start of the control (0,0) and the outputed text? Or is there a way to get the amount of indentation? I'm asking because some of my code uses canvas.textrect to output some text, and I'd like to have an borderless editbox right after that at the same height. (Even with borderstyle set to bsNone the text starts after a small offset) I know I can just adjust the top and left of the edit by a specific amount I can gather from my system, but I'm sure that is a really bad idea (I already tried centering the textrect output by the height of the edit but then the height is still off by 1 pixel) ---------------------------------------------------------------------- howardpc · 2013-10-23 21:15 With edits you're constrained by the way the OS widget is implemented. If all you want is to display text (no editing required) some improvement on this basic skeleton component will suffice: Code: [Select]unit borderless_text; {$mode objfpc}{$H+} interface uses   Classes, Controls, types; type   { TBorderlessText }   TBorderlessText=class(TGraphicControl)  protected    class function GetControlClassDefaultSize: TSize; override;    procedure Paint; override;  public    constructor Create(anOwner: TComponent);  published    property Text;  end; implementation { TBorderlessText } class function TBorderlessText.GetControlClassDefaultSize: TSize; begin   Result.cy:=15;   Result.cx:=200; end; constructor TBorderlessText.Create(anOwner: TComponent); begin   inherited Create(anOwner);   with GetControlClassDefaultSize do     SetInitialBounds(Left, Top, cx, cy); end; procedure TBorderlessText.Paint; begin   Canvas.Brush.Color:=Color;   Canvas.FillRect(ClientRect);   Canvas.TextOut(0,0,Text);   inherited Paint; end;     end. ---------------------------------------------------------------------- scribly · 2013-10-23 21:45 It's specifically for editing. I have a custom drawn listview, and when the 3th column is clicked i want to go into edit mode. I do that by creating an TEdit, but i was wondering if i could make it look better by ditching that border, giving it a correct background and make it look like it belongs in the row I could of course implement a full text editor that does look and works good (copy/paste, context menu, key handling) but i was hoping a custom edit might save a lot of time on something not very important (the editing and other parts work fine, this is just a matter of aesthetics) ---------------------------------------------------------------------- howardpc · 2013-10-23 22:04 Quote from: scribly on October 23, 2013, 08:38:23 pmI know I can just adjust the top and left of the edit by a specific amount I can gather from my system, but I'm sure that is a really bad idea I don't think a TCustomEdit having a negative offset of a few pixels in either direction is a bad idea unless it starts to clip other text, which is unlikely with bsNone. Try it and see! ---------------------------------------------------------------------- engkin · 2013-10-24 01:36 Windows has an edit message called EM_POSFROMCHAR. Use it to ask your TCustomEdit about the first letter. Maybe something like this Code: [Select]var   xyCombined: DWord;   xOffset, yOffset:Word; begin   xyCombined := SendMessage(YourEdit.Handle, EM_POSFROMCHAR, 0, 0);   xOffset := xyCombined and $FFFF;   yOffset := xyCombined shr 16; I believe the distance you are asking about is related to the chosen font and its size and whether a border is included or not. If my assumption is correct, you only need to send that message once and every time a change related to the font occurs. Edit: A better way using another edit message EM_GETRECT Code: [Select]var   textRect: TRect;   xOffset, yOffset: Word; begin   SendMessage(YourEdit.Handle, EM_GETRECT, 0, Integer(@textRect));   xOffset := textRect.Left;   yOffset := textRect.Top;