Jump to content

[Solved] Using Context Menus to load Desktop Files


Recommended Posts

I was looking though the forums and saw context menus, and thought it would be cool to make my own context menu.

#include<WindowsConstants.au3>
#include<GUIMenu.au3>
#include <File.au3>
#include <Array.au3>

$hGUI = GUICreate("", 1, 1, -5, -5, $WS_POPUP, $WS_EX_TOOLWINDOW)
$ctlMenu = GUICtrlCreateContextMenu()
$ctlMenuAlert = GUICtrlCreateMenu("Computer", $ctlMenu)
$ctlMenuClose = GUICtrlCreateMenu("Desktop", $ctlMenuAlert)
$ctlMenuClose1 = GUICtrlCreateMenuItem("Close", $ctlMenuClose)

Local $FileList = _FileListToArray(@DesktopDir)
While UBound($FileList)
    GUICtrlCreateMenuItem(_ArrayPop($FileList), $ctlMenuClose)
WEnd

GUISetState(@SW_SHOW, $hGUI)
_GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($ctlMenu), $hGUI, -1, -1, 1, 1, 1)
GUISetState(@SW_HIDE, $hGUI)

While 1
     Switch GUIGetMsg()
        Case $ctlMenuClose1
            MsgBox(0, '', "Closing")
    EndSwitch
WEnd

Code using MouseOnEvent.Au3 so when you right click it brings up the menu

#include<WindowsConstants.au3>
#include<GUIMenu.au3>
#include "MouseOnEvent.au3"
#include <File.au3>
#include <Array.au3>

_MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT, "_SecondaryDown", "", "")
$hGUI = GUICreate("", 1, 1, -5, -5, $WS_POPUP, $WS_EX_TOOLWINDOW)

$ctlMenu = GUICtrlCreateContextMenu()
$ctlMenuAlert = GUICtrlCreateMenu("Computer", $ctlMenu)
$ctlMenuClose = GUICtrlCreateMenu("Desktop", $ctlMenuAlert)
$ctlMenuClose1 = GUICtrlCreateMenuItem("Close", $ctlMenuClose)
Local $FileList = _FileListToArray(@DesktopDir)
While UBound($FileList)
    GUICtrlCreateMenuItem(_ArrayPop($FileList), $ctlMenuClose)  
WEnd

While 1
     Switch GUIGetMsg()
        Case $ctlMenuClose1
            MsgBox(0, '', $ctlMenuClose1)
    EndSwitch
WEnd

Func _SecondaryDown()
     GUISetState(@SW_SHOW, $hGUI)
sleep(250)
    _GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($ctlMenu), $hGUI, -1, -1, 1, 1, 1)
    GUISetState(@SW_HIDE, $hGUI)
EndFunc

This code brings up a context menu showing you all your desktop items but i dont know how to go about making it so after you select one it then loads it. So i welcome all ideas ;)

Edited by IanN1990
Link to comment
Share on other sites

#include<WindowsConstants.au3>
#include<GUIMenu.au3>
#include <File.au3>
#include <Array.au3>
$hGUI = GUICreate("", 1, 1, -5, -5, $WS_POPUP, $WS_EX_TOOLWINDOW)
$ctlMenu = GUICtrlCreateContextMenu()
$ctlMenuAlert = GUICtrlCreateMenu("Computer", $ctlMenu)
$ctlMenuClose = GUICtrlCreateMenu("Desktop", $ctlMenuAlert)
$ctlMenuClose1 = GUICtrlCreateMenuItem("Close", $ctlMenuClose)
Local $FileList = _FileListToArray(@DesktopDir)
For $i = 1 to $FileList[0]
   GUICtrlCreateMenuItem($FileList[$i], $ctlMenuClose)
Next
;While 1
$ret = _GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($ctlMenu), $hGUI, -1, -1, 1, 1, 2)
$stext = _GUICtrlMenu_GetItemText(GUICtrlGetHandle($ctlMenu), $ret, False)
MsgBox(0, "Text", $stext)
ShellExecute(@DesktopDir & "" & $stext)
;WEnd

Commented out the loop as it doesn't really make sense in the example.

While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

One last question, Sense you posted your code i have been working on it.

#include<GUIMenu.au3>
#include<File.au3>
#include<MouseOnEvent.au3>


$hGUI = GUICreate("", 1, 1, -5, -5, $WS_POPUP, $WS_EX_TOOLWINDOW)
$ctlMenu = GUICtrlCreateContextMenu()
$ctlMenuAlert = GUICtrlCreateMenu("Computer", $ctlMenu)
$ctlMenuClose = GUICtrlCreateMenu("Desktop", $ctlMenuAlert)
$ctlMenuClose1 = GUICtrlCreateMenuItem("Close", $ctlMenuClose)

_MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT, "_SecondaryDown", "", "")


Local $FileList = _FileListToArray(@DesktopDir)
For $i = 1 to $FileList[0]
   GUICtrlCreateMenuItem($FileList[$i], $ctlMenuClose)
Next
 
while 1
   sleep(500)
   ConsoleWrite("A")
WEnd


Func _SecondaryDown()
$ret = _GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($ctlMenu), $hGUI, -1, -1, 1, 1, 2)
$stext = _GUICtrlMenu_GetItemText(GUICtrlGetHandle($ctlMenu), $ret, False)

If $sText = "Close" Then
   MsgBox(0, '', "Closing")
Elseif $sText = "" Then
Else
   ShellExecute(@DesktopDir & "" & $stext)
EndIf
EndFunc

The code does everything i want but i know when it gets to

$ret = _GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($ctlMenu), $hGUI, -1, -1, 1, 1, 2)

$stext = _GUICtrlMenu_GetItemText(GUICtrlGetHandle($ctlMenu), $ret, False)

the script hangs "The main While loop doesn't run".

Is there any way to keep things as they are but having this context menu and main while loop running at the same time ?

Link to comment
Share on other sites

I don't have the mouseevent UDF loaded, but for example this works;

#include <GUIMenu.au3>
#include <File.au3>
#include <Misc.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
;#include<MouseOnEvent.au3>
$hGUI = GUICreate("", 1, 1, -5, -5, $WS_POPUP, $WS_EX_TOOLWINDOW)
$ctlMenu = GUICtrlCreateContextMenu()
$ctlMenuAlert = GUICtrlCreateMenu("Computer", $ctlMenu)
$ctlMenuClose = GUICtrlCreateMenu("Desktop", $ctlMenuAlert)
$ctlMenuClose1 = GUICtrlCreateMenuItem("Close", $ctlMenuClose)
;_MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT, "_SecondaryDown", "", "")
Local $FileList = _FileListToArray(@DesktopDir)
For $i = 1 to $FileList[0]
   GUICtrlCreateMenuItem($FileList[$i], $ctlMenuClose)
Next
while 1
   sleep(10)
   If _IsPressed(02) Then ;right click mouse button
    _SecondaryDown()
   EndIf
   ;ConsoleWrite("A")
WEnd

Func _SecondaryDown()
$ret = _GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($ctlMenu), $hGUI, -1, -1, 1, 1, 2)
$stext = _GUICtrlMenu_GetItemText(GUICtrlGetHandle($ctlMenu), $ret, False)
If $sText = "Close" Then
   MsgBox(0, '', "Closing")
Elseif $sText = "" Then
Else
   ShellExecute(@DesktopDir & "" & $stext)
EndIf
EndFunc

I can tell you your main sleep is waaaay too big, lower it down to 10, and try it before you do anything else. If that doesn't work and you can't grab what you need from the code posted above, let me know and I'll download the mouseevent UDF and try it exactly as you are.

Edited by someone
While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

Hey, Sorry for not replying yesterday but i was needed for some things. I spent most of the morning coding and have come up with a nice finished result for this little project.

Main System Exe

#NoTrayIcon
#include<MouseOnEvent.au3>

Global $tStruct = DllStructCreate($tagPOINT)
local $Cpu = 0
local $Test
local $Temp

while 1
   sleep(250)
   DllStructSetData($tStruct, "x", MouseGetPos(0))
   DllStructSetData($tStruct, "y", MouseGetPos(1))
   $hWnd = _WinAPI_WindowFromPoint($tStruct)

   If WinGetTitle($hWnd) = "FolderView" then
     If WinActive("Program Manager") and $Cpu = 1 Then
     _MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT)
    _MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT)
     $Cpu = 0
     Elseif Not(WinActive("Program Manager")) and $Cpu = 0 Then
     _MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT, "_SecondaryDown", "", "")
     _MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT, "__Dummy", "", "")
     $Cpu = 1
EndIf
   Elseif $Cpu = 1 Then
  _MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT)
  _MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT)
  $Cpu = 0
EndIf

If WinExists("Context Menu") Then
$hWnd = _WinAPI_GetAncestor($hWnd, 1)
  
If Not($Test = $hWnd) Then
$Temp +=1
If $Temp = 5 Then
$Temp = 0
ProcessClose("SecondaryExe.exe")
EndIf
EndIf
EndIf
WEnd

Func _SecondaryDown()
If WinExists("Context Menu") Then ProcessClose("SecondaryExe.exe")
Run("C:\Users\UserName\Desktop\SecondaryExe.exe")
sleep(250)
$Test = _WinAPI_GetAncestor(WinGetHandle("[Class:#32768]"), 1)
$Temp = 0
EndFunc

Context Menu Exe

#NoTrayIcon
#include<GUIMenu.au3>
#include<File.au3>

Local $FileList = _FileListToArray(@DesktopDir)
$Temp = Mousegetpos()

$hGUI = GUICreate("Context Menu", 1, 1, -5, -5, $WS_POPUP, $WS_EX_TOOLWINDOW)
$ctlMenu = GUICtrlCreateContextMenu()
$ctlMenuAlert = GUICtrlCreateMenu("Computer", $ctlMenu)
$ctlMenuClose = GUICtrlCreateMenu("Desktop", $ctlMenuAlert)
$ctlMenuClose1 = GUICtrlCreateMenuItem("Close", $ctlMenuClose)

Local $FileList = _FileListToArray(@DesktopDir)
For $i = 1 to $FileList[0]
   If Not(StringRight($FileList[$i], 4) = ".ini") Then GUICtrlCreateMenuItem($FileList[$i], $ctlMenuClose)
Next
$FileList = ""

$ret = _GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($ctlMenu), $hGUI, $Temp[0]-10, $Temp[1]-10, 1, 1, 2)
$stext = _GUICtrlMenu_GetItemText(GUICtrlGetHandle($ctlMenu), $ret, False)

If $sText = "Close" Then
   MsgBox(0, '', "Closing", 1)
Elseif $sText = "" Then
Else
   ShellExecute(@DesktopDir & "\" & $stext)
EndIf

The Code does the following, In the Main Exe if the mouse is over the desktop and the desktop is not active when you right click it brings up the context menu. If the mouse hovers off the menu for more then 1.25 seconds the menu is closed.

The reason why i split it into two exes is for two reasons.

One. Having all the code in 1 exe lagged a bit when you right clicked

Two. The line, Stops other code in the exe being run "sense i know autoit is not muti-thread and trying to use the Abli didn't work out very well i thought the simplest way was to just make a separate exe for it.

$ret=_GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($ctlMenu),$hGUI,-1,-1, 1, 1, 2)
Edited by IanN1990
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...