共用方式為


如何建立單行編輯控件

本主題示範如何建立包含單行編輯控件的對話框。

單行編輯控制項具有 ES_PASSWORD 樣式。 根據預設,使用此樣式編輯控件會針對使用者輸入的每個字元顯示星號。 不過,這個範例會使用 EM_SETPASSWORDCHAR 訊息,將預設字元從星號變更為加號 (+)。 下列螢幕快照顯示使用者輸入密碼之後的對話框。

screen shot of a dialog box containing an edit control for entering a password

注意

Comctl32.dll第 6 版無法轉散發。 若要使用第 6 版Comctl32.dll,請在指令清單中指定它。 如需指令清單的詳細資訊,請參閱 啟用可視化樣式

 

您需要知道的事項

技術

必要條件

  • C/C++
  • Windows 使用者介面程序設計

指示

步驟 1:建立密碼對話框的實例。

下列 C++ 程式代碼範例會使用 DialogBox 函式來建立強制回應對話方塊。 對話框範本 IDD_PASSWORD 會當做參數傳遞。 它定義密碼對話框的視窗樣式、按鈕和維度等。

DialogBox(hInst,                   // application instance
    MAKEINTRESOURCE(IDD_PASSWORD), // dialog box resource
    hWnd,                          // owner window
    PasswordProc                    // dialog box window procedure
    );

步驟 2:初始化對話框並處理用戶輸入。

下列範例中的視窗程式會初始化密碼對話框,並處理通知訊息和用戶輸入。

在初始化期間,視窗程式會將默認密碼字元變更為 + 符號,並將預設推播按鈕設定為 Cancel

在使用者輸入處理期間,視窗程式會在使用者輸入編輯控件中的文字時,將預設的按鈕從 CANCEL 變更為 [確定]。 如果使用者按下 [確定] 按鈕,視窗程式會使用EM_LINELENGTHEM_GETLINE訊息來擷取文字。

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); 
}

關於編輯控制件

編輯控制件參考

使用編輯控制件

編輯控制件