Colen
Colen

Reputation: 13918

How do I use 'placeholder text' in a win32 edit control?

Take a look at the top-right of the Stack Overflow site. The search box has some text in it saying "search". When you click within it, the text disappears.

I want to do something similar to this - if a win32 edit control is empty (i.e. has no text), I want to paint some text inside it, in a more subdued color than the normal text. If the control has the focus, or if there's text inside it, I don't want to paint that.

Is there any way I can do it without setting the actual text into the control and changing the text color? Maybe by intercepting the control paint or something?

Thanks.

Upvotes: 9

Views: 6665

Answers (6)

marc
marc

Reputation:

You don't need owner-drawn, it's native with User apis (Banner) See Winapi grp for samples (in C)

Upvotes: 0

JosephStyons
JosephStyons

Reputation: 58765

Thanks for this question, I will be able to use this in the future. FWIW (not much, probably), here is an implementation in Delphi:

procedure TForm1.FormShow(Sender: TObject);
const
  ECM_FIRST = $1500;
  EM_SETCUEBANNER = ECM_FIRST + 1;
begin
  SendMessage(edt.Handle,EM_SETCUEBANNER,0,LParam(PWideChar(WideString('Enter search here'))));
end;

Upvotes: 3

Michael Madsen
Michael Madsen

Reputation: 55009

It is possible as of XP. Check the EM_SETCUEBANNER message. However, there are certain issues that make it not work entirely as it should on XP, so it's best if you're dealing with Vista.

If you need it for Win2k or older versions, you'll need to do it yourself, at least on those platforms.

Upvotes: 10

Alan
Alan

Reputation: 13731

Take a look at EM_SETCUEBANNER

Upvotes: 1

Aric TenEyck
Aric TenEyck

Reputation: 8032

One possibility: Make it owner-draw, and manually paint the text onto it if the .Text property is empty.

Upvotes: 1

AShelly
AShelly

Reputation: 35580

Maybe, but why not just set the default text and color as needed, and clear it with an 'onClick' event?

Upvotes: 0

Related Questions