Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: BaDDBLooD on August 24, 2004, 09:58 PM

Title: Adding Menu Items?
Post by: BaDDBLooD on August 24, 2004, 09:58 PM
How do you add a Menu item through Code?
Title: Re:Adding Menu Items?
Post by: Spht on August 24, 2004, 10:14 PM
Quote from: BaDDBLooD on August 24, 2004, 09:58 PM
How do you add a Menu item through Code?

See PluginMenu (http://www.valhallalegends.com/pub/BCP/SDK/PluginMenu/).  C++, but very straight-forward for porting if you know VB.
Title: Re:Adding Menu Items?
Post by: BaDDBLooD on August 24, 2004, 10:46 PM
Thanks, but i can't port from c++ to VB yet.
Title: Re:Adding Menu Items?
Post by: The-FooL on August 25, 2004, 06:51 AM
Depending on what your adding, you can create menu items, set them not visible by default, then change the caption/visibility during runttime.
Title: Re:Adding Menu Items?
Post by: BaDDBLooD on August 25, 2004, 08:01 AM
Yeah but, i dont want waisted menu items :O
Title: Re:Adding Menu Items?
Post by: Eli_1 on August 25, 2004, 01:38 PM
Set the index to 0, and load new instances of it every time you want another menu (?)
Title: Re:Adding Menu Items?
Post by: Spht on August 25, 2004, 03:35 PM
Quote from: BaDDBLooD on August 24, 2004, 10:46 PM
Thanks, but i can't port from c++ to VB yet.

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):

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;
}


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


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.
Title: Re:Adding Menu Items?
Post by: BaDDBLooD on August 25, 2004, 05:28 PM
Relying on your example *HEAVILY*, i managed to come out with this.



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



Dunno if that's right or not, but that's what i came up with.  Not sure on how to do the WINAPI Ones...
Title: Re:Adding Menu Items?
Post by: MyndFyre on August 25, 2004, 05:48 PM
Quote from: BaDDBLooD on August 25, 2004, 05:28 PM

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

Have you searched the CRT files for:


#define WINAPI /* the code value you're looking for to emulate */

?
Title: Re:Adding Menu Items?
Post by: BaDDBLooD on August 25, 2004, 06:07 PM
Nope.. not sure what a CRT File is lmao
Title: Re:Adding Menu Items?
Post by: MyndFyre on August 25, 2004, 06:25 PM
Quote from: BaDDBLooD on August 25, 2004, 06:07 PM
Nope.. not sure what a CRT File is lmao

C runtime.  I believe the C runtime source is included in the Platform SDK for Win32.  I could be wrong, though.
Title: Re:Adding Menu Items?
Post by: BaDDBLooD on August 25, 2004, 06:27 PM
Could you explain that in less complex terms?
Title: Re:Adding Menu Items?
Post by: MyndFyre on August 25, 2004, 06:29 PM
Quote from: BaDDBLooD on August 25, 2004, 06:27 PM
Could you explain that in less complex terms?

www.google.com.
Title: Re:Adding Menu Items?
Post by: UserLoser. on August 25, 2004, 06:57 PM
You don't need to worry about WINAPI (__stdcall (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_core___stdcall.asp)).  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.