API编程,DialogBox中的控件消息处理,如EditBox的WM_CHAR消息,WM_PASTE消息,ComboBox的CBN_SELCHANGE消息和EditBox的EN_CHANGE 消息,示例代码如下:
//1。创建 Dialog
short ShowActivationDlg(HWND hParent)
{
LRESULT lRet = 0;
lRet = DialogBox(g_hinstThisDll, MAKEINTRESOURCE(IDD_DIALOG_ACTIVATE), hParent, (DLGPROC) DlgActivationProc);
return (short)lRet;
}
//2。Dialog 消息处理函数
LRESULT CALLBACK DlgActivationProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return InitActivationDialog(hWnd);
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDOK:
EndDialog(hWnd, IDOK);
return IDOK;
case IDCANCEL:
EndDialog(hWnd, IDCANCEL);
return IDCANCEL;
case IDC_CMB_LOCAL: //ComboBox control
if(HIWORD(wParam) == CBN_SELCHANGE)
{
HWND hComboLocal = GetDlgItem(hWnd, IDC_CMB_LOCAL);
int nCurSel = SendMessage(hComboLocal, CB_GETCURSEL, 0, 0);
}
break;
case IDC_EDIT_TEXT: //EditBox control
if(HIWORD(wParam) == EN_CHANGE)
{
HWND hEditEmailWnd = GetDlgItem(hWnd, IDC_EDIT_TEXT);
//Add your code here...
}
}
}
}
return 0L;
}
//3。初始化控件处理函数
BOOL InitActivationDialog(HWND hWnd)
{
//...
//Set new process function of EditBox
hChildWnd = GetDlgItem(hWnd, IDC_EDIT_TEXT);
g_EditProcOrig = (WNDPROC)SetWindowLong(hChildWnd, GWL_WNDPROC, (LONG)EditProcNew);
}
//4。控件处理函数
LRESULT CALLBACK EditProcNew(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PASTE:
return DlgDoEditPaste(hWnd);
case WM_CHAR:
{
g_bIsPasteMessage = FALSE;
char sInfo[1024];
memset(sInfo, 0, 1024);
GetWindowText(hWnd, sInfo, 1024);
int nEditTextLength = 4;
int nStartSel = 0;
int nEndSel = 0;
SendMessage(hWnd, EM_GETSEL, (WPARAM)(LPDWORD)&nStartSel, (LPARAM)(LPDWORD)&nEndSel);
LRESULT h = CallWindowProc(g_EditProcOrig, hWnd, message, wParam, lParam);
char sInfo2[1024];
memset(sInfo2, 0, 1024);
GetWindowText(hWnd, sInfo2, 1024);
if(strlen(sInfo2) > nEditTextLength)
{
SetWindowText(hWnd, sInfo);
SendMessage(hWnd, EM_SETSEL, nStartSel, nEndSel);
}
else
{
memset(sInfo, 0, 1024);
strcpy(sInfo, sInfo2);
}
if(strlen(sInfo) >= nEditTextLength)
{
HWND hNextWnd = GetWindow(hWnd, GW_HWNDNEXT);
if(::SendMessage(hNextWnd, WM_GETDLGCODE, 0, 0L) & DLGC_WANTCHARS)
{
if(!g_bIsPasteMessage)
SetFocus(hNextWnd);
}
}
return 0;
}
default : break;
}
return CallWindowProc(g_EditProcOrig, hWnd, message, wParam, lParam);
}
//5。Edit 粘贴消息处理
BOOL DlgDoEditPaste(HWND hWnd)
{
g_bIsPasteMessage = TRUE;
if(!(::SendMessage(hWnd, WM_GETDLGCODE, 0, 0L) & DLGC_WANTCHARS))
return FALSE;
char szActivationKey[STRINGLENGTH];
memset(szActivationKey, 0, STRINGLENGTH);
char szSnippet[ID_VALUE_LENGTH];
memset(szSnippet, 0, ID_VALUE_LENGTH);
OpenClipboard(hWnd);
HANDLE hText = GetClipboardData(CF_TEXT);
CloseClipboard();
int nEditTextLength = 40;
if(hText != NULL)
{
if(strlen((char *)GlobalLock(hText)) >= nEditTextLength)
memcpy(szActivationKey , (char *)GlobalLock(hText), nEditTextLength-1);
else
strcpy(szActivationKey , (char *)GlobalLock(hText));
GlobalUnlock(hText);
}
nEditTextLength = 4;
//string is like this 1234-5678-abcd-efgh-ijkl-mnop-qrst-uvwx
CParseString oParseString(szActivationKey);
char cReturn = '-';
LPSTR lpszHeader = oParseString.GetNextSubString(cReturn, szSnippet);
while (lpszHeader)
{
if(strlen(szSnippet) > 0)
{
if(strlen(szSnippet) > nEditTextLength)
szSnippet[nEditTextLength] = '\0';
do
{
if (SendMessage(hWnd, WM_GETDLGCODE, 0, 0L) & DLGC_WANTCHARS)
{
SetFocus(hWnd);
SetWindowText(hWnd, szSnippet);
SendMessage(hWnd, EM_SETSEL, lstrlen(szSnippet), lstrlen(szSnippet));
memset(szSnippet, 0, ID_VALUE_LENGTH);
hWnd = GetWindow(hWnd, GW_HWNDNEXT);
break;
}
hWnd = GetWindow(hWnd, GW_HWNDNEXT);
} while(hWnd != NULL && !(GetWindowLong(hWnd, GWL_STYLE) & WS_GROUP));
if(hWnd == NULL || (GetWindowLong(hWnd, GWL_STYLE) & WS_GROUP))
break;
}
lpszHeader = oParseString.GetNextSubString(cReturn, szSnippet);
}
}