views:

1487

answers:

4

I've tried my best and cannot figure out what happened here. It worked fine in Delphi 4. After upgrading to Delphi 2009, I don't know if this is the way it is supposed to work, or if it's a problem:

This is what my program's menu looks like in Design Mode under Delphi 2009:

alt text

Notice that every word in the Main Menu and the File submenu have one letter underlined. It is supposed to be like this. This underlined letter is called the Accelerator Key and is standard in Windows applications so that you can use the Alt-key and that letter to quickly select the menu item and then submenu item with the keyboard rather than with your mouse.

You get them this way by using the "&" character as part of the caption of the item, for example: Save &As...

When I run my application, and use the mouse to open the File menu, it looks like this:

alt text

The characters are underlined in the main menu, but are not underlined in the File menu.

If instead, I use the Alt-F key to open up the File submenu, then it looks correct like this:

alt text

and all the Accelerator Key letters are properly underlined.

I've played with the AutoHotKeys option but that's not the problem.

Has someone encountered this problem before? Is the example in the 2nd image correct behavior that I don't know of? Or is there some option or coding mistake that I might have missed?


Nov 2009 (one year later): mghie seems to have got to the root of this and figured out the problem. See his accepted answer below.

+1  A: 

I don't think it is a Delphi generated bug as you have the same behavior with Notepad on Vista. Also in Delphi itself BTW...
I must confess that I did not pay attention before your question. Thanks for pointing it out.

François
No. In Delphi 2009 for me, all the submenu items show the accelerator key, even when I select the menu with the mouse. That's why I thought it was some setting I had, rather than Delphi or the Operating System.It is definitely a stramge one, I've spent about 4 hours already trying to fix it.
lkessler
+6  A: 

There is a standard Windows setting (under display properties) to normally hide those accelerators unless the Alt key is held down. That would explain why opening the menu with Alt+F10 shows them for you. Maybe that's the cause?

[EDIT]: No, it's not. I just tried, and a simple TForm with a menu item shows the accelerator, but as soon as I add a TImageList and set the ImageIndex of the single menu item, or simply set OwnerDraw to true, then the accelerator underline disappears. I guess that really is a bug in the VCL.

BTW, this is on Windows XP.

Workaround:

I have debugged this using Delphi 2009 on Windows XP 64, and the root cause for the missing accelerators seems to be that Windows sends WM_DRAWITEM messages with the ODS_NOACCEL flag set, which it shouldn't if the system is set to show accelerators at all times. So you could say that it is not a VCL bug, but a Windows problem which the VCL does not work around.

However, you can work around it in your own code, you just need to reset the flag before passing the message to the VCL. Override the window proc

protected
  procedure WndProc(var Message: TMessage); override;

like so:

procedure TYourForm.WndProc(var Message: TMessage);
const
  ODS_NOACCEL = $100;
var
  pDIS: PDrawItemStruct;
  ShowAccel: BOOL;
begin
  if (Message.Msg = WM_DRAWITEM) then begin
    pDIS := PDrawItemStruct(Message.LParam);
    if (pDIS^.CtlType = ODT_MENU)
      and SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, @ShowAccel, 0)
    then begin
      if ShowAccel then
        pDIS^.itemState := pDIS^.itemState and not ODS_NOACCEL;
    end;
  end;
  inherited;
end;

This is demonstration code only, you should not call SystemParametersInfo() every time a WM_DRAWITEM message is received, but once at program start, and then every time your program receives a WM_SETTINGCHANGE message.

mghie
I'm on Windows XP as well. After these answers and my thinking on this, and your help, I do agree it may be a bug in the VCL. I'll report it.
lkessler
Reported as a bug to Embarcadero: http://qc.codegear.com/wc/qcmain.aspx?d=68816
lkessler
BTW, I get the same behaviour with Delphi 2007.
mghie
Mghie: Thank you for your further research into this matter (a whole year later!!) and finding this. I've updated the bug report at Embarcadero with your information. Wish I could give you ++++++++++1
lkessler
Probably calling SystemParametersInfo() at program startup would be good enough 99% of the time.
lkessler
+4  A: 

It is a "feature" introduced with Windows 2000:

The Old New Thing: Why does Windows hide keyboard accelerators and focus rectangles by default?

It would appear that Delphi 4 didn't support this Windows feature.

To have 2000 and XP menus show accelerator keys, right-click an empty spot on the desktop, choose Properties, click the Appearance tab, and under Effects, uncheck Hide Underlined Letters for Keyboard Navigation until I Press the Alt Key. Click OK twice.

Not sure how to do it in Vista.

Jim McKeeth
I thought you had the answer. But unfortunately when I looked at that "Hide Underlined Letters" option (I'd never known about it before), I found it to be unchecked.
lkessler
What OS? Your screen shots look like XP. I just tried it on XP and it works (uncheck the option and the accelerator key underlines show up.) Maybe you need to reboot.
Jim McKeeth
A: 

As Jim McKeeth noted above (correctly), this is "by design" behavior. If the menus are triggered through keyboard action the accelerators should be shown, but if triggered by the mouse the accelerators are intentionally not shown.

I have my XP configured to show accelerators at all times, but a quick test with that option changed confirms that the menus should not show underlines either (Visual Studio responded as I expected, no underlines when using the mouse). However, Microsoft Office ignores this setting and always shows the underlines. So it looks like a bug in how the menus are drawn in Delphi (I don't have any experience with Delphi myself).

I found the option for Vista as well: http://www.vistax64.com/vista-general/42125-always-show-menu-underline-keyboard-accelerators.html

You can turn this on in the new Ease of Access Center (go to Control Panel, click Ease of Access and then click Ease of Access Center). In the Ease of Access Center, click Make the keyboard easier to use, and at the very bottom select the Underline keyboard shortcuts and access keys check box.

While doing further research I found this related bug on Delphi forums: http://qc.codegear.com/wc/qcmain.aspx?d=37403

It looks like in your case the child windows (the drawn menus) aren't getting or aren't handling WM_UIUPDATESTATE message from their parent window, which is what causes the redraw with accelerators.

Step
"It looks like in your case the child windows (the drawn menus) aren't getting or aren't handling WM_UIUPDATESTATE message from their parent window, which is what causes the redraw with accelerators" - Exactly! That's the Bug!
lkessler