DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Modifying menu items in other applications

  1. #1
    Davide Rinaldi Guest

    Modifying menu items in other applications


    Hi,
    I want to fire up Word viewer and display a document. But I do not want
    the user to have the ability to edit the document from here even though they
    have word installed. Is there anyway to disable the "File->Open for Editiing"
    menu item.

    I have also tried finding command line paramaters to do this but have had
    no luck.

    Cheers

    Davide

  2. #2
    Mark Alexander Bertenshaw Guest

    Re: Modifying menu items in other applications

    Davide -

    In theory, the code below <ought> to work:

    -------------------------------------------

    Option Explicit

    Private Type MENUITEMINFO
    cbSize As Long
    fMask As Long
    fType As Long
    fState As Long
    wID As Long
    hSubMenu As Long
    hbmpChecked As Long
    hbmpUnchecked As Long
    dwItemData As Long
    dwTypeData As Long
    cch As Long
    hbmpItem As Long
    End Type

    Private Declare Function EnableMenuItem Lib "User32.dll" ( _
    ByVal hMenu As Long, _
    ByVal uIDEnableItem As Long, _
    ByVal uEnable As Long _
    ) As Long

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String _
    ) As Long

    Private Declare Function GetMenu Lib "User32.dll" ( _
    ByVal hWnd As Long _
    ) As Long

    Private Declare Function GetMenuItemInfo Lib "User32.dll" Alias
    "GetMenuItemInfoA" ( _
    ByVal hMenu As Long, _
    ByVal uItem As Long, _
    ByVal fByPosition As Long, _
    ByRef lpmii As MENUITEMINFO _
    ) As Long

    Private Const MF_BYPOSITION = &H400&
    Private Const MF_DISABLED = &H2&
    Private Const MF_GRAYED = &H1&

    Private Sub Command1_Click()
    Const MAIN_MENU_POS As Long = 0
    Const MENU_POS As Long = 1
    Dim hWnd As Long
    Dim hMenuBar As Long
    Dim udtMenuItemInfo As MENUITEMINFO

    ' First get your window.
    hWnd = FindWindow(vbNullString, "Untitled - Notepad")
    'hWnd = FindWindow(vbNullString, "Microsoft Word - Document1")

    ' Next get a handle to the menu bar.
    hMenuBar = GetMenu(hWnd)

    ' Get info about the MAIN_MENU_POS'th item.
    udtMenuItemInfo.cbSize = LenB(udtMenuItemInfo)
    Debug.Print GetMenuItemInfo(hMenuBar, MAIN_MENU_POS, 1, udtMenuItemInfo)

    ' Enable the MENU_POS'th item of the submenu.
    EnableMenuItem udtMenuItemInfo.hSubMenu, MENU_POS, MF_BYPOSITION Or
    MF_GRAYED

    End Sub

    -------------------------------------------

    Sadly, I have yet to get this to work with submenu items.

    Unfortunately, there are a number of things going against you. For instance,
    most programs tend to react the click on the top level menu bar, and set
    menu items; therefore even if you set an item to be disabled, the program
    might set it back to enabled when you clicked on the top level menu item.
    Also, the recent Office applications (95/97/2000) tend to handle their menus
    in a completely different way to other windows programs, and you can't even
    get the top level menu bar. I hope that this doesn't happen in the Word
    Viewer.


    --
    Mark Alexander Bertenshaw
    Programmer/Analyst
    PrimeResponse
    Brentford
    UK
    "Davide Rinaldi" <GetRidOf.davide@iweb.notrequired.net.au> wrote in message
    news:39ebb98d$1@news.devx.com...
    >
    > Hi,
    > I want to fire up Word viewer and display a document. But I do not want
    > the user to have the ability to edit the document from here even though

    they
    > have word installed. Is there anyway to disable the "File->Open for

    Editiing"
    > menu item.
    >
    > I have also tried finding command line paramaters to do this but have had
    > no luck.
    >
    > Cheers
    >
    > Davide




  3. #3
    Davide Rinaldi Guest

    Re: Modifying menu items in other applications


    Hi Mark,
    Thankyou for you help, it put me on the right track.

    I did get it to work in the end by using the RemoveMenu function instead.
    The word viewer handles it's menus a little different then other apps like
    notepad. It seems to reset itself on every activation. I have included
    my code that 90% works. The user can still open a document for editing if
    they use "ctrl E", but that option isnt displayed.


    Cheers

    Davide

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Option Explicit

    Private Declare Function EnableMenuItem Lib "User32.dll" ( _
    ByVal hMenu As Long, _
    ByVal uIDEnableItem As Long, _
    ByVal uEnable As Long _
    ) As Long

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String _
    ) As Long

    Private Declare Function GetMenu Lib "User32.dll" ( _
    ByVal hwnd As Long _
    ) As Long

    Public Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal
    nPos As Long) As Long

    Public Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As
    Long

    Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, _
    ByVal nPosition As Long, ByVal wFlags As Long) As Long

    Public Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long,
    _
    ByVal nPos As Long) As Long

    Public Const MF_BYCOMMAND = &H0&
    Private Const MF_GRAYED = &H1&


    Public Sub pDisableMenu()
    Dim hwnd As Long
    Dim hMenuBar As Long
    Dim hSubMenuItem As Long
    Dim hSubMenu As Long
    Dim lngRet As Long

    ' First get your window.
    hwnd = FindWindow(vbNullString, "Microsoft Word Viewer 97 - daves.doc")
    'hwnd = FindWindow(vbNullString, "Microsoft Word Viewer 97")
    'hwnd = FindWindow(vbNullString, "Untitled - Notepad")

    ' Next get a handle to the menu bar.
    hMenuBar = GetMenu(hwnd)

    'Get a handle to the sub menu
    hSubMenu = GetSubMenu(hMenuBar, 1)

    'Get a handle to the item
    hSubMenuItem = GetMenuItemID(hSubMenu, 1)

    'use RemoveMenu as the EnableMenuItem doesnt work in the case of the
    word viewer
    'it resets itself on every menu click. I noticed this as I had my code
    execute
    'while I had the menu open and noticed it did grey out the command only
    to
    'reset itself when I moved to the edit menu and then back

    ' lngRet = EnableMenuItem(hSubMenu, hSubMenuItem, MF_BYCOMMAND Or MF_GRAYED)
    lngRet = RemoveMenu(hSubMenu, hSubMenuItem, MF_BYCOMMAND)
    lngRet = DrawMenuBar(hwnd)

    End Sub

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



    "Mark Alexander Bertenshaw" <mark.bertenshaw@virgin.net> wrote:
    >Davide -
    >
    >In theory, the code below <ought> to work:
    >
    >-------------------------------------------
    >
    >Option Explicit
    >
    >Private Type MENUITEMINFO
    > cbSize As Long
    > fMask As Long
    > fType As Long
    > fState As Long
    > wID As Long
    > hSubMenu As Long
    > hbmpChecked As Long
    > hbmpUnchecked As Long
    > dwItemData As Long
    > dwTypeData As Long
    > cch As Long
    > hbmpItem As Long
    >End Type
    >
    >Private Declare Function EnableMenuItem Lib "User32.dll" ( _
    > ByVal hMenu As Long, _
    > ByVal uIDEnableItem As Long, _
    > ByVal uEnable As Long _
    >) As Long
    >
    >Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    > ByVal lpClassName As String, _
    > ByVal lpWindowName As String _
    >) As Long
    >
    >Private Declare Function GetMenu Lib "User32.dll" ( _
    > ByVal hWnd As Long _
    >) As Long
    >
    >Private Declare Function GetMenuItemInfo Lib "User32.dll" Alias
    >"GetMenuItemInfoA" ( _
    > ByVal hMenu As Long, _
    > ByVal uItem As Long, _
    > ByVal fByPosition As Long, _
    > ByRef lpmii As MENUITEMINFO _
    >) As Long
    >
    >Private Const MF_BYPOSITION = &H400&
    >Private Const MF_DISABLED = &H2&
    >Private Const MF_GRAYED = &H1&
    >
    >Private Sub Command1_Click()
    >Const MAIN_MENU_POS As Long = 0
    >Const MENU_POS As Long = 1
    >Dim hWnd As Long
    >Dim hMenuBar As Long
    >Dim udtMenuItemInfo As MENUITEMINFO
    >
    > ' First get your window.
    > hWnd = FindWindow(vbNullString, "Untitled - Notepad")
    > 'hWnd = FindWindow(vbNullString, "Microsoft Word - Document1")
    >
    > ' Next get a handle to the menu bar.
    > hMenuBar = GetMenu(hWnd)
    >
    > ' Get info about the MAIN_MENU_POS'th item.
    > udtMenuItemInfo.cbSize = LenB(udtMenuItemInfo)
    > Debug.Print GetMenuItemInfo(hMenuBar, MAIN_MENU_POS, 1, udtMenuItemInfo)
    >
    > ' Enable the MENU_POS'th item of the submenu.
    > EnableMenuItem udtMenuItemInfo.hSubMenu, MENU_POS, MF_BYPOSITION Or
    >MF_GRAYED
    >
    >End Sub
    >
    >-------------------------------------------
    >
    >Sadly, I have yet to get this to work with submenu items.
    >
    >Unfortunately, there are a number of things going against you. For instance,
    >most programs tend to react the click on the top level menu bar, and set
    >menu items; therefore even if you set an item to be disabled, the program
    >might set it back to enabled when you clicked on the top level menu item.
    >Also, the recent Office applications (95/97/2000) tend to handle their menus
    >in a completely different way to other windows programs, and you can't even
    >get the top level menu bar. I hope that this doesn't happen in the Word
    >Viewer.
    >
    >
    >--
    >Mark Alexander Bertenshaw
    >Programmer/Analyst
    >PrimeResponse
    >Brentford
    >UK
    >"Davide Rinaldi" <GetRidOf.davide@iweb.notrequired.net.au> wrote in message
    >news:39ebb98d$1@news.devx.com...
    >>
    >> Hi,
    >> I want to fire up Word viewer and display a document. But I do not want
    >> the user to have the ability to edit the document from here even though

    >they
    >> have word installed. Is there anyway to disable the "File->Open for

    >Editiing"
    >> menu item.
    >>
    >> I have also tried finding command line paramaters to do this but have

    had
    >> no luck.
    >>
    >> Cheers
    >>
    >> Davide

    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center