CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 1999
    Posts
    27

    Change Shortcut of Menu Item

    I want to change the shortcuts of menu items programatically. I read in the online help that this is not possible. Does someone know a way, how I can nevertheless manage to change shortcuts?
    Thanks
    Robert Maier


  2. #2
    Join Date
    Dec 1999
    Posts
    128

    Re: Change Shortcut of Menu Item

    If i've understood your question, the solution is the following:

    In the caption of a menu, say Menu1, you must put an ambersand (&) in front of the letter you wish to be the shortcut for that menu. This letter will appear underlined in run-time.

    i.e.

    Menu1.Caption = "H&ello"



    In this case, "e" is the shortcut. If you want to set "H" as the shortcut:

    Menu1.Caption = "&Hello"




    Hope i helped.


    -------------------------
    Nick A.

  3. #3
    Join Date
    Apr 1999
    Posts
    27

    Re: Change Shortcut of Menu Item

    No, it did not mean that. With the menu editor you can specify a key - like e.g. "Ctrl c" for a menu item, which coresponds to the Shortcut-property of the menu item. Obviously this property can not be changed during runtime. My problem is, that I want to change an application from one language to another. This implies, that the menu entries have different names and so the shortcuts, which were chosen quite reasonable for the 1st language appear odd for the other language.
    Any opinions on this?
    Cheers
    Robert maier






  4. #4
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Change Shortcut of Menu Item

    The only way I can think of to do this would be to remove the Shortcuts from the Menu via the Menu Editor and Add the Name of the Shortcut to the Menu Caption, (Which can be changed at runtime), then use the RegistHotKey and UnRegisterHotKey API's to register your own Shortcut Keys, these would be totaly under your control and to intercept the Shortcut Message and Process the Correct Menu Item you would need to Subclass the Form.

    Something like this:

    In a Module..

    public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (byval hwnd as Long, byval nIndex as Long, byval dwNewLong as Long) as Long
    private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (byval lpPrevWndFunc as Long, byval hwnd as Long, byval Msg as Long, byval wParam as Long, byval lParam as Long) as Long
    public Declare Function RegisterHotKey Lib "user32" (byval hwnd as Long, byval id as Long, byval fsModifiers as Long, byval vk as Long) as Long
    public Declare Function UnregisterHotKey Lib "user32" (byval hwnd as Long, byval id as Long) as Long

    private Const WM_HOTKEY = &H312
    public Const MOD_ALT = &H1
    public Const MOD_CONTROL = &H2
    public Const MOD_SHIFT = &H4
    public Const GWL_WNDPROC = (-4)

    public lPrevWndProc as Long

    public Function WindowProc(byval hwnd as Long, byval Msg as Long, byval wParam as Long, byval lParam as Long) as Long
    If Msg = WM_HOTKEY then
    'Found a Shortcut Message
    'Call the Forms Shortcut Handler with the Shortcut Index Number
    Form1.CallShortcut wParam
    End If
    WindowProc = CallWindowProc(lPrevWndProc, hwnd, Msg, wParam, lParam)
    End Function



    In the Form with the Menu..

    private iShortcuts as Integer

    private Sub Form_Load()
    ' Subclass the Form to Capture the Shortcut Messages (HotKeys)
    lPrevWndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
    ' Create the Shortcuts
    RegisterShortcuts
    End Sub

    private Sub Form_Unload(Cancel as Integer)
    ' Remove the Shortcuts
    UnRegisterShortcuts
    ' Remove the Subclassing
    Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWndProc)
    End Sub

    private Sub mnuOpen_Click()
    'Menu Item "Open" Increments a Number in the forms Caption
    Caption = Val(Caption) + 1
    End Sub

    private Sub RegisterShortcuts()
    ' Create the Shortcuts for the Menu Items Here
    'Use the Caption to Store the Shortcut Text"
    mnuOpen.Caption = "&Open (CTRL+O)"
    ' Register the Shortcut Key Combo
    'The 1 is a Shortcut ID/Index, increment this for each Shortcut you add
    Call RegisterHotKey(hwnd, 1, MOD_CONTROL, vbKeyO)
    'Repeat for other Menu Items..
    '
    'ie.
    'mnuPrint.Caption = "&print (CTRL+P)"
    'Call RegisterHotKey(hwnd, 2, MOD_CONTROL, vbKeyP)
    '
    'set to the Total Number of Shortcuts
    iShortcuts = 1
    End Sub

    private Sub UnRegisterShortcuts()
    Dim iIndex as Integer
    'Keep things tidy and Remove the Shortcuts we registered
    for iIndex = 1 to iShortcuts
    Call UnregisterHotKey(hwnd, iIndex)
    next
    End Sub

    public Sub CallShortcut(byval Index as Long)
    'Handle the Processing of the Shortcuts as they are called
    'The Subclassing Function will pass this Sub the
    'Shortcut Index..
    Select Case Index
    Case 1
    'Execute the Event for the Shortcut with Index of 1
    mnuOpen_Click
    Case 2
    'Etc..
    End Select
    End Sub




    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  5. #5
    Join Date
    Sep 1999
    Posts
    202

    Re: Change Shortcut of Menu Item

    >mnuOpen.Caption = "&Open (CTRL+O)"
    mnuOpen.Caption = "&Open" & Chr(9) & "Ctrl+O"


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured