Learning DX Step-By-Step - #7

Replacing the RIGHT-CLICK menu! - Advanced Scripting

By on March 1, 2007 12:49:37 PM from WinCustomize Forums WinCustomize Forums

RomanDA

Join Date 12/2003
+147
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
' 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 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"
  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

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

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
http://romanda.wincustomize.com
9 Replies
Search this post
Subscription Options


Reason for Karma (Optional)
Successfully updated karma reason!
March 1, 2007 12:57:43 PM from WinCustomize Forums WinCustomize Forums
Wow!  Wicked cool!
Thanks!
Reason for Karma (Optional)
Successfully updated karma reason!
March 2, 2007 12:51:28 PM from WinCustomize Forums WinCustomize Forums
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  .

Eddie

Reason for Karma (Optional)
Successfully updated karma reason!
March 2, 2007 1:08:23 PM from WinCustomize Forums WinCustomize Forums
No problems Eddie.. as long as someone is enjoying them and getting something out of them its worth it.
Reason for Karma (Optional)
Successfully updated karma reason!
March 3, 2007 5:36:22 AM from WinCustomize Forums WinCustomize Forums


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

Ronan H
Reason for Karma (Optional)
Successfully updated karma reason!
March 3, 2007 5:10:02 PM from WinCustomize Forums WinCustomize Forums
Very well done.

This seems to be one of the more popular questions lately.
Reason for Karma (Optional)
Successfully updated karma reason!
March 9, 2007 5:35:02 PM from WinCustomize Forums WinCustomize Forums
See update for link to widget with this already created for you.
Reason for Karma (Optional)
Successfully updated karma reason!
June 27, 2007 6:16:44 PM from WinCustomize Forums WinCustomize Forums
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?





Reason for Karma (Optional)
Successfully updated karma reason!
February 10, 2008 7:14:18 AM from WinCustomize Forums WinCustomize Forums
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

ie:

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..
Reason for Karma (Optional)
Successfully updated karma reason!
February 11, 2008 6:10:56 AM from WinCustomize Forums WinCustomize Forums
Great!  
Reason for Karma (Optional)
Successfully updated karma reason!
Stardock Forums v1.0.0.0    #108432  walnut2   Server Load Time: 00:00:00.0001031   Page Render Time: