使用 CFileDialog::AddCheckButton 失败

using CFileDialog::AddCheckButton fails

好的,我正在尝试使用 CFileDialog::AddCheckButton。函数调用成功,我可以看到新的复选框。我看不到任何事件,虽然我可以覆盖 OnInitDialog,但忽略覆盖 OnOK。我不确定我做错了什么:

//header

class CTPSaveDialog : public CFileDialog
{
    DECLARE_DYNAMIC(CTPSaveDialog)
    static const CString CTPSaveDialog::m_cstrFilter;
public:
    BOOL m_bForce;
    CTPSaveDialog(
        LPCTSTR lpszDefExt = NULL,
        LPCTSTR lpszFileName = NULL,
        DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
        CWnd* pParentWnd = NULL,
        DWORD dwSize = 0);
    ~CTPSaveDialog();
    virtual BOOL OnInitDialog();
    DECLARE_MESSAGE_MAP()
    afx_msg void OnBnClickedCheckForce();
    virtual void OnOK();
};

// 实施

const CString CTPSaveDialog::m_cstrFilter = "JPEG images (*.jpg)|*.jpg|TIFF Format (*.tif)|*.tif|Windows Bitmap (*.bmp)|*.bmp|Portable Network Graphics (*.png)|*.png|GIF (*.gif)|*.gif||";

IMPLEMENT_DYNAMIC(CTPSaveDialog, CFileDialog)

CTPSaveDialog::CTPSaveDialog(LPCTSTR lpszDefExt, LPCTSTR lpszFileName, DWORD dwFlags, CWnd * pParentWnd, DWORD dwSize) :
    CFileDialog(FALSE, lpszDefExt, lpszFileName, dwFlags, m_cstrFilter, pParentWnd, dwSize, TRUE)
{
    AddCheckButton(IDC_CHK_FORCE, "Force", FALSE);
    m_bForce = FALSE;
    m_ofn.lpstrTitle = "Write Simulation To File";
}

CTPSaveDialog::~CTPSaveDialog()
{
}


BOOL CTPSaveDialog::OnInitDialog()
{
    CFileDialog::OnInitDialog();

    if (GetDlgItem(IDC_CHK_FORCE))
        SendDlgItemMessage(IDC_CHK_FORCE, BM_SETCHECK, m_bForce ? BST_CHECKED : BST_UNCHECKED);
    // TODO:  Add extra initialization here
    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}

BEGIN_MESSAGE_MAP(CTPSaveDialog, CFileDialog)
    ON_BN_CLICKED(IDC_CHK_FORCE, &CTPSaveDialog::OnBnClickedCheckForce)
END_MESSAGE_MAP()

void CTPSaveDialog::CTPSaveDialog()
{
    m_bForce = !m_bForce;
}

void CTPSaveDialog::OnOK()
{
    // TODO: Add your specialized code here and/or call the base class

    CFileDialog::OnOK();
}

CFileDialog Vista 风格中,windows 消息不在消息映射中处理。相反 CFileDialog 使用特定的虚函数。你只需要声明和定义这些功能。

使用OnCheckButtonToggled检测复选框是否被点击。

使用 OnFileNameOK 检测何时选择了文件并单击了 Open/Save 按钮。

使用SetCheckButtonState来set/unset勾选按钮(不是SendDlgItemMessage

有关所有可用方法,请参阅 CFileDialog

如文档中所述,也不支持 OnInitDialog

Some CFileDialog methods are not supported under Windows Vista or later. Check the individual method topic for information about whether the method is supported. In addition, the following inherited functions are not supported under Windows Vista or later:

CDialog::OnInitDialog
...

只需在构造函数中或调用 DoModal() 之前进行初始化,并覆盖这些函数:

class CTPSaveDialog : public CFileDialog
{
    ...
    virtual void OnCheckButtonToggled(DWORD dwIDCtl, BOOL bChecked);
    virtual BOOL OnFileNameOK();
};

void CTPSaveDialog::OnCheckButtonToggled(DWORD dwIDCtl, BOOL bChecked)
{
    if (dwIDCtl == IDC_CHK_FORCE)
        TRACE("Is checked? %d\n", bChecked);
}

BOOL CTPSaveDialog::OnFileNameOK()
{
    TRACE("Clicked Open/Save button\n");

    //return FALSE to close the dialog
    return FALSE;
}