views:

870

answers:

4

How could I prevent esc from closing a dialog box?

I searched for this topic, but all I found was for MFC (You can overwrite PreTranslateMessage function in MFC). but my program is written in Windows API, not MFC.

I tried to catch all Keyboard messages in Dialog procedure, but none of them works. I also tried using subclassing in dialog box, but it didn't work too.

How could I do that? Thanks.

+9  A: 

Handle WM_COMMAND and check for IDCANCEL. Then do not pass the control to the default window procedure.

Ivan Krechetov
A: 

It's a Win32 api FAQ for many years. See Google Groups : 7 000 hits...

A: 

But IDCANCEL is used in 3 places, when user hit the cancel button, hit the system menu, or press esc. They all send IDCANEL message. How could I know the user acturally press esc key or hit the cancel button?

A: 

You can determine whether it's from the system menu easily enough -- check out the "return value" section of this MSDN page to see how.

If you can't determine whether it's from the ESCAPE key or a button, you could always get around that by using a different identifier for your Cancel button.

Head Geek