Enable/disable popups in main menu

Hi;

We have an Add-In where we need to enable/disable a couple of menu items based on the location of the caret in the document. As an example, think of the Cut menu item in Edit - it is only enabled if text is selected in the document.

Is there any event or any other method where we can enable/disable the menu items just before the menu is displayed. Possibly an event that occurs when the user clicks on the main level menu that will cause it's menu to drop down

At present we use the select change event. But this is a bad solution both because it is fired all the time so we are using a lot of CPU cycles and because it does not fire when the user is typing text so we are not always enabled correctly.

Is there a better way to do this

thanks - dave


Answer this question

Enable/disable popups in main menu

  • petemod

    Hi

    Here you go a simple scenario sample





    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
       
    Const WM_QUIT As Long = &H12
       Const WM_CLOSE As Integer = &H10
       Const WM_DESTROY As Integer = &H2
       Const WM_PARENTNOTIFY As Integer = &H210

       Select Case m.Msg
          Case WM_CLOSE
             MessageBox.Show("WMCLOSE")
       End Select

       MyBase.WndProc(m)

    End Sub


     


    This is a VB Sample as we write our Office OM Stuff in VB to save writting long hand on OM Calls Smile

    Hope this gives a start point to hooking into the WndProc and you can then look at the message queue, you need to pass the onward process though as otherwise your app will freeze Sad

    Regards



  • Drea27

    You may be able to get the click event of the main items of the menu, instead of the menu items.....

    Have you try it

    Bye.

  • Srini A

    Hi Dave

    I have done something similar before and dont have access to the code at the moment I will try and get chance and post a sample tomorrow.

    Rgds

  • rmillerii

    Hi;

    I add my menu to the main Word menu. So along with Word's File, Edit, View,... I add my top CommandBarPopup too. But every way I tried to do this I have to add a CommandBarPopup menu item. Am I missing something

    thanks - dave

  • Ting Wang - MSFT

    Hi;

    The WndProc part I know. What I don't understand is how to get my WndProc hooked into Word's WndProc. Can you point me at how to do that

    thanks - dave

  • Cy Huckaba

    Hi Dave

    The following url may help http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemwindowsformsnativewindowclasswndproctopic.asp

    Note the complete line may need to be rejoined.

    Regards

  • Derek Moffett

    Hi

    So to sumarise, you are looking at having a menu item in the normal word menu structure and wish to enable/disable the menu item according to the position within the document.

    There are a couple of questions firstly if the menu is clicked and the cursor is in the wrong place is this acceptable user behaviour I think with the limitation of being able to scan all current activity within the word document you are within other than to the acceptance of using XML Schema event notification when entering/leaving an element I would propose this is the easiest route to go.

    It is possible to change a state of the button by using the Enabled property and finding the menu item and setting this eg.

    cmdbar.Item['MenuItemName'].Enabled = false;

    This can be done dynamically if so required.

    Regards

  • Steven Ramacher

    Hum.. forgive me: I didn't understant that you are using a commandbar menu...

    Sorry.

    Indeed, in that case, I don't know how you could do what you want, sorry :(



    Bye.

  • FreddieCode

    Why don't you use a MenuStrip for your menu

    With it, each item in the menu have a click event: so File, Edit, etc.. will have a click event and the mennu items too.



    HTH.


    Bye.

  • Hubman

    Hello;

    I tried that. But the main menu items (like File, Edit, etc) are a CommandBarPopup and they don't have any events at all. So I can't have a click handler for the CommandBarPopup I add to the main menu.

    Am I missing something on where/how I do this

    thanks - dave

  • edotcom

    Hello;

    I have a bad habit of not posting enough detail and assuming people can read my mind. So here it is in full (I hope) detail.

    When Word starts up, I get ThisApplication.CommandBars["Menu Bar"] and add to it a CommandBarPopup menu named AutoTag. So Word's main menu across the top is now "File Edit View Insert Format Tools AutoTag Table Window Help". The AutoTag menu itself is never disabled.

    AutoTag has a number of CommandBarButton items and two of them are "Insert Tag..." and "Edit Tag...". If the caret is on a tag then "Edit Tag" should be enabled and "Insert Tag" should be disabled. And if the caret is not on a tag - vice-versa.

    I have code that determines if the caret is on a tag and then calls cmdbar.Item['MenuItemName'].Enabled = true/false appropiately.

    The problem is, I only want to call this code when the user clicks on the AutoTag menu, causing it's menu items to appear. Right now I enable/disable the menu items on each selection change event and that is using a bunch of CPU cycles any time the user does anything.

    I don't care if I call enable/disable a little more than necessary. For example, if there is an event anytime the mouse is clicked in the non-client area, that would be ok as that is usually a menu click. I just want something less expensive than using the selection changed event.

    Any ideas

    thanks - dave


  • Matt Zimmerman

    Were you able to find the sample We could really use it.

    thanks - dave

  • Mauricio hevs

    Hi

    Thanks for the refresher, I understand your scenario, IMO this is not going to be easy, an idea might be to hook the message loop of word the master menu being clicked and then refresh the status otherwise you are doing what you can with the OM as it stands.

    Regards


  • JaganMalkareddy

    Hi;

    You may be on to something. What if I hooked the message loop and updated on each WM_COMMAND (it's been years - that is the message for the menu - right ) It would be more than necessary - but not a lot more. And wouldn't that get the menu - by mouse or keystroke

    If so, how do I hook the message loop I am in C# so it's managed code.

    thanks - dave

  • Enable/disable popups in main menu