2009-12-09

设置Default button

缺省情况下,IDOK会是Default button。也就是说,如果按回车键,则相当于点击了IDOK按钮。

需要跟Focus区分。比如,当前焦点在哪个按钮上,那么按空格键,则相当于点击焦点所在的按钮。

如果需要动态修改Default button,参考如下代码
Q.  How do I set the default button in a dialog when using MFC?

A.  Usually its the "OK" button.
This function does the trick:
int CMyDialogClass::SetDefaultButton(const DWORD nNewId)
{
    DWORD nPrevId;

    nPrevId  = GetDefID();
    // Its not automatic - we have to remove the border from the old button
    SendDlgItemMessage(nPrevId, BM_SETSTYLE, BS_PUSHBUTTON, (LPARAM)TRUE);

    SetDefID(nNewId);
    // Likewise, add a border to the new one
    SendDlgItemMessage(nNewId, BM_SETSTYLE, BS_DEFPUSHBUTTON, (LPARAM)TRUE);

    return nPrevId;
}

No comments: