이 섹션의 예제에서는 창 프로시저를 사용 하 여 암호를 입력 하 라는 메시지를 표시 하는 대화 상자를 생성 하는 방법을 보여 줍니다.
암호 대화 상자에서 단일 줄 편집 컨트롤에 ES_PASSWORD 스타일입니다. 기본적으로 사용자가 스타일 표시 각 문자에 대 한 별표를 사용 하 여 컨트롤을 편집 합니다. 그러나이 예제에서는, 별표에서 더하기 기호 (+) 기본 문자를 변경 하려면 EM_SETPASSWORDCHAR 메시지를 사용.
이 창 프로시저 기본 누름 단추에서에서 변경 취소 확인 하자마자 편집 컨트롤에 텍스트를 입력 합니다. 창 프로시저 EM_LINELENGTH 및 EM_GETLINE 메시지를 사용 하 여 텍스트를 검색 하려면 사용자가 확인 단추를 누를 경우.
LRESULT CALLBACK PassProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
CHAR 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,
"Too many characters.",
"Error",
MB_OK);
EndDialog(hDlg, TRUE);
return FALSE;
}
else if (cchPassword == 0)
{
MessageBox(hDlg,
"No characters entered.",
"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,
"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);
}