7

how can i have a image for Editbox background ?

Kermia
  • 4,171
  • 13
  • 64
  • 105

1 Answers1

15

This is very possible, indeed. In your form, define

private
  { Private declarations }
  FBitmap: TBitmap;
  FBrush: HBRUSH;
protected
  procedure WndProc(var Message: TMessage); override;      

and do

procedure TForm1.FormCreate(Sender: TObject);
begin
  FBitmap := TBitmap.Create;
  FBitmap.LoadFromFile('C:\Users\Andreas Rejbrand\Pictures\AS20Utv.bmp');
  FBrush := 0;
  FBrush := CreatePatternBrush(FBitmap.Handle);
end;

and

procedure TForm1.WndProc(var Message: TMessage);
begin
  inherited;
  case Message.Msg of
    WM_CTLCOLOREDIT, WM_CTLCOLORSTATIC:
      if (Message.LParam = Edit1.Handle) and (FBrush <> 0) then
      begin
        SetBkMode(Message.WParam, TRANSPARENT);
        Message.Result := FBrush;
      end;
  end;
end;

Of course you can wrap this into a component of your own, say TEditEx. If I get time over, I might do this. (And, notice that there is no need to buy an expensive (and maybe not that high-quality) component pack from a third-party company.)

Custom edit background

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • 2
    Why WM_CTLCOLOREDIT? It won't get called when the control is disabled. Why not WM_ERASEBACKGROUND and WM_PRINTCLIENT? – David Heffernan Dec 02 '10 at 20:04
  • +1 because someone have down voted for a working code ! it works – Vibeeshan Mahadeva Dec 03 '10 at 04:11
  • To be clear WM_CTLCOLOREDIT is sent when the control is Enabled, WM_CTLCOLORSTATIC when it is disabled. Probably wise to grey scale (or similar) your background for the latter to make it clear that the control will not accept input. I see that Andreas has now edited the code to handle WM_CTLCOLORSTATIC as well as WM_CTLCOLOREDIT. – David Heffernan Dec 03 '10 at 20:46