Prevent double click on MFC-Dialog button

给你一囗甜甜゛ 提交于 2019-12-10 11:36:16

问题


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.



来源:https://stackoverflow.com/questions/31653383/prevent-double-click-on-mfc-dialog-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!