Solved

Puting an image inside an edit box

Posted on 2008-06-10
6
2,146 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
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.

 

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

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

Title # Comments Views Activity
Using FastMM4... should I enable MMX option? 9 75
Communication Between RC4 Delphi <-> PHP 3 99
Help on project with Soap 10 47
Delphi Form ownership 4 70
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

929 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

19 Experts available now in Live!

Get 1:1 Help Now