【代码】 #include <Windows.h> #include "resource.h"
#include <CommCtrl.h> #pragma comment(lib, "comctl32.lib") #pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
BOOL bEmpty = TRUE;
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; int wmId, wmEvent; switch (uMsg) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); switch (wmId) { case IDOK: case IDCANCEL: EndDialog(hDlg, wmId); break; case IDC_EDIT1: if (wmEvent == EN_SETFOCUS && bEmpty) // 当获得焦点时 { bEmpty = FALSE; // 保证输入的文字不为灰色 SetWindowTextA((HWND)lParam, ""); } else if (wmEvent == EN_KILLFOCUS) // 当失去焦点时 { bEmpty = (GetWindowTextLength((HWND)lParam) == 0); if (bEmpty) SetDlgItemTextA(hDlg, IDC_EDIT1, "请输入用户名"); } break; } break; case WM_CTLCOLOREDIT: if (bEmpty && GetDlgCtrlID((HWND)lParam) == IDC_EDIT1) { hdc = (HDC)wParam; SetTextColor(hdc, RGB(128, 128, 128)); // 设置文字颜色 return (INT_PTR)GetSysColorBrush(COLOR_WINDOW); // 文本框背景为默认的白色 } break; } return 0; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc); return 0; }
|