Jump to content
Sign in to follow this  

[Solved] CtrlCreateMenuItem slow speed on many uses

Recommended Posts

When i got home from uni, i was without my computer for 2 months. So i used the time to under-go a huge autoit project for replacing the right-click on desktop for a completely customizated menu system.

For the most part it is finished, but with one slight thing i dont really like and would to like to improve.

At first i thought it was the alorigutm for sorting though files (from which i updated to Melbas RecFileListToArray and though this has made the coding better / easier to understand it was still very slow).

I have finally come to the conclusion the problem lies within the CtrlCreateMenuItem Function.

Below is some simple code that creates a Context Menu with 9,999 Items (The context menu i created is is programed to load Videos, Desktop, Favorites, Downloads and Music which is summed up to around 6,000 so still a big number).

And it takes 1 second to generate all these menus. So you would right-click the desktop and then need to wait 1-2 seconds before the menu appears which isn't very nice (My work around at the moment is turning each section into a link, so when u click videos, it opens a new context menu with the videos loaded. So instead of loading all 6k all the time, just 3k when the videos is clicked.)

Now onto the purpose of the question, Though my work-around works its not a very nice ending to this project so i was wondering if there is a way to make it all faster?

#include 'WindowsConstants.au3'
#include 'MenuConstants.au3'
#include 'MouseOnEvent.au3'

Local $GUIRunning = GUICreate("Running", 1, 1, -5, -5, $WS_POPUP, $WS_EX_TOOLWINDOW)
Local $GUIContextMenu = GUICreate("Context Menu", 1, 1, -5, -5, $WS_POPUP, $WS_EX_TOOLWINDOW)
local $MainContextMenu = GUICtrlCreateContextMenu()
local $ContextHandle = GUICtrlGetHandle($MainContextMenu)
local $MousePosition = Mousegetpos()

For $i = 0 to 9999
GUICtrlCreateMenuItem("Hello Test", $MainContextMenu)

$ContextMenu = _GUICtrlMenu_TrackPopupMenu($ContextHandle, $GUIContextMenu, $MousePosition[0]-10, $MousePosition[1]-10, 1, 1, 2)
$ContextMenuText = _GUICtrlMenu_GetItemText($ContextHandle, $ContextMenu, False)

#Region .Au3 Functions
Func _GUICtrlMenu_TrackPopupMenu($hMenu, $hWnd, $iX = -1, $iY = -1, $iAlignX = 1, $iAlignY = 1, $iNotify = 0, $iButtons = 0)
If $iX = -1 Then $iX = _WinAPI_GetMousePosX()
If $iY = -1 Then $iY = _WinAPI_GetMousePosY()
Local $iFlags = 0
Switch $iAlignX
Case 1
$iFlags = BitOR($iFlags, $TPM_LEFTALIGN)
Case 2
$iFlags = BitOR($iFlags, $TPM_RIGHTALIGN)
Case Else
$iFlags = BitOR($iFlags, $TPM_CENTERALIGN)
Switch $iAlignY
Case 1
$iFlags = BitOR($iFlags, $TPM_TOPALIGN)
Case 2
$iFlags = BitOR($iFlags, $TPM_VCENTERALIGN)
Case Else
$iFlags = BitOR($iFlags, $TPM_BOTTOMALIGN)
If BitAND($iNotify, 1) <> 0 Then $iFlags = BitOR($iFlags, $TPM_NONOTIFY)
If BitAND($iNotify, 2) <> 0 Then $iFlags = BitOR($iFlags, $TPM_RETURNCMD)
Switch $iButtons
Case 1
$iFlags = BitOR($iFlags, $TPM_RIGHTBUTTON)
Case Else
$iFlags = BitOR($iFlags, $TPM_LEFTBUTTON)
Local $aResult = DllCall("User32.dll", "bool", "TrackPopupMenu", "handle", $hMenu, "uint", $iFlags, "int", $iX, "int", $iY, "int", 0, "hwnd", $hWnd, "ptr", 0)
If @error Then Return SetError(@error, @extended, 0)
Return $aResult[0]
EndFunc ;==>_GUICtrlMenu_TrackPopupMenu

Func _GUICtrlMenu_GetItemText($hMenu, $iItem, $fByPos = True)
Local $iByPos = 0
If $fByPos Then $iByPos = $MF_BYPOSITION
Local $aResult = DllCall("User32.dll", "int", "GetMenuStringW", "handle", $hMenu, "uint", $iItem, "wstr", 0, "int", 4096, "uint", $iByPos)
If @error Then Return SetError(@error, @extended, 0)
Return SetExtended($aResult[0], $aResult[3])
EndFunc ;==>_GUICtrlMenu_GetItemText
Edited by IanN1990

Share this post

Link to post
Share on other sites

Probably not, AutoIt is an interpreted language, it's going to be slow especially when you're putting up 6000 items on a right click menu. Can you even see all 3-6K items at the same time on that context menu?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post

Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...