none
SetFoucs设置焦点问题 RRS feed

  • 问题

  • 按Button1之后,使用SetFocus给Button2按钮设置焦点,Button2按钮有焦点虚线框了,但是Button1还有蓝色的边框,这是什么原因哪?如何能取消Button1的蓝色边框?只设置SetFocus不可以吗?

    2013年1月30日 13:42

答案

  • 解决了,贴出来解决方法。

    Do not call SetFocus in a dialog window, except in WM_INITDIALOG

    Note: Do not call SetFocus to change a control's focus in a dialog window, except when handling the WM_INITDIALOG message.

    To explicitly set the focus to a dialog's control call SendMessage(hDialog, WM_NEXTDLGCTL, (WPARAM)hwndCtrl, TRUE). The WM_NEXTDLGCTL message notifies the dialog manager that the focus has changed.

    The exception is the initial WM_INITDIALOG message. When you receive WM_INITIDIALOG call SetFocus on the desired control's hWnd. You must return FALSE to notify the dialog manager that you set the initial focus.

    When handling other dialog messages if you use SetFocus (and not WM_NEXTDLGCTL) it prevents the dialog manager from updating its internal bookkeeping. For example, it might cause the dialog manager to display the wrong default pushbutton or fail to select the text of an edit control. For details see Raymond Chen's book The Old New Thing, page 227, "How to set focus in a dialog box." (Or see his blog.)
    • 已标记为答案 hchack 2013年1月31日 12:32
    2013年1月31日 12:32

全部回复

  • 解决了,贴出来解决方法。

    Do not call SetFocus in a dialog window, except in WM_INITDIALOG

    Note: Do not call SetFocus to change a control's focus in a dialog window, except when handling the WM_INITDIALOG message.

    To explicitly set the focus to a dialog's control call SendMessage(hDialog, WM_NEXTDLGCTL, (WPARAM)hwndCtrl, TRUE). The WM_NEXTDLGCTL message notifies the dialog manager that the focus has changed.

    The exception is the initial WM_INITDIALOG message. When you receive WM_INITIDIALOG call SetFocus on the desired control's hWnd. You must return FALSE to notify the dialog manager that you set the initial focus.

    When handling other dialog messages if you use SetFocus (and not WM_NEXTDLGCTL) it prevents the dialog manager from updating its internal bookkeeping. For example, it might cause the dialog manager to display the wrong default pushbutton or fail to select the text of an edit control. For details see Raymond Chen's book The Old New Thing, page 227, "How to set focus in a dialog box." (Or see his blog.)
    • 已标记为答案 hchack 2013年1月31日 12:32
    2013年1月31日 12:32
  • 你好,

    感谢你在这里分享你的解决方案,不过这里是中文论坛,如果能尽量用中文描述会更加方便其他人阅读。谢谢!


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年2月1日 2:39
    版主