How to set up default background (greyed out) text

2019-02-28 08:18发布

问题:

When creating a text box

hwnd = CreateWindowEx(0, "EDIT", [...])

How to set default, grey text on the background of that box, which disappears when providing some input? An example. I'm using codeblocks.

EDIT:

#include <commctrl.h>

[...]

HWND hwndEdit = CreateWindowEx(0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 10, 110, 300, 20, hwnd, NULL, hThisInstance, NULL);
SendMessage(hwndEdit, EM_SETCUEBANNER, FALSE, (LPARAM) L"Default text");

回答1:

After creating the edit control, send the EM_SETCUEBANNER message to it:

SendMessage(hwndEdit, EM_SETCUEBANNER, 0, (LPARAM)L"Default text");

Or use the Edit_SetCueBannerText macro:

Edit_SetCueBannerText(hwndEdit, L"Default text");

Either way, you also need to enable Visual Styles.