MFC按钮onclick处理程序

MFC button onclick handler

本文关键字:程序 处理 onclick 按钮 MFC      更新时间:2023-10-16

我有2个mfc按钮,我想在3D stl文件加载时隐藏,然后在stl加载完成时再次显示按钮。这是为了防止用户在正在加载模型时重新加载模型。

代码片段如下

        { // change control state when model loads
            ((CButton *)this->GetDlgItem(IDC_RELOAD_STOCK_BUTTON))->ShowWindow(SW_HIDE);
            ((CButton *)this->GetDlgItem(IDOK))->ShowWindow(SW_HIDE);
            UpdateWindow();
        }
        // this process takes few seconds to load the file in the memory
        customStockModel.LoadFile(pathName.GetBuffer(pathName.GetLength()));
        {
            // change control state when the model has loaded
            ((CButton *)this->GetDlgItem(IDC_RELOAD_STOCK_BUTTON))->ShowWindow(SW_SHOW);
            ((CButton *)this->GetDlgItem(IDOK))->ShowWindow(SW_SHOW);
            UpdateWindow();
        }

按钮确实像我期望的那样隐藏并重新出现,但是当我单击隐藏的按钮区域时,当按钮控件重新出现在屏幕上时,onclick处理程序立即被调用。看起来点击消息是排队的,一旦按钮被激活,MFC调用处理程序。我哪里做错了?

理想情况下,您应该在线程中读取文件-这将允许您的应用程序正常刷新自身并处理消息。现在,如果您不确定这一点(多线程困难的),您可以定期调用length函数中的以下函数来清除消息循环。

BOOL YourClass::DoEvents()
{
    MSG msg;
    while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        if (msg.message == WM_QUIT)
        {
            return FALSE;
        }
        if (!AfxGetApp()->PreTranslateMessage(&msg))
        {
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
        }
    }
    return TRUE;
}

它会工作,但"合适的程序员"会尖叫当他们看到这个!祝你好运