Jump to content

How to insert menu item to created menu?


MrCreatoR
 Share

Recommended Posts

Hi,

I trying to understand, why i can't insert an item to menu that was created by my script, but in previous session?

Here is an example of what i mean:

#include <GuiMenu.au3>
#include <WinAPI.au3>

If Not StringInStr($CmdLineRaw, "/Test/") Then
    Run(@WindowsDir & "\Notepad.exe")
    WinWait("[CLASS:Notepad]")
    
    $hMainWnd = WinGetHandle("[CLASS:Notepad]")
    
    $hMainMenu = _GUICtrlMenu_GetMenu($hMainWnd)
    $hCreatedMenu = _GUICtrlMenu_CreateMenu()
    
    _GUICtrlMenu_InsertMenuItem($hMainMenu, 5, "New Menu", 1001, $hCreatedMenu)
    _GUICtrlMenu_DrawMenuBar($hMainWnd)
    
    Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /Test/')
    
    Exit
EndIf

;Now i want to add an item (this is the new session, or another script runing)
$hMainWnd = WinGetHandle("[CLASS:Notepad]")

$sAddedItemName = "My Item"

$hMainMenu = _GUICtrlMenu_GetMenu($hMainWnd)
$hFileMenu = _GUICtrlMenu_GetItemSubMenu($hMainMenu, 5) ;Change to 0, and this works (it appears in "File" menu) :(

$iRet = _GUICtrlMenu_InsertMenuItem($hFileMenu, -1, $sAddedItemName, 1002)
_GUICtrlMenu_DrawMenuBar($hMainWnd)

If Not $iRet Then _WinAPI_ShowError(_WinAPI_GetLastErrorMessage()) ;This one tells me that there is wrong menu descripter (handle?)

Run it and check the new menu, there is no "My Item" inside :(

While this example works fine, because this is all created and inserted in one session:

#include <GuiMenu.au3>
#include <WinAPI.au3>

;~ If Not StringInStr($CmdLineRaw, "/Test/") Then
    Run(@WindowsDir & "\Notepad.exe")
    WinWait("[CLASS:Notepad]")
    
    $hMainWnd = WinGetHandle("[CLASS:Notepad]")
    
    $hMainMenu = _GUICtrlMenu_GetMenu($hMainWnd)
    $hCreatedMenu = _GUICtrlMenu_CreateMenu()
    
    _GUICtrlMenu_InsertMenuItem($hMainMenu, 5, "New Menu", 1001, $hCreatedMenu)
    _GUICtrlMenu_DrawMenuBar($hMainWnd)
    
;~  Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /Test/')
    
;~  Exit
;~ EndIf

;Now i want to add an item (this is the new session, or another script runing)
;~ $hMainWnd = WinGetHandle("[CLASS:Notepad]")

$sAddedItemName = "My Item"

$hMainMenu = _GUICtrlMenu_GetMenu($hMainWnd)
$hFileMenu = _GUICtrlMenu_GetItemSubMenu($hMainMenu, 5);Change to 0, and this works (it appears in "File" menu) :(

$iRet = _GUICtrlMenu_InsertMenuItem($hFileMenu, -1, $sAddedItemName, 1002)
_GUICtrlMenu_DrawMenuBar($hMainWnd)

If Not $iRet Then _WinAPI_ShowError(_WinAPI_GetLastErrorMessage());This one tells me that there is wrong menu descripter (handle?)

As you can see, i only commented few lines to marge it to one session, no lines were changed.

What is wrong here? :P

Edited by MrCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Try this:

#include <GuiMenu.au3>
#include <WinAPI.au3>

If Not StringInStr($CmdLineRaw, "/Test/") Then
    Run(@WindowsDir & "\Notepad.exe")
    WinWait("[CLASS:Notepad]")
    
    $hMainWnd = WinGetHandle("[CLASS:Notepad]")
    
    $hMainMenu = _GUICtrlMenu_GetMenu($hMainWnd)
    $hCreatedMenu = _GUICtrlMenu_CreateMenu()
    
    _GUICtrlMenu_InsertMenuItem($hMainMenu, 5, "New Menu", 1001, $hCreatedMenu)
    _GUICtrlMenu_DrawMenuBar($hMainWnd)
    
    Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /Test/')
    
    Exit
EndIf

;Now i want to add an item (this is the new session, or another script runing)
$hMainWnd = WinGetHandle("[CLASS:Notepad]")
$hMainMenu = _GUICtrlMenu_GetMenu($hMainWnd)

$sAddedItemName = "My Item"

$hMainMenu = _GUICtrlMenu_GetMenu($hMainWnd)
$hFileMenu = _GUICtrlMenu_GetItemSubMenu($hMainMenu, 5) ;Change to 0, and this works (it appears in "File" menu) 

$iRet = _GUICtrlMenu_InsertMenuItem($hFileMenu, -1, $sAddedItemName, 1002)
_GUICtrlMenu_DrawMenuBar($hMainWnd)

If Not $iRet Then _WinAPI_ShowError(_WinAPI_GetLastErrorMessage()) ;This one tells me that there is wrong menu descripter (handle?)

added $hMainMenu = _GUICtrlMenu_GetMenu($hMainWnd) after second $hMainWnd = WinGetHandle("[CLASS:Notepad]")

Link to comment
Share on other sites

@Zedna

It's the same, is it works for you? :P

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

I think problem is inside _GUICtrlMenu_InsertMenuItem()

If you compare its sources with for example _GUICtrlListView_InsertItem()

you will see the difference:

There is missing part using _MemInit, _MemWrite

I think this is "bug" that for menu it isn't implemented the same way as for listview/treeview.

Edited by Zedna
Link to comment
Share on other sites

Here is my try to implement memory functions into _GUICtrlMenu_InsertMenuItem2

but unfortunatelly it's not working - there is error message when you will run example.

So I put it here just for your info - maybe I'm very close to right solution :-)

#include <GuiMenu.au3>
#include <WinAPI.au3>
#include <Memory.au3>

If Not StringInStr($CmdLineRaw, "/Test/") Then
    Run(@WindowsDir & "\Notepad.exe")
    WinWait("[CLASS:Notepad]")
    
    $hMainWnd = WinGetHandle("[CLASS:Notepad]")
    
    $hMainMenu = _GUICtrlMenu_GetMenu($hMainWnd)
    $hCreatedMenu = _GUICtrlMenu_CreateMenu()
    
    $iRet = _GUICtrlMenu_InsertMenuItem2($hMainMenu, 5, "New Menu", 1001, $hCreatedMenu)
;~ ConsoleWrite($iRet & @CRLF)
    _GUICtrlMenu_DrawMenuBar($hMainWnd)
    
    Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /Test/')
    
    Exit
EndIf

;Now i want to add an item (this is the new session, or another script runing)
$hMainWnd = WinGetHandle("[CLASS:Notepad]")
$hMainMenu = _GUICtrlMenu_GetMenu($hMainWnd)

$sAddedItemName = "My Item"

$hMainMenu = _GUICtrlMenu_GetMenu($hMainWnd)
$hFileMenu = _GUICtrlMenu_GetItemSubMenu($hMainMenu, 5) ;Change to 0, and this works (it appears in "File" menu) 

$iRet = _GUICtrlMenu_InsertMenuItem2($hFileMenu, -1, $sAddedItemName, 1002)
ConsoleWrite($iRet & @CRLF)
MsgBox(0,'',$iRet & @CRLF)
_GUICtrlMenu_DrawMenuBar($hMainWnd)

If Not $iRet Then _WinAPI_ShowError(_WinAPI_GetLastErrorMessage()) ;This one tells me that there is wrong menu descripter (handle?)

Func _GUICtrlMenu_InsertMenuItem2($hMenu, $iIndex, $sText, $iCmdID = 0, $hSubMenu = 0)
 Local $pMenu, $tMenu, $tText, $aResult, $hWnd
 Local $iBuffer, $pBuffer, $tBuffer, $iItem, $pItem, $tItem, $pMemory, $tMemMap, $pText, $iMask, $iResult

 $tMenu = DllStructCreate($tagMENUITEMINFO)
 $pMenu = DllStructGetPtr($tMenu)
 DllStructSetData($tMenu, "Size", DllStructGetSize($tMenu))
 DllStructSetData($tMenu, "Mask", BitOR($MIIM_ID, $MIIM_STRING, $MIIM_SUBMENU))
 DllStructSetData($tMenu, "ID", $iCmdID)
 DllStructSetData($tMenu, "SubMenu", $hSubMenu)
 If $sText = "" Then
    DllStructSetData($tMenu, "Mask", $MIIM_FTYPE)
    DllStructSetData($tMenu, "Type", $MFT_SEPARATOR)
 Else
    DllStructSetData($tMenu, "Mask", BitOR($MIIM_ID, $MIIM_STRING, $MIIM_SUBMENU))
    $tText = DllStructCreate("char Text[" & StringLen($sText) + 1 & "]")
    $pBuffer = DllStructGetPtr($tText)
    DllStructSetData($tText, "Text", $sText)
    DllStructSetData($tMenu, "TypeData", DllStructGetPtr($tText))
 EndIf
 
 ; add on - start
 $hWnd = _GUICtrlMenu_FindParent($hMenu)
 $iBuffer = StringLen($sText) + 1
 $iItem = DllStructGetSize($tMenu)
 ConsoleWrite($iItem + $iBuffer & @CRLF)
 $pMemory = _MemInit($hWnd, $iItem + $iBuffer, $tMemMap)
 $pText = $pMemory + $iItem
 DllStructSetData($tMenu, "TypeData", $pText)
 _MemWrite($tMemMap, $pMenu, $pMemory, $iItem)
 _MemWrite($tMemMap, $pBuffer, $pText, $iBuffer)
 $aResult = DllCall("User32.dll", "int", "InsertMenuItem", "hwnd", $hMenu, "int", $iIndex, "int", True, "ptr", $pMemory)
 _MemFree($tMemMap)
 ; add on - end
 Return $aResult[0] <> 0
EndFunc   ;==>_GUICtrlMenu_InsertMenuItem
Link to comment
Share on other sites

Thanks Zedna, i will play with it later...

But i think i know what is the problem. When the script is exit, all handles are closed, and the menu resources are released (destroyed). And the InsertItem function need that these resources will be opened (accessable).

Just look at this example, instead of 5 (it is the inserted menu item) i used 0 (File menu), in the current conditions this should work, item inserted to the File menu, but i added one line wich is destroyed the main menu, and as i expected, this not working anymore:

#include <GuiMenu.au3>
#include <WinAPI.au3>

If Not StringInStr($CmdLineRaw, "/Test/") Then ;Comment
    Run(@WindowsDir & "\Notepad.exe")
    WinWait("[CLASS:Notepad]")
    
    $hMainWnd = WinGetHandle("[CLASS:Notepad]")
    
    $hMainMenu = _GUICtrlMenu_GetMenu($hMainWnd)
    $hCreatedMenu = _GUICtrlMenu_CreateMenu()
    
    _GUICtrlMenu_InsertMenuItem($hMainMenu, 5, "New Menu", 1000, $hCreatedMenu)
    _GUICtrlMenu_DrawMenuBar($hMainWnd)
    
    ;Added, so the menu resources is now released, destroyed!
    _GUICtrlMenu_DestroyMenu($hMainMenu)
    
    Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /Test/ ' & $hCreatedMenu) ;Comment
    
    Exit ;Comment
EndIf ;Comment

;Now i want to add an item (this is the new session, or another script runing)
$hMainWnd = WinGetHandle("[CLASS:Notepad]") ;Comment

$sAddedItemName = "My Item"

$hMainMenu = _GUICtrlMenu_GetMenu($hMainWnd)

;Changed to 0, "My Item" should be inserted to "File" menu,
;but since the menu is destroyed, this causing the same problem as with my custom created menu.
$hFileMenu = _GUICtrlMenu_GetItemSubMenu($hMainMenu, 0)

$iRet = _GUICtrlMenu_InsertMenuItem($hFileMenu, -1, $sAddedItemName, 1001)
_GUICtrlMenu_DrawMenuBar($hMainWnd)

If Not $iRet Then
    _WinAPI_ShowError(_WinAPI_GetLastErrorMessage(), 0) ;This one tells me that there is wrong menu descripter (handle?)
    WinClose($hMainWnd)
EndIf

Now when we know the problem, we should look for a way to do one of two things... 1) Find out how to force autoit script not destroying the menu on Exit (if it's possible at all), or 2) How to load that menu, or just set it as "not destroyed" :P There is function _GUICtrlMenu_LoadMenu(), but i can't make it work :(

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

BTW: While playing with _GUICtrlMenu_* functions, completely by accident i came up with new function: _GUICtrlMenu_DuplicateMenu() - Not sure where we can use it, but just in case here it is :P:

#include <GuiMenu.au3>

Run(@WindowsDir & "\Notepad.exe")
WinWait("[CLASS:Notepad]")

$hMainWnd = WinGetHandle("[CLASS:Notepad]")

$hMainMenu = _GUICtrlMenu_GetMenu($hMainWnd)
$sDuplMenuText = _GUICtrlMenu_GetItemText($hMainMenu, 0)

_GUICtrlMenu_DuplicateMenu($hMainMenu, 0, 1, $sDuplMenuText & " 2")
_GUICtrlMenu_DrawMenuBar($hMainWnd)

Func _GUICtrlMenu_DuplicateMenu($hMain, $iSrcItem, $iDstItem, $sDuplMenuText)
    Local $hSubMenu = _GUICtrlMenu_GetItemSubMenu($hMain, $iSrcItem)
    _GUICtrlMenu_InsertMenuItem($hMain, $iDstItem, $sDuplMenuText, 0, $hSubMenu)
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...