Prevent double click on MFC-Dialog button

2019-06-07 00:06发布

问题:

i'm developing Autocad/Bricscad-Dialogs in MFC C++. Know i detected a bigger problem. There is a dialog which sets metadata for 'special' drawing objects. I update the data of every 'special' drawing object with this dialog (in a loop). So if you have ten 'special' drawing objects, the same dialog will open ten times (successively). Now i have the problem that the user sometimes make a double click on the "OK"-Button. But if this double click is fast enough, the "OK"-Button of the next instance of this dialog will clicked automatically. I tried a lot (for example disabling the button if it was clicked) but nothing solved my problem. Maybe someone of you have a good idea.

Best regards, Simon

回答1:

When you open a new dialog you can flush the message queue of mouse click messages before going into your normal message loop, e.g.:

MSG msg;
while (PeekMessage(&msg, hWndDlg, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE));


回答2:

I try to extend the answer of Jonathan Potter.

When you open a new dialog and OnInitDIalog is called, just remove the mouse messages from the queue and wait for 1/10 of a second.

MSG msg;
DWORD dwStart = ::GetTickCount():
while (PeekMessage(&msg, hWndDlg, WM_LBUTTONDOWN, WM_LBUTTONDOWN, PM_REMOVE)!=0 ||
       (::GetTickCount() - dwStart) < 100))
      ;

The trick with the PeekMessage will work, the problem is that you need to run the loop as long as a "double click" will take. If the clicks have a distance of 1/10th of a second you need to remove all mouse clicks for this period of time.

And also OnInitDialog is the correct position. You may extend this flush to all mouse messages WM_MOUSEFIRST/WM_MOUSELAST... to get all clicks.

The delay of 1/10 second when launching the next dialog isn't expensive or annoying.