0

We often see a background text on search bars i.e. "Search Here", "Type here to search" etc. and it disappears when we start typing. Have a look at the following one:

A sample of what I need

Is there any built-in method to implement the same functionality in an MFC Application for the CComboBox Control ?

Itban Saeed
  • 1,660
  • 5
  • 25
  • 38

1 Answers1

3

Use CComboBox::SetCueBanner

This sends window message CB_SETCUEBANNER. SetCueBanner is also available for CEdit. This feature needs at least Windows Vista.

This function is unicode only, it has to have to unicode text.

If you are targeting WinXP, SetCueBanner is not available and the program will not compile. But you can still send CB_SETCUEBANNER (0x1703) message:

::SendMessageW(myComboBox->m_hWnd, CB_SETCUEBANNER, 0, (LPARAM)L"Text");

This will compile and run on all systems, however it won't have any effect in Windows XP.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • 1
    It seems helpful but I am amazed that `SetCueBannar` is not available in the methods list of `CComboBox`, All that I got is `error C2039: 'SetCueBanner' : is not a member of 'CComboBox'` What am I doing wrong here? – Itban Saeed Aug 24 '15 at 09:26
  • Probably you are targeting WinXP. You can still use use this function through WinAPI message (see updated answer) – Barmak Shemirani Aug 24 '15 at 09:41
  • Thanks for the update sir, but I am using `Windows 8.1 Enterprise` – Itban Saeed Aug 24 '15 at 09:46
  • You are working on Windows 8.1, but you are targeting your app for WinXP. Try the `SendMessageW` option. – Barmak Shemirani Aug 24 '15 at 09:53
  • `SendMessageW` gave me the error `0xC000041D: An unhandled exception was encountered during a user callback.` – Itban Saeed Aug 24 '15 at 10:21
  • @ItbanSaeed: [CComboBox::SetCueBanner](https://msdn.microsoft.com/en-us/library/bb385230.aspx): *"This method is supported in Windows Vista and later. Additional requirements for this method are described in [Build Requirements for Windows Vista Common Controls](https://msdn.microsoft.com/en-us/library/bb531404.aspx)."* Check your project to see what platform you are targeting. – IInspectable Aug 24 '15 at 11:12
  • I have no idea why `SendMessage` would cause `0xC000041D` error here. If you don't have a combo box with ID = `IDC_COMBO1` then it results in unhandled exception and crash. Try `myComboBox->m_hWnd` instead of `GetDlgItem` – Barmak Shemirani Aug 24 '15 at 11:38
  • Late to the party, but perhaps @ItbanSaeed was using a multibyte character set. I had this issue and changing to unicode fixed it.. – aquirdturtle Jul 17 '16 at 23:39
  • @aquirdturtle yes, `CB_SETCUEBANNER` is Unicode only, it doesn't have ANSI version unlike many other Windows APIs – Barmak Shemirani Jul 18 '16 at 00:28
  • myComboBox->SendMessage(CB_SETCUEBANNER, 0, (LPARAM) L"Text"); – Farid Z Dec 09 '20 at 17:14