在对话框中有EDIT、BUTTON控件,在对话框过程函数passwordDlgProc()中:
WM_INITDIALOG:
         //设置对话框中EDIT控件可输入的字符数目为5个
SendDlgItemMessage(hDlg,IDC_EDIT1,EM_SETLIMITTEXT,(WPARAM)8,(LPARAM)0);
//把取消按钮设置为默认按钮
         SendMessage(hDlg,DM_SETDEFID,(WPARAM)IDCANCEL,(LPARAM)0); SetFocus(GetDlgItem(hDlg,IDC_EDIT1));
return FALSE;
//return(TRUE);//模式对话框此处必须返回TRUE
WM_COMMAND:
         ...............
如果WM_INITDIALOG返回值为TRUE,SetFocus()函数不起作用。
如果WM_INITDIALOG返回值为FALSE,SetFocus()函数起作用。其中TabOrder是:IDOK,1;IDCANCEL,2;IDC_EDIT1,3
将IDC_EDIT1的TabOrder更改为1,可以不使用SetFocus()函数请各位指导一下是何原因?如何在不更改TabOrder,使SetFocus()函数起作用?

解决方案 »

  1.   

    SetFocus总是起作用的。
    问题在于WM_INITDIALOG的消息如果返回TRUE,Windows就会重新设置焦点,所以原先的SetFocus就没用了。
    自己SetFocus的话就不要返回True。
      

  2.   

    不返回True对Dialog没有影响吗?
      

  3.   

    你认为会有什么影响?对话框创建失败?
    不会的,MSDN说得够清楚了The dialog box procedure should return TRUE to direct the system to set the keyboard focus to the control specified by wParam. Otherwise, it should return FALSE to prevent the system from setting the default keyboard focus. 
      

  4.   

    Modal dialog boxes are created using DialogBox, while Modeless dialog boxes are created using CreateDialog.
      

  5.   

    楼主似乎被误导了。MSDN上还有补充的一点说明,The control to receive the default keyboard focus is always the first control in the dialog box that is visible, not disabled, and that has the WS_TABSTOP style. 
    //这就是你说的“将IDC_EDIT1的TabOrder更改为1,可以不使用SetFocus()函数”的原因
    When the dialog box procedure returns TRUE, the system checks the control to ensure that the procedure has not disabled it. If it has been disabled, the system sets the keyboard focus to the next control that is visible, not disabled, and has the WS_TABSTOP. An application can return FALSE only if it has set the keyboard focus to one of the controls of the dialog box. 
      

  6.   

    经过查阅msdn以上问题得到解决,谢谢各位!