4

I'm building a custom edit control which consists of adding both an icon at the left and an icon button at the right, both inside the edit control. This requires shifting the starting point of the text (and cursor) to the right by X amount of pixels. This also means I need to 'Limit' how wide the text can be drawn too, to make room for the button on the right. The intention is to provide both a custom icon on the left, such as in a browser, as well as an 'X' button on the right to clear the contents of the edit control.

How to offset the Rect of where to draw the text and cursor in a TCustomEdit descendant?

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327

1 Answers1

6

If you are using more recent version of Delphi, there should already be a TButtonedEdit Control and can do your work.

If not, I think you can send a EM_SETMARGINS message to your TCustomEdit to set the left and right margin.

SendMessage(CustomEdit.Handle, EM_SETMARGINS, EC_LEFTMARGIN or EC_RIGHTMARGIN, MakeLong(LeftMargin, RightMargin));
Justmade
  • 1,496
  • 11
  • 16
  • That's right (+1ed), but I have to ask when you hit this topic, I've seen in many examples is used the `TWinControl` as the underlayer for the "inside" control and I'm wondering why. Is that because the edit would flicker even when you set the margins the way you shown here ? What would happen if I create for instance button and set its parent to the edit control without this underlayer ? – TLama Mar 13 '12 at 02:33
  • +1 Looks like that'll do the trick, thanks! And actually, I didn't know about the buttoned edit already in XE2, but I'm adding some more capabilities too. It's essentially going to be a search box. – Jerry Dodge Mar 13 '12 at 02:37
  • 1
    @TLama I didn't really implemented that control but I see that Delphi use the EM_SETMARGINS to implement the official version of TButtonEdit so I think it can be a valid way to implement as such. – Justmade Mar 13 '12 at 03:09
  • @JerryDodge I think it would be easier if you inherit from TCustomButtonedEdit to add your own functionality. – Justmade Mar 13 '12 at 03:11
  • 1
    @TLama, "What would happen if I create for instance button and set its parent to the edit control without this underlayer": The child button will hide that portion from the edit control. you *must* use this technique. +1 – kobik Mar 13 '12 at 15:20