回车键不触发IDOK默认按钮动作 [英] Enter key does not trigger IDOK Default Push Button action

查看:271
本文介绍了回车键不触发IDOK默认按钮动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CDialog派生类。它的界面定义有几个图片框和一些按钮,在资源文件中定义为:
$ b $ pre $ ID $ C $ IDD_SELECT_ITEMS DIALOGEX 0,0,462, 274
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTIONSelect
FONT 8,MS Shell Dlg,0,0,0,0
BEGIN
CONTROL,IDC_ITEM1,Static,SS_BLACKFRAME,13, 18,59,52
// ...
CONTROL,IDC_ITEM18,Static,SS_BLACKFRAME,373,178,59,52
LTEXTSelect,IDC_STATIC,7,256,40, 8
PUSHBUTTONAll,IDC_ALL,47,253,50,14
PUSHBUTTONNone,IDC_NONE,101,253,50,14
PUSHBUTTONFilter ...,IDC_FILTER,155,253,60 ,14
DEFPUSHBUTTONOK,IDOK,353,253,50,14
PUSHBUTTONCancel,IDCANCEL,405,253,50,14
END

在初始化窗体时,按下回车键不会触发与IDOK按钮关联的操作,因为IDC_ALL按钮处于焦点状态,Enter会执行操作,这是不可取的。

在其初始化例程中,

  BOOL CSelectDialog :: OnInitDialog()
{
CDialog :: OnInitDialog();
// ...
返回TRUE; //返回TRUE,除非你将焦点设置为一个控件
// EXCEPTION:OCX属性页面应该返回FALSE

code $ pre

我试着把所有的重点放在IDOK按钮上,但没有成功。为了避免交替评论和取消注释,我补充说:

  BOOL CSelectParts :: OnInitDialog()
{
SendDlgItemMessage(IDOK,DM_SETDEFID,0);

CDialog :: OnInitDialog();
// ...

this-> SetDefID(IDOK);

GetDlgItem(IDOK) - > SendMessage(BN_SETFOCUS,0,0);

SendDlgItemMessage(IDOK,WM_SETFOCUS,0,0);
SendDlgItemMessage(IDOK,BN_SETFOCUS,0,0);
SendDlgItemMessage(IDC_ALL,WM_KILLFOCUS,0,0);

SendDlgItemMessage(IDOK,DM_SETDEFID,0,0);
:: SetFocus(GetDlgItem(IDOK) - > GetSafeHwnd());

GetDlgItem(IDOK) - > SetFocus();
返回TRUE; //返回TRUE,除非你将焦点设置为一个控件
// EXCEPTION:OCX属性页面应该返回FALSE

code $ pre

我得到的越接近,使用
$ b

 $ $ $ SendDlgItemMessage(IDOK,WM_SETFOCUS, 0,0); 

做了一件非常奇怪的事情:它在两个按钮上绘制了一个焦点矩形, image:



但是回车键还没有触发默认的按钮!

我甚至尝试添加一个OnOK方法

pre> void CSelectDialog :: OnOK()
{
__super :: OnOK();

$ / code>

没有成功!

  ON_COMMAND(IDOK,OnOK)$ b $另外一个实验:我添加了一个入口到类的消息映射b  

仍然不成功!

如何设置Enter键做IDOK按钮的动作?

解决方案

解决方案正好在我眼前留下评论,完全是盲目的,没有看到它!



看到 WM_INITDIALOG 消息 a href =https://msdn.microsoft.com/en-us/library/windows/desktop/ms644995%28v=vs.85%29.aspx =nofollow>对话框编程注意事项我知道我必须返回FALSE。

我删除了所有我添加的垃圾行,将返回值更改为 FALSE ,并且所有的东西都按预期正常工作:)

I have a CDialog derived class. Its interface definition has several picture boxes and some buttons after, defined in resource file as:

IDD_SELECT_ITEMS DIALOGEX 0, 0, 462, 274
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Select"
FONT 8, "MS Shell Dlg", 0, 0, 0x0
BEGIN
    CONTROL         "",IDC_ITEM1,"Static",SS_BLACKFRAME,13,18,59,52
 //...
    CONTROL         "",IDC_ITEM18,"Static",SS_BLACKFRAME,373,178,59,52
    LTEXT           "Select",IDC_STATIC,7,256,40,8
    PUSHBUTTON      "All",IDC_ALL,47,253,50,14
    PUSHBUTTON      "None",IDC_NONE,101,253,50,14
    PUSHBUTTON      "Filter ...",IDC_FILTER,155,253,60,14
    DEFPUSHBUTTON   "OK",IDOK,353,253,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,405,253,50,14
END

When I init the form, pressing the Enter Key does not trigger the action associated to the IDOK button, because the IDC_ALL button is focused and Enter does its action, which is undesirable.

In its initialization routine,

BOOL CSelectDialog::OnInitDialog() 
{
    CDialog::OnInitDialog();
//...
    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}

I tried everything to put the focus on the IDOK button, but no success. To the point of getting tired of alternating the commenting and uncommenting the lines I added:

BOOL CSelectParts::OnInitDialog() 
{
    SendDlgItemMessage(IDOK, DM_SETDEFID, 0);

    CDialog::OnInitDialog();
//  ...

    this->SetDefID(IDOK);

    GetDlgItem(IDOK)->SendMessage(BN_SETFOCUS, 0, 0);

    SendDlgItemMessage(IDOK, WM_SETFOCUS, 0, 0);
    SendDlgItemMessage(IDOK, BN_SETFOCUS, 0, 0);
    SendDlgItemMessage(IDC_ALL, WM_KILLFOCUS, 0, 0);

    SendDlgItemMessage(IDOK, DM_SETDEFID, 0,0);
    ::SetFocus(GetDlgItem(IDOK)->GetSafeHwnd());

    GetDlgItem(IDOK)->SetFocus();
    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}

The closer I got, was by using the line

    SendDlgItemMessage(IDOK, WM_SETFOCUS, 0, 0);

which did a very strange thing: it drawed a focus rectangle on two buttons as you can see in the image:

but the Enter key was not yet triggering the Default Push Button!

I even tried to add a OnOK method

void CSelectDialog::OnOK()
{
    __super::OnOK();
}

No success yet!

One more experiment: I added an entry to the class's message map:

ON_COMMAND(IDOK, OnOK)

Still unsuccessful!

How can I manage to put the Enter key to do the IDOK button's action?

解决方案

The solution was right in front of my eyes in a comment, and I was completely blind, not seeing it!

After seeing the end of the "The WM_INITDIALOG Message" section on Dialog Box Programming Considerations, I understood I had to return FALSE.

I eliminated all the trashy lines I added, changed the return value to FALSE, and everything began to work fine as expected :)

这篇关于回车键不触发IDOK默认按钮动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆