Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 201
Colen Avatar asked Jul 28 '09 21:07

Colen


2 Answers

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.

like image 189
Michael Madsen Avatar answered Nov 02 '22 13:11

Michael Madsen


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;
like image 25
JosephStyons Avatar answered Nov 02 '22 13:11

JosephStyons