CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2003
    Posts
    60

    Making the menu invisible

    Hi,
    Can someone help me in making the menus of the given window(for example on screen keyboard) invisible by any programming language. Is there any API that does this. Please help me out in this.Thanks in advance.

    Vinoth

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Making the menu invisible

    You can use the combination of GetMenu and ShowWindow APIs to hide or show a menu. Lookup on MSDN for the documentation related to these APIs.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Making the menu invisible

    Sorry to revive this thread....

    I tried this :
    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    
    Private Const SW_HIDE = 0
    Private Const SW_SHOW = 5
    
    Private Sub Command1_Click()
    Dim retval As Long
    Dim hwnd As Long, hSysMenu As Long, count As Long
    hwnd = FindWindow(vbNullString, "Untitled - Notepad")
    hSysMenu = GetMenu(hwnd)
    
    retval = ShowWindow(hSysMenu, SW_HIDE)
    End Sub
    But my Notepad's menu doesn't disappear, in fact, nothing happens with Notepad. Am I doing something dumb again ¿

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Making the menu invisible

    Well it doesn't work for me either. Let me do some research. I guess ShowWindow doesn't work with menus.

  5. #5
    Join Date
    Nov 2005
    Location
    Omaha, Nebraska, USA
    Posts
    696

    Re: Making the menu invisible

    I couldn't get that method to work either (LastDLLError was "Invalid window handle."), but I did find a way to remove a menu (not hide it).

    Code:
    Option Explicit
    
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
    Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
    Const MF_BYPOSITION = &H400&
    Const MF_REMOVE = &H1000&
    
    Private Sub Command1_Click()
        Dim retval As Long, i As Long
        Dim hwnd As Long, hSysMenu As Long, count As Long
        hwnd = FindWindow(vbNullString, "Untitled - Notepad")
        hSysMenu = GetMenu(hwnd)
        If hSysMenu Then
            count = GetMenuItemCount(hSysMenu)
            For i = 0 To count
                'Removing an item from the menu repositions
                'the menu items, so we always remove the first position
                RemoveMenu hSysMenu, 0, MF_BYPOSITION Or MF_REMOVE
            Next
        End If
    End Sub
    Of course, if you actually need the menu at some time (e.g. make it visible again), this method wouldn't work.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Making the menu invisible

    I think the whole problem ( I may be wrong) is that the GetMenu API doesn't return the "whole menu object" ie. the MenuBar itself. Do you guys agree ¿ I've dabbed around a bit with FindWindowEx, but I don't know how to reference the MenuBar object.
    I thought of RemoveMenu too, but it removes it, then what if we want it back ¿

    Guys, sorry to have put everyone in a flat spin, it was just something I've also wanted to do too....

    I love these threads

  7. #7
    Join Date
    Nov 2005
    Location
    Omaha, Nebraska, USA
    Posts
    696

    Re: Making the menu invisible

    You, unfortunately, can't check if the handle returned by GetMenu is the right window handle, as Spy++ can't highlight or find the menu on Notepad (or other programs with standard menus).

    Of course, if we looked at GetMenu's brother API function, SetMenu, we can see how to show and hide menus.
    Code:
    Option Explicit
    
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function SetMenu Lib "user32.dll" (ByVal hwnd As Long, ByVal hMenu As Long) As Long
    
    Dim nhwnd As Long, nhSysMenu As Long
    
    Private Sub Command1_Click()
        SetMenu nhwnd, 0
    End Sub
    
    Private Sub Command2_Click()
        SetMenu nhwnd, nhSysMenu
    End Sub
    
    Private Sub Form_Load()
        nhwnd = FindWindow(vbNullString, "Untitled - Notepad")
        nhSysMenu = GetMenu(nhwnd)
    End Sub
    Note that this doesn't actually "hide" the menu, it will remove it as well, but since you're storing the handle to the menu (which does stay in memory), you can restore it.

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Making the menu invisible

    Nice! Good stuff! Thanx Chaos!! Yeah, SetMenu never even crossed my mind! LOL!

  9. #9
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Making the menu invisible

    Well I was wrong with the ShowWindow API. It is probably because of the evident reasons cited by Chaos. SetMenu is right.

    That was a good one Chaos

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