i know owner draw menus, but how can i test if the alt key acelarator was activated?(i try the getkeystate(), but don't works, in these)
Printable View
i know owner draw menus, but how can i test if the alt key acelarator was activated?(i try the getkeystate(), but don't works, in these)
Maybe one of these
1. Use a mouse hook (SetWindowsHookEx API). That allows you to preview all keyboard events before VB gets them.
2. In a subclass procedure for main-level window if wanting to trap Alt+Space, you can look for the WM_SYSCOMMAND & WM_COMMAND messages with wParam being SC_KEYMENU
Maybe you should tell us what you want to do when the ALT key is pressed? The ALT key is used for accelerator keys in both menus and on controls.
Oh, that system setting. See SystemParametersInfo API
If you want to underline the characters based on that setting, this is my recommendation.
1. Set a mouse hook to look for the ALT key being pressed if that setting is on; else always underline
2. When ALT is pressed, without another key, set a flag in your program
3. When you are drawing your menus and the flag is set, include the & symbol else do not
4. When the menus close, reset that flag
The above is just an idea; not the complete logic. For other ideas, may want to search for the following both here & google: SPI_SETKEYBOARDCUES
Ugh, ignore above.
If you want to draw underlined characters based on the system setting. It's easier than that:
The DRAWITEMSTRUCTURE you used for custom drawing menus tells you.
Code:Dim DIS As DRAWITEMSTRUCTURE
Const ODS_NOACCEL As Long = &H100
...
If (DIS.ItemState And ODS_NOACCEL) = 0 Then
' do draw with &
Else
' don't draw with &
End If
...
If your system isn't set up for it, that ItemState flag will not be set.
Attachment 84500
Regarding other apps they do not hide the accelerator key. That option first came in Win2K.
If the menus are owner drawn, the code for owner-drawing must handle the ODS_NOACCEL flag (post #8). If not, they will most likely draw the accelerator all the time. If the menus are true menus and not owner drawn, then they should behave as expected.
Now, I downloaded the menu sample project you are using from your other thread. It works fine.
1. Ensure you have the option activated to hide accelerator menu keys unless ALT pressedCode:...
CopyMemory DIS, ByVal lParam, Len(DIS)
If (DIS.itemState And ODS_NOACCEL) = 0 Then
Debug.Print "use accelerator keys "; DIS.itemState
Else
Debug.Print "do not use accelerator keys "; DIS.itemState
End If
...
2. Click on a subclassed menu that has at least one accelerator key
-- this should print: do not use accelerator keys
3. Now hit the ESC key a couple times to ensure your menu does not have focus
4. Press the ALT key and click on the same menu item
-- this should print: use accelerator keys
The above only applies to subclassed menu items. Unsubclassed menu items should behave correctly
Use Spy++ on those applications
Mozilla: Their "menus" are not true menus. They are drawn as part of the window, just like you'd draw on a picturebox
VB IDE: Menu bar is not a true menu; it is a toolbar. The dropdown menus from that bar are ownerdrawn I believe. They are always showing the accelerator key simply because VB probably hasn't updated the IDE since Win2K to handle the new ownerdrawn flags.
Some notes
1) If true menus are not owner drawn, they should behave correctly
2) Trapping the ALT key is not the correct way to do it in my opinion, if you want to replicate how Windows draws menus. Up to you of course.
3) Subclassed popup menus probably should be drawn with the accelerator key all the time unless they are specifically processed in the subclass procedure by handling the WM_CONTEXTMENU message. Pressing Shift+F10 is similar to mousing a Right Click with ALT key toggled on.
To see what I mean, do this
a: Ensure hiding accelerator keys option is selected
b. Add a textbox to your form
c. Set focus to it. Right click on it. You will not see accelerator keys
d. Hit ESC and now press Shift+F10. You will see accelerator keys
Edited: The entire idea, in my opinion, regarding hiding the accelerator keys was just stupid. Basically, when Windows uses that option, it is saying: "hey if I'm using my mouse to navigate menus, hide the keys. But if I'm using my keyboard then show the keys". So with those keys hidden by default, if there was an accelerator key for a menu item, you'd never know it unless you took the time to find them, using the keyboard to start the menus off. But then again, people over the past decade have been more mouse-users than keyboard-users. And the ratio of mouse to keyboard users will probably increase exponentially over time
sorry LaVolpe but i'm seen your code:
and something isn't right. the dis.itemstate=false(i understand) but ODS_NOACCEL=0 i don't understand:(Code:If (DIS.ItemState And ODS_NOACCEL) = 0 Then
' do draw with &
Else
' don't draw with &
End If
because it's a const.
anotherthing: finally a put to work.. thanks
The const value was given in previous post: ODS_NOACCEL = &H100
When using AND, we are testing to see if the .ItemState contains the flag ODS_NOACCEL.
If the result is zero, then the flag is not included and means 1 of 3 things
1) The operating system is older than Win2K
2) The user does not have the option set to hide the accelerator keys
3) The user has the option set, and the keyboard initiated the menus not the mouse
When the flag is not set, it simply means to draw the menu with the accelerator key
I'd say it depends on you.
VB IDE uses right aligned short cut keys.
Windows Explorer (look at View | Go To menu) uses a tabular alignment
Mozilla (look at its Bookmarks menu) uses a tabular alignment.
Tabular alignment can be calculated as so
1. Determine the maximum width needed to display all menu captions, not including the shortcut keys, on that menu panel only.
2. Add a buffer to that max width, say 10 pixels as you determine looks good. Your menu panel size will include the max size needed to display the caption + shortcut key + buffer, for all menu items
3. Draw the menu caption without the shortcut keys
4. Now draw the shortcut keys offset at the max width + that buffer
The captions will be aligned left. The shortcut keys will be left aligned but appear like they are in another column within the menu panel, understand?
yes thanks my friend.
i will tell you my style:
-when you move then mouse 1 fill rectangule is showed(blue) and the bitmap is showed but in bottom 1 black color is showed(like a shadow). i did these with bitblt() with vbSrcAnd flag;)
maybe in time i will give it the modern style;)
thanks for everything my friend
(when is possible i rate you;))