DesktopX: Replacing the Right-Click Menu
From WinCustomize Wiki
| Learning DX Step-By-Step - #7: Replacing the Right-Click Menu (Advanced Scripting) | ||||||||
| ||||||||
Contents |
[edit] Introduction
Today's Lesson: "Replacing the Right-Click Menu"
This lesson is more advanced then the previous ones I have posted. This is something that I have found myself working on a LOT lately. Replacing the standard right-click menu that DX puts on everything with my own menu. There are lots of reasons for this.
- You want to remove the DX About box
- You want to add more functions to this menu
- You just don't like the standard menu, whatever...
- You want to hide the standard preferences box
- If you make it with PRO, you dont want the "CREATED WITH DESKTOPX PRO" on the about box!!
This is not going to be a simple STEP-BY-STEP, I'm assuming if you are this advanced into DX, I don't need to explain how to get the script windows up, or edit properties! This is more like a SCRIPT example, not a step-by-step.
I have uploaded this as a widget for you to download and enjoy HERE
For this and all the Step-By-Step DX Tutorials you will need to purchase DesktopX for $14.95 from Stardock.
Lets get started.
[edit] Replacing the Right-Click Menu
[edit] STEP 1 - RegisterController
In order for DX to ignore the built-in right-click menu, you have to work a little magic. Its not that complex, but it has to be setup in order to work.
- Create a new object (see Tutorial #1)
- Make it whatever you want, image/text.
- Add the following to the script
'Place this at the TOP of your code. Dim IsMinimized 'We will use this to determine if the widget is minimized or not. Dim ShowMenu 'Used to decide if we need to show/not show the right click menu Sub Object_OnScriptEnter IsMinimized = False 'Set the IsMinimized to False ShowMenu = False 'Use to set the right-click menu NOT to show desktopx.RegisterController Object.Name 'this code sets this object as the control for handling the right-clicks, etc. The ides is to use the script into the MAIN object, and never add this in more then 1 place!! It will mess things up. End Sub
[edit] STEP 2 - OnControl Function
The OnControl function handles clicks on the tray icon, as well as right clicks coming from your widget.
- Add the code below to the above script.
Function OnControl(lType, lCode)
OnControl = True 'Allow control to run
ShowMenu = True 'Show the menu - by default
'These codes are there to allow you to use the RIGHT-CLICK from the tray icon, as well as the RIGHT-CLICK from the widget.
If lType=2 And lCode=517 Then ShowMenu = True ' RIGHT CLICK ON TRAY ICON
If lType=1 And lCode=2 Then ShowMenu = True ' RIGHT CLICK ON ITEM
If ShowMenu = True Then ' If we right-clicked on the tray or widget then...
OnControl = True 'allow control to pass to the object
If IsMinimized = False Then 'If the object isnt minimized (we will set this later in the popup menu) run the main popupmenu / else run the minimize popupmenu minimized function.
Call PopupMenu
Else
Call PopupMenuMin
End If
End If
End Function
[edit] STEP 3 - Adding the POPUP Menu
The popup menu is the menu you want to show up when you RIGHT-CLICK on the widget/tray when the widget is not minimized, etc. (We will do the minimized version in a min).
I am not going to rip this all apart, there is another tutorial out there on making a right-click menu
Function PopupMenu()
Set mainmenu = nothing
Set mainmenu = DesktopX.CreatePopupMenu '-- Create Main Men
If widget.Autorun=False Then
mainmenu.AppendMenu 0, 1, "Autorun OFF"
Else
mainmenu.AppendMenu &H00000008, 1, "Autorun ON"
End If
mainmenu.AppendMenu 0, 2, "Minimize to Tray"
mainmenu.AppendMenu 0, 3, "Close"
mainmenu.AppendMenu &H00000800, 100, "seperator"
mainmenu.AppendMenu 0, 4, "Preference"
mainmenu.AppendMenu 0, 5, "About"
'-- Save the results of mouse movements
result = mainmenu.TrackPopupMenu(0, System.CursorX, System.CursorY)
Select Case result
Case 1
If widget.Autorun = False Then
widget.Autorun = True
Else
widget.Autorun = False
End If
Case 2
IsMinimized = True
object.Visible = False
Case 3
widget.Close
Case 4
widget.OpenProperties
Case 5
msgbox "This is my about box"
End Select
End Function
[edit] STEP 4 - Minimized (Tray icon) menu
We are going to add a minimized menu, so that we have a way to restore the widget back to working order from minimized state. Its not going to have all the functions of the main popup menu.
This menu will only be accessible with its minimized and from the tray icon.
Function PopupMenuTray()
Set mainmenu = nothing
Set mainmenu = DesktopX.CreatePopupMenu '-- Create Main Menu
mainmenu.AppendMenu 0, 1, "Restore from Tray"
mainmenu.AppendMenu 0, 2, "Close"
'-- Save the results of mouse movements
result = mainmenu.TrackPopupMenu(0, System.CursorX, System.CursorY)
Select Case result
Case 1
IsMinimized = False
object.Visible = True
Case 2
widget.Close
End Select
End Function
[edit] STEP 5 - Add Left-Click menu
If you want you can also make the popup menu show when you LEFT-CLICK on the object as well. This is a simple bit of code to be added to the object.
Function Object_OnLButtonUp(x,y,dragged) If dragged = False Then Call PopupMenu End If End Function
[edit] Conclusion
This should all work, there are a lot of options in the ONCONTROL function, as far as clicking works, this shuts off the CLICK HIDE/CLICK RESTORE on the tray icon as well, it has a way of messing things up.
This is not something i would recommend someone doing unless you really know DX. One side effect of this is that you cant right-click on the main object anymore, it wont work, you will just get your menu (when editing things!!). AND you will not be able to right-click the DX TRAY icon to pick edit, etc. So just a warning.
I hope you have enjoyed this step into DX, and look forward to the next installment..
Enjoy, RomanDA aka David A. Roman
