How to set background image for Edit (Delphi)
Asked Answered
S

1

7

how can i have a image for Editbox background ?

Severus answered 2/12, 2010 at 17:29 Comment(0)
M
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

Moony answered 2/12, 2010 at 19:23 Comment(3)
Why WM_CTLCOLOREDIT? It won't get called when the control is disabled. Why not WM_ERASEBACKGROUND and WM_PRINTCLIENT?Lichenology
+1 because someone have down voted for a working code ! it worksVertical
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.Lichenology

© 2022 - 2024 — McMap. All rights reserved.