2

When a window is disabled via EnableWindow(hwnd, FALSE), and the user clicks on it, then the "Default Beep" sound gets played. I don't want this to happen. How can I disable this behavior programatically for the current Process in C/C++ with Win32 code?

user3756504
  • 127
  • 6

1 Answers1

8

The beep is generated by the default window processing of the WM_SETCURSOR message.

From docs for WM_SETCURSOR:

If the low-order word of the lParam parameter is HTERROR and the high-order word of lParam specifies that one of the mouse buttons is pressed, DefWindowProc calls the MessageBeep function.

To prevent the beep, your window procedure should handle WM_SETCURSOR and not call DefWindowProc under those conditions.

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • Thanks for the reply. Funnily, in my runloop the breakpoint never gets hit: `MSG msg; while(GetMessage(&msg, NULL, 0, 0) > 0) { if (msg.message==WM_SETCURSOR) { int i=0; // <--- breakpoint here never gets hit! } TranslateMessage(&msg); DispatchMessage(&msg); }` – user3756504 Sep 24 '20 at 12:22
  • 3
    @user3756504 *WM_SETCURSOR message is **sent** to a window* - you need handle this in window procedure. not in message loop – RbMm Sep 24 '20 at 12:38
  • It's the same in my window procedure: WM_SETCURSOR is never received. BTW it's perfectly possible to do it as I did above, because the message loop dispatches to the window procedure. I can see the message with Spy++, but it never shows up in my Window Procedure or Message Loop, which is a baffling mistery. I have the feeling it has something to do with the fact that the window is disabled. – user3756504 Sep 24 '20 at 15:00
  • 2
    @user3756504 some messages is returned by `GetMessage` or `PeekMessage` and you need call `DispatchMessage` for pass it to window procedure. another messages **direct** passed to window procedure by system and not returned by `Get[Peek]Message` - you never view it it message loop. `WM_SETCURSOR` direct passed to window procedure. even if it disabled. so some error in your code or you use wrong window – RbMm Sep 24 '20 at 15:30
  • I stand corrected, indeed it works in window procedure. So the code to get rid of the MessageBeep now is: `if (msg==WM_SETCURSOR && LOWORD(lParam)==WORD(HTERROR) && IsWindowEnabled(hwnd)==FALSE) {return TRUE;}` Thanks for the help! – user3756504 Sep 24 '20 at 16:38