單行編輯控件具備ES_密碼樣式。默認狀況下,具備此樣式的編輯控件爲用戶鍵入的每一個字符顯示一個星號。app
可是,本例使用EM_SETPASSWORDCHAR消息將默認字符從星號更改成加號(+)。如下屏幕截圖顯示用戶輸入密碼後的對話框。函數
下面的C++代碼示例使用DealBox函數建立一個模態對話框。對話框模板IDD_PASSWORD做爲參數傳遞。它定義了「密碼」對話框的窗口樣式、按鈕和尺寸。blog
DialogBox(hInst, // application instance MAKEINTRESOURCE(IDD_PASSWORD), // dialog box resource hWnd, // owner window PasswordProc // dialog box window procedure );
如下示例中的窗口過程初始化「密碼」對話框並處理通知消息和用戶輸入。初始化期間,窗口過程將默認密碼字符更改成+號,並將默認按鈕設置爲取消。string
在用戶輸入處理期間,只要用戶在編輯控件中輸入文本,窗口過程就會將默認按鈕從「取消」更改成「肯定」。it
若是用戶按下「肯定」按鈕,窗口過程將使用EM_LINELENGTH_和EM_GETLINE消息來檢索文本。io
INT_PTR CALLBACK PasswordProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { TCHAR lpszPassword[16]; WORD cchPassword; switch (message) { case WM_INITDIALOG: // Set password character to a plus sign (+) SendDlgItemMessage(hDlg, IDE_PASSWORDEDIT, EM_SETPASSWORDCHAR, (WPARAM) '+', (LPARAM) 0); // Set the default push button to "Cancel." SendMessage(hDlg, DM_SETDEFID, (WPARAM) IDCANCEL, (LPARAM) 0); return TRUE; case WM_COMMAND: // Set the default push button to "OK" when the user enters text. if(HIWORD (wParam) == EN_CHANGE && LOWORD(wParam) == IDE_PASSWORDEDIT) { SendMessage(hDlg, DM_SETDEFID, (WPARAM) IDOK, (LPARAM) 0); } switch(wParam) { case IDOK: // Get number of characters. cchPassword = (WORD) SendDlgItemMessage(hDlg, IDE_PASSWORDEDIT, EM_LINELENGTH, (WPARAM) 0, (LPARAM) 0); if (cchPassword >= 16) { MessageBox(hDlg, L"Too many characters.", L"Error", MB_OK); EndDialog(hDlg, TRUE); return FALSE; } else if (cchPassword == 0) { MessageBox(hDlg, L"No characters entered.", L"Error", MB_OK); EndDialog(hDlg, TRUE); return FALSE; } // Put the number of characters into first word of buffer. *((LPWORD)lpszPassword) = cchPassword; // Get the characters. SendDlgItemMessage(hDlg, IDE_PASSWORDEDIT, EM_GETLINE, (WPARAM) 0, // line 0 (LPARAM) lpszPassword); // Null-terminate the string. lpszPassword[cchPassword] = 0; MessageBox(hDlg, lpszPassword, L"Did it work?", MB_OK); // Call a local password-parsing function. ParsePassword(lpszPassword); EndDialog(hDlg, TRUE); return TRUE; case IDCANCEL: EndDialog(hDlg, TRUE); return TRUE; } return 0; } return FALSE; UNREFERENCED_PARAMETER(lParam); }