PDA

Click to See Complete Forum and Search --> : Dynamic Menu


WadeD
May 15th, 2000, 05:36 AM
Hi, I'm trying (unsuccessfully so far :()to add a menu to a foreign window which doesn't have a menu. I know the syntax is right because I can add the same exact menu to other progs (like Notepad). I checked to see if the window is technically a child window and it's not. That's the only restriction I've read about that disallows new menu creation. Any ideas??? Here's the code:Private Declare Function FindWindow& Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String)
Private Declare Function CreateMenu Lib "user32" () As Long
Private Declare Function SetMenu Lib "user32" (ByVal hwnd As Long, ByVal hMenu As Long) As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long

Private Const MF_POPUP& = &H10&
Private Const MF_BYPOSITION& = &H400&
Private Const MF_STRING& = &H0&

Private Sub Form_Load()
Call AddMnu
End Sub

Private Sub AddMnu()
Dim lMnMnu As Long 'MenuBar Handle
Dim lMnuLevl1 As Long 'Top Level Menu Item
Dim lMnuLevl2 As Long 'Submenu Item
Dim lRetVal As Long 'Return Value from API Call
Dim NotepadWindow As Long 'Window Handle of Notepad

'NotepadWindow Handle is Found Using EnumWindows; I verified the handle in both cases (for Notepad and for the other foreign window) in the Debug Window

lMnMnu = CreateMenu
lMnuLevl1 = CreateMenu
lRetVal = InsertMenu(lMnMnu, 0, MF_POPUP Or MF_STRING Or MF_BYPOSITION, lMnuLevl1, "Test")

lMnuLevl2 = CreateMenu
lRetVal = InsertMenu(lMnuLevl1, 0, MF_STRING Or MF_BYPOSITION, lMnuLevl2, "&File")

lRetVal = SetMenu(NotepadWindow, lMnMnu)
lRetVal = DrawMenuBar(NotepadWindow)
End Sub



Thanks,
Wade

Stevie-O
May 15th, 2000, 08:03 PM
I tried this on an app that didn't already have a menubar, and it worked fine. What app are you failing to add your menu to?

WadeD
May 15th, 2000, 08:30 PM
It's a financial application - Solomon (middle-line ERP). The window relies on hot keys and menus from a main window. But it's not technically a child window, at least according to the IsChild API.

[Edited by WadeD on 05-16-2000 at 09:36 AM]

WadeD
May 15th, 2000, 08:53 PM
It wasn't the code that I'm using to create the menus. A while back, I posted a question about getting the handle based on a window's PID. I think it was you Steve that gave me the routine I needed to get that handle which was different from the norm with this particular app. I hadn't applied that to this separate item while testing, so I had a valid handle but not the one I needed. So it works great now. Thanks for looking at it. That jogged my mem.

:)