Learning DX Step-By-Step - #7

Replacing the RIGHT-CLICK menu! - Advanced Scripting

Thursday, March 1, 2007 by RomanDA | Discussion: DesktopX Tutorials

Script Tutorials

#7 - Replacing the Right-Click Menu

A series by RomanDA

Listing of other DX Tutorials: Click here
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.

---- UPDATE - REVISED THE CODE and 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.

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

  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
    Call PopupMenuTray
  End If
 End If
End Function

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"
    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
        widget.Autorun = False
      End If
    Case 2
      IsMinimized = True
      object.Visible = False
    Case 3
    Case 4
    Case 5
      msgbox "This is my about box"
  End Select
End Function

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
  End Select
End Function
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


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..

AKA: David A. Roman
Reply #1 Thursday, March 1, 2007 12:57 PM
Wow!  Wicked cool!
Reply #2 Friday, March 2, 2007 12:51 PM
Thanks David,

I saved it for later use.I'm not there yet.I save all your articles. Really do appreciate them I learn something from all of them  .


Reply #3 Friday, March 2, 2007 1:08 PM
No problems Eddie.. as long as someone is enjoying them and getting something out of them its worth it.
Ronan H
Reply #4 Saturday, March 3, 2007 5:36 AM

Thanks RomanDA, this is very much related to threads i have been knocking about in recently, as you know! Nice one...

Ronan H
Reply #5 Saturday, March 3, 2007 5:10 PM
Very well done.

This seems to be one of the more popular questions lately.
Reply #6 Friday, March 9, 2007 5:35 PM
See update for link to widget with this already created for you.
Reply #7 Wednesday, June 27, 2007 6:16 PM
Just started using this and it's great. Thanks!

Just wanted to mention this line:

Call PopupMenuMin

doesn't match the actual function's name: PopupMenuTray()

Also, any ideas why when I export my widget I have to click twice on the systray icon? I click it once and it flashes. Then I have to click it again.

And is there any way to make it disappear if I don't select anything from the menu?

Reply #8 Sunday, February 10, 2008 7:14 AM
Call PopupMenuMin

doesn't match the actual function's name: PopupMenuTray()

fixed code.. thanks.

im not sure about the double clicks, the best way to test things is to make a TEXT Object and have it pass all the codes being sent by the right-click handeler to it to see what codes make things work.
Create "TEST" object


Function OnControl(lType, lCode)
OnControl = True 'Allow control to run
desktopx.object("TEST").text = desktopx.object("TEST").text & vbnewline
desktopx.object("TEST").text = "LType: " & lType & "LCode: " & lCode
End Function

This way you can see what its passing..
Reply #9 Monday, February 11, 2008 6:10 AM

Please login to comment and/or vote for this skin.

Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:

  • Richer content, access to many features that are disabled for guests like commenting on the forums and downloading skins.
  • Access to a great community, with a massive database of many, many areas of interest.
  • Access to contests & subscription offers like exclusive emails.
  • It's simple, and FREE!