Valhalla Legends Forums Archive | Visual Basic Programming | Adding Menu Items?

AuthorMessageTime
BaDDBLooD
How do you add a Menu item through Code?
August 25, 2004, 2:58 AM
Spht
[quote author=BaDDBLooD link=board=31;threadid=8361;start=0#msg77258 date=1093402724]
How do you add a Menu item through Code?
[/quote]

See PluginMenu. C++, but very straight-forward for porting if you know VB.
August 25, 2004, 3:14 AM
BaDDBLooD
Thanks, but i can't port from c++ to VB yet.
August 25, 2004, 3:46 AM
The-FooL
Depending on what your adding, you can create menu items, set them not visible by default, then change the caption/visibility during runttime.
August 25, 2004, 11:51 AM
BaDDBLooD
Yeah but, i dont want waisted menu items :O
August 25, 2004, 1:01 PM
Eli_1
Set the index to 0, and load new instances of it every time you want another menu (?)
August 25, 2004, 6:38 PM
Spht
[quote author=BaDDBLooD link=board=31;threadid=8361;start=0#msg77266 date=1093405578]
Thanks, but i can't port from c++ to VB yet.
[/quote]

Did you try? Or are you assuming you can't? As I said, the PluginMenu module is VERY straight-forward for porting. You basically just need to know VB and have API Viewer on hand. Example (since you probably still don't have enough courage to go and port it all on your own):

[code]HMENU FindPluginMenu(HMENU hMainMenu)
{
UINT MenuItems = GetMenuItemCount(hMainMenu);

for(UINT i = 0; i < MenuItems; i++) {
int MenuStringSize;
LPSTR MenuString = new CHAR[MenuStringSize = GetMenuString(hMainMenu, i, 0, 0, MF_BYPOSITION)+1];
if(GetMenuString(hMainMenu, i, MenuString, MenuStringSize, MF_BYPOSITION)
&& !strcmp(MenuString, PLUGIN_MENU_NAME)) {
delete [] MenuString;
return GetSubMenu(hMainMenu, i);
}

delete [] MenuString;
}

// Menu not found, create it

HMENU PopupMenu = CreateMenu();

AppendMenu(hMainMenu, MF_STRING | MF_POPUP, (UINT_PTR)PopupMenu, PLUGIN_MENU_NAME);

return PopupMenu;
}[/code]

[code]Function FindPluginMenu(ByVal hMainMenu As Long) As Long
Dim MenuItems As Long
Dim MenuString As String
Dim MenuStringSize As Long
Dim i As Long

MenuItems = GetMenuItemCount(hMainMenu)
For i = 0 To MenuItems
MenuStringSize = GetMenuString(hMainMenu, i, 0, 0, MF_BYPOSITION)
MenuString = String(MenuStringSize, vbNullChar)
If GetMenuString(hMainMenu, i, MenuString, MenuStringSize, MF_BYPOSITION) Then
If Left$(MenuString, InStr(MenuString, vbNullChar) - 1) <> PLUGIN_MENU_NAME Then FindPluginMenu = GetSubMenu(hMainMenu, i)
End If
Next i

If FindPluginMenu = 0 Then
' Menu not found, create it
Dim PopupMenu As Long

PopupMenu = CreateMenu()
AppendMenu hMainMenu, MF_STRING Or MF_POPUP, PopupMenu, PLUGIN_MENU_NAME
FindPluginMenu = PopupMenu
End If
End Function[/code]

That should get you started. If you take the time to carefully read through the code, you'll realize it is quite simple. Now go do the rest.
August 25, 2004, 8:35 PM
BaDDBLooD
Relying on your example *HEAVILY*, i managed to come out with this.

[code]

Function FindPluginMenu(ByVal hMainMenu As Long) As Long
Dim MenuItems As Long
Dim MenuString As String
Dim MenuStringSize As Long
Dim i As Long

MenuItems = GetMenuItemCount(hMainMenu)
For i = 0 To MenuItems
MenuStringSize = GetMenuString(hMainMenu, i, 0, 0, MF_BYPOSITION)
MenuString = String(MenuStringSize, vbNullChar)
If GetMenuString(hMainMenu, i, MenuString, MenuStringSize, MF_BYPOSITION) Then
If Left$(MenuString, InStr(MenuString, vbNullChar) - 1) <> PLUGIN_MENU_NAME Then FindPluginMenu = GetSubMenu(hMainMenu, i)
End If
Next i

If FindPluginMenu = 0 Then
' Menu not found, create it
Dim PopupMenu As Long

PopupMenu = CreateMenu()
AppendMenu hMainMenu, MF_STRING Or MF_POPUP, PopupMenu, PLUGIN_MENU_NAME
FindPluginMenu = PopupMenu
End If
End Function

Function RemovePluginMenu(ByVal hMainMenu as Long) as Long
Dim MenuItems as Long
Dim Menustring as String
Dim MenuStringSize as Long
Dim i as Long

For i = 0 To Menu Items
   MenuStringSize = GetMenuString(hMainMenu, i, 0, 0, MF_BYPOSITION)
   MenuString = String(MenuStringSize, vbNullChar)
   If GetMenuString(hMainMenu, i, MenuString, MenuStringSize, MF_BYPOSITION) Then
If Left$(MenuString, InStr(MenuString, vbNullChar) - 1) <> PLUGIN_MENU_NAME Then DeleteMenu(hMainMenu, i, MF_BYPOSITION)
End If
Next I
End Function

Function CheckContiguousMenuID(ByVal Menu as Long, ByVal MenuID as Long, ByVal Required as Long) as Boolean
Dim EndRange as Long
EndRage = MenuID + Required
Do
   If GetMenuState(Menu, MenuID+Required, MF_BYCOMMAND) <> -1 Then
    MenuID = EndRange
    CheckContiguousMenuID = False
   End If
While Required = Required - 1   
CheckContiguousMenuID = True
End Function

[/code]

Dunno if that's right or not, but that's what i came up with. Not sure on how to do the WINAPI Ones...
August 25, 2004, 10:28 PM
Myndfyr
[quote author=BaDDBLooD link=board=31;threadid=8361;start=0#msg77333 date=1093472932]

Dunno if that's right or not, but that's what i came up with. Not sure on how to do the WINAPI Ones...
[/quote]

Have you searched the CRT files for:

[code]
#define WINAPI /* the code value you're looking for to emulate */
[/code]
?
August 25, 2004, 10:48 PM
BaDDBLooD
Nope.. not sure what a CRT File is lmao
August 25, 2004, 11:07 PM
Myndfyr
[quote author=BaDDBLooD link=board=31;threadid=8361;start=0#msg77337 date=1093475229]
Nope.. not sure what a CRT File is lmao
[/quote]

C runtime. I believe the C runtime source is included in the Platform SDK for Win32. I could be wrong, though.
August 25, 2004, 11:25 PM
BaDDBLooD
Could you explain that in less complex terms?
August 25, 2004, 11:27 PM
Myndfyr
[quote author=BaDDBLooD link=board=31;threadid=8361;start=0#msg77340 date=1093476452]
Could you explain that in less complex terms?
[/quote]

www.google.com.
August 25, 2004, 11:29 PM
UserLoser.
You don't need to worry about WINAPI (__stdcall). Visual Basic is nothing but Standard calls. In VB, you can't really change the calling convention from stdcall to fastcall, cdecl, ect. Therefore, you don't need to worry about setting the calling convention on your function to stdcall.
August 25, 2004, 11:57 PM

Search