Dear all, I found the solution and I like to share it with you. I took the idea from an article I have found on your site, http://www.codeproject.com/cpp/cppforumfaq.asp#ui\_workerthread I implemented this code within the button handler and... wonder!, it works. OnButtonClicked() { m_crtlButton->Enable(FALSE); while (Do_A_Bit_Of_Work()) { MSG msk; while(PeekMessage(&msg, NULL, NULL, PM_NOREMOVE) { AfxGetApp()->PumpMessage(); } } m_crtlButton->Enable(TRUE); return; } Ciao and thanks for your kind help. Daniele
Daniele Godi
Posts
-
Disabling dialog button from within the ON_BN_CLICKED handler -
Disabling dialog button from within the ON_BN_CLICKED handlerHi all, I have a problem. I need to start a long computing work when the user clicks a dialog button. During this time the button needs to be disabled. The following simple code doesn't work. Despite the fact that the button changes state from enable to disabled (grayed), it actually gets the mouse inputs. Some of you knows the reason? I wasn't able to disable this button and then I had to write a workaround. Thanks, Daniele Godi /////////////////////////////////////////////////////////////// Sample code void CMyDlg::OnBnClickedButton1() { // disable the button m_ctrlButton1.EnableWindow(FALSE); BeginWaitCursor(); DoSomeLongWork(); EndWaitCursor(); // workaround, I have to remove manually the mouse input from // the message queue if (::GetInputState()) { MSG msg; while ( ::PeekMessage( &msg, NULL, NULL, NULL, PM_NOREMOVE ) ) { // found a mouse message, remove it if ((msg.message >= WM_MOUSEFIRST) && (msg.message <= WM_MOUSELAST)) { ::PeekMessage( &msg, NULL, NULL, NULL, PM_REMOVE); } else { if ( !::AfxGetThread()->PumpMessage() ) { // if FALSE exit both dialog and application EndDialog(FALSE); ::PostQuitMessage( -1 ); return; } } } } m_ctrlButton1.EnableWindow(TRUE); return; } Daniele Godi