views:

83

answers:

2

I have a dynamically created toolbar on a plain Win32 dialog. My buttons are added with & shortcuts which correctly puts underscore to characters following ampersand but pressing Alt+(char) causes a beep and the button is not clicked.

It has been a while since I have done Win32 API development. Is there something that needs to be done to a dynamically created child window (toolbar) in order for the accelerator keys to work?

This could be something really obvious that I am missing...

+1  A: 

The beep indicates that the command isn't handled by any window in your app.

Since you created the toolbar dynamically, I would guess that the toolbar window isn't set up properly as a child window of your main window (i.e., it's parent and owner window are not set).

To test: click on the toolbar so it has the focus, then press Alt- and it should work.

Stefan
A toolbar is created as a child window of a dialog. Thus a parent *IS* set. As for owner, I have never heard of a child window needing owner. I have used Spy++ to check other child windows (created from a dialog template) and none have the owner set. As for focus, even when the toolbar has the focus, Alt+ does not work. Basically, when I tab to a toolbar and a button is focused, pressing SPACE clicks the button but pressing its Alt+ shortcut does not! Thanks for your effort!
wpfwannabe
+1  A: 

Well... You have to write code to handle these keypresses and convert them into WM_COMMAND messages. The traditional way to do this is to define an accelerator table and process them using TranslateAccelerator() - but of course, you're free to do it however you like... Just make sure the keys you handle jibe with the keys you underline!

You might also find this KB article helpful: How to use accelerator keys within a modal dialog box in Visual C++... Or, for a more in-depth (and MFC-free) look at implementing custom message processing in dialogs, check out Raymond Chen's articles on the dialog manager, specifically part 4: The dialog loop and part 9: Custom accelerators in dialog boxes (but seriously, read the whole thing, you know you want to...)

Shog9
Thanks for trying but I don't think accelerators apply here. I am talking about Alt+ shortcuts which are somehow automatic by nature. None of the controls from a resource dialog template require accelerators.
wpfwannabe
@wpfwannabe: yeah, because those are defined *in the dialog template*. Unlike the buttons on your toolbar. If you want to rely on the functionality built into the dialog, add some normal buttons and give them text with shortcuts... Remember: toolbars, like menus, weren't originally created for use on dialogs, and as such they don't act like dialog controls.
Shog9
Ok, I think you are absolutely right. Toolbars aren't really meant for dialogs and the proper way to handle accelerators is by using Win32 accelerators. I do have an issue though... How to `TranslateAccelerator` in a Win32 (non-MFC) dialog? I do not own the message loop. There is no `PreTranslateMessage`.
wpfwannabe
@wpfwannabe: You do what MFC does - write your own message loop. I've added some good links to help you out...
Shog9
Figured it out on my own. But thanks a lot anyway!
wpfwannabe