views:

298

answers:

6

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.

A: 

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

AShelly
Because until someone types into it, I don't want to retrieve the text and get the default text. That would mean I'd have to add all sorts of checking for the default text, etc, which would be a pain.
Colen
A: 

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

Aric TenEyck
+1  A: 

Take a look at EM_SETCUEBANNER

Alan
+2  A: 

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.

Michael Madsen
A: 

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;
JosephStyons
Note that it's been built in to the VCL as a property called TextHint - I think it was added in D2009, although I'm not entirely sure right now.
Michael Madsen
A: 

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