Solved

Puting an image inside an edit box

Posted on 2008-06-10
6
2,122 Views
Last Modified: 2012-06-27
I would like to insert a small picture <icon> inside an edit box to give a nicer edit box and indicate the function of the edit box. I have attached a picture that represent this. Can anyone kindly provide code to that this.

I have tried setting the edit box as a parent to a timage but that doesn't work.

Note that typing inside the textbox starts just where the image ends i.e. typing doesn't occur above the image.

Thanks.
picinsideeditbox.JPG
0
Comment
Question by:venussc
6 Comments
 
LVL 4

Expert Comment

by:tobjectpascal
ID: 21749361
Might be easier to not use TEdit and use something else to do it instead like Richedit and insert the image then the text, either that or overlay an edit box, kinda messy.
0
 

Author Comment

by:venussc
ID: 21749720
Any other suggestions Please?
0
 
LVL 14

Expert Comment

by:SteveBay
ID: 21750145
You will need a custom component. This discusion show a couple of ways to do it.
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_10200599.html
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:venussc
ID: 21752458
Thanks for the link!! Will have a look at it and give feedback later!
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 250 total points
ID: 21754217
Source code provided below

Regards,
Russell

----

unit EditEx;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

////////////////////////////////////////////////////////////////////////////////
//   TEditEx
////////////////////////////////////////////////////////////////////////////////
type
  TEditEx           =  class(TEdit)
  private
     // Private declarations
     FSize:         Integer;
     FPicture:      TPicture;
     FImage:        TImage;
  protected
     // Protected declarations
     procedure      Change; override;
     procedure      CreateWnd; override;
     procedure      PictureChanged(Sender: TObject);
     procedure      SetMargin;
     procedure      SetPicture(Value: TPicture);
     procedure      WMKillFocus(var Message: TMessage); message WM_KILLFOCUS;
     procedure      WMSetFocus(var Message: TMessage); message WM_SETFOCUS;
     procedure      WMSize(var Message: TWMSize); message WM_SIZE;
  public
     // Public declarations
     constructor    Create(AOwner: TComponent); override;
     destructor     Destroy; override;
  published
     // Published declarations
     property       Anchors;
     property       AutoSelect;
     property       AutoSize;
     property       BiDiMode;
     property       BorderStyle;
     property       CharCase;
     property       Color;
     property       Constraints;
     property       Ctl3D;
     property       DragCursor;
     property       DragKind;
     property       DragMode;
     property       Enabled;
     property       Font;
     property       HideSelection;
     property       ImeMode;
     property       ImeName;
     property       MaxLength;
     property       OEMConvert;
     property       ParentBiDiMode;
     property       ParentColor;
     property       ParentCtl3D;
     property       ParentFont;
     property       ParentShowHint;
     property       PasswordChar;
     property       PopupMenu;
     property       ReadOnly;
     property       ShowHint;
     property       TabOrder;
     property       TabStop;
     property       Text;
     property       Visible;
     property       OnChange;
     property       OnClick;
     property       OnContextPopup;
     property       OnDblClick;
     property       OnDragDrop;
     property       OnDragOver;
     property       OnEndDock;
     property       OnEndDrag;
     property       OnEnter;
     property       OnExit;
     property       OnKeyDown;
     property       OnKeyPress;
     property       OnKeyUp;
     property       OnMouseDown;
     property       OnMouseMove;
     property       OnMouseUp;
     property       OnStartDock;
     property       OnStartDrag;
     // Published extensions
     property       Picture: TPicture read FPicture write SetPicture;
  end;

procedure Register;

implementation

//// TEditEx ///////////////////////////////////////////////////////////////////
constructor TEditEx.Create(AOwner: TComponent);
begin

  // Perform inherited
  inherited Create(AOwner);

  // Set control style
  ControlStyle:=ControlStyle + [csReplicatable];

  // Set defaults
  FSize:=0;
  FImage:=TImage.Create(Self);
  FImage.Transparent:=True;
  FImage.Stretch:=True;
  FImage.Center:=True;
  FImage.Parent:=Self;
  FImage.SetBounds(0, 0, 0, Height);
  FPicture:=TPicture.Create;
  FPicture.OnChange:=PictureChanged;

end;

destructor TEditEx.Destroy;
begin

  // Resource protection
  try
     // Free the image
     FImage.Free;
     // Cleanup
     FPicture.Free;
  finally
     // Perform inherited
     inherited Destroy;
  end;

end;

procedure TEditEx.Change;
begin

  // Perform inherited
  inherited;

  // Repaint
  FImage.Repaint;

end;

procedure TEditEx.SetPicture(Value: TPicture);
begin

  // Assign from passed value
  FPicture.Assign(Value);

end;

procedure TEditEx.PictureChanged(Sender: TObject);
begin

  // Load the image
  FImage.Picture.Assign(FPicture);

  // Update the margins in case image was cleared
  SetMargin;

end;

procedure TEditEx.WMKillFocus(var Message: TMessage);
begin

  // Perform inherited
  inherited;

  // Repaint
  FImage.Repaint;

end;

procedure TEditEx.WMSetFocus(var Message: TMessage);
begin

  // Perform inherited
  inherited;

  // Repaint
  FImage.Repaint;

end;

procedure TEditEx.WMSize(var Message: TWMSize);
begin

  // Perform inherited
  inherited;

  // Update the size
  FSize:=Message.Height;

  // Set the left size margin
  SetMargin;

end;

procedure TEditEx.CreateWnd;
begin

  // Perform inherited
  inherited;

  // Set the left size margin
  SetMargin;

end;

procedure TEditEx.SetMargin;
begin

  // Resource protection
  try
     // Check state
     if (FSize = 0) or (FPicture.Graphic = nil) or FPicture.Graphic.Empty then
        // Clear border
        Perform(EM_SETMARGINS, EC_LEFTMARGIN, MakeLong(0, 0))
     else
        // Set left size border
        Perform(EM_SETMARGINS, EC_LEFTMARGIN, MakeLong(FSize + 4, 0));
  finally
     // Set the image bounds
     FImage.SetBounds(1, 1, FSize - 2, FSize - 2);
  end;

end;

procedure Register;
begin

  // Register the component in the Additional tab
  RegisterComponents('Additional', [TEditEx]);

end;

end.
0
 

Expert Comment

by:klompen
ID: 23974692
There is a bug in this TEditEx component.

Here is how to reproduce the bug:

- Create a TEditEx object in a form
- Load that small picture (16x16 pixels)
- Then click the edit area so it get focused
- Type a long text until the text scrolled inside the edit area
- Now, use mouse ... point to the right-most side of the edit area
- press and hold the left mouse (so the text will be selected if you move the mouse)
- then move the mouse to the far far left

You will see that the small image will be sort of cleared ...
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

15 Experts available now in Live!

Get 1:1 Help Now