Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Cancel button using PeekMessage - am I doing this wrong?

180 views
Skip to first unread message

Paul N

unread,
Jan 4, 2012, 4:44:05 PM1/4/12
to
I have a program to search through files. It can take quite a while
and so I have it call the following routine before each file, to give
a cancel button:

int checkbreak(HWND hWnd, int& st) {
MSG msg;

/*
while(PeekMessage(&msg, hWnd, WM_COMMAND, WM_COMMAND, PM_REMOVE))
if (LOWORD(msg.wParam) == IDD_CANCELBUTTON) return TRUE; */

while(PeekMessage(&msg, NULL, WM_KEYDOWN, WM_KEYDOWN, PM_REMOVE))
if (LOWORD(msg.wParam) == VK_ESCAPE) { st = TRUE; return TRUE; }

if (PeekMessage(&msg, hCancel, WM_LBUTTONDOWN, WM_LBUTTONDOWN,
PM_REMOVE)) { st = TRUE; return TRUE; }
if (PeekMessage(&msg, hCancel, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE))
SetCursor(ArrowCursor);
else if (PeekMessage(&msg, hWnd, WM_MOUSEMOVE, WM_MOUSEMOVE,
PM_REMOVE)) SetCursor(WaitCursor);
return FALSE;
}

(hWnd is the application main window, and hCancel is a handle to a
push button with Id IDD_CANCELBUTTON.)

This seems to do what I want - it allows the user to click on the
button or to press the ESC key to cancel. And the cursor changes to an
arrow over the button, as you can press it, whereas it is an hourglass
everywhere else, to show the program is busy.

It doesn't seem right, however. I would have expected the correct test
to be for a WM_COMMAND showing BN_CLICKED, but my original attempt
(commented out above) didn't seem to respond to the button being
clicked. WM_CHAR seems the correct message to respond to, rather than
WM_KEYDOWN. And when, on one attempt, I had the last two lines as:

if (PeekMessage(&msg, hWnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE))
SetCursor(WaitCursor);
if (PeekMessage(&msg, hCancel, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE))
SetCursor(ArrowCursor);

(ie opposite order and no else) the arrow cursor did not seem to
appear at all, despite that test being later.

So I would welcome any advice on how I ought to be doing it.

Many thanks.
Paul.

David Lowndes

unread,
Jan 4, 2012, 5:01:23 PM1/4/12
to
>I have a program to search through files. It can take quite a while
>and so I have it call the following routine before each file, to give
>a cancel button:
>
>int checkbreak(HWND hWnd, int& st) {
>...
>So I would welcome any advice on how I ought to be doing it.

I'd recommend that you put your long running search code into a worker
thread and leave the UI thread to handle the cancel button.
When the button is pressed it can set a flag that the worker thread
examines periodically - similar (but simpler) than what you have now.

Dave
0 new messages