caseWM_GETDLGCODE:// Call the check box window procedure firstlRet=CallWindowProc(lpCheckProc,hWnd,wMessage,wParam,lParam);// If lParam points to an MSG structureif(lParam){lpmsg=(LPMSG)lParam;if(lpmsg->message==WM_CHAR){if(lpmsg->wParam=='x'||lpmsg->wParam=='X'){// Select the check box when user presses "X"SendMessage(hWnd,BM_SETCHECK,TRUE,0);lRet|=DLGC_WANTMESSAGE;}elseif(lpmsg->wParam=='o'||lpmsg->wParam=='O'){// Clear the check box when user presses "O"SendMessage(hWnd,BM_SETCHECK,FALSE,0);lRet|=DLGC_WANTMESSAGE;}}}returnlRet;
上述代码使得Windows对X, x, O, o字符的WM_CHAR消息不会做进一步处理,因为WM_GETDLGCODE返回值包含了DLGC_WANTMESSAGE码。上例中,控件返回值也可以用DLGC_WANTCHARS代替DLGC_WANTMESSAGE。
下例代码子类化编辑框控件,禁止其通过Tab键获得输入焦点时默认地全部选中编辑框内的文本。
// In the subclass procedurecaseWM_GETDLGCODE:// Call the original edit control window procedurelRet=CallWindowProc(lpEditProc,hWnd,wMessage,wParam,lParam);// Clear the DLGC_HASSETSEL bit from the return valuelRet&=~DLGC_HASSETSEL;returnlRet;