Add Popup Menu to Tray Icon [MS Version 13+]

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Add Popup Menu to Tray Icon [MS Version 13+]

Post by Rain » Sat Dec 10, 2011 4:50 pm

This example illustrates how to add a pop up menu to a system tray icon with Macro Scheduler 13+.

Code: Select all

//Set icon file and directory
Let>IconFile=C:\MyIcon.ico
//Add Tray icon
AddTrayIcon>%IconFile%,MyTrayIcon,Left click to show popup menu
//Add Tray icon Handler to Show Pop up Menu
AddTrayHandler>MyTrayIcon,OnClick,ShowPopupMenu


Label>ActionLoop
Wait>0.1
Goto>ActionLoop


SRT>ShowPopupMenu
//Set Menu Items. The hyphen (-) Is used to separate items
Let>Items=Cancel;-;Exit

//Set Position of the po pup menu by getting the cursor position
GetCursorPos>X,Y

//Display pop up menu at the specified X,Y position
PopupMenu>X,Y,Items,menres

If>menres=0
MessageModal>Cancel Clicked
Endif

If>menres=2,Quit
END>ShowPopupMenu


SRT>Quit
  Exit>1
END>Quit
Online help
http://www.mjtnet.com/manual/

AddTrayIcon
http://www.mjtnet.com/manual/index.html?addtrayicon.htm

AddTrayHandler
http://www.mjtnet.com/manual/index.html ... andler.htm

PopupMenu
http://www.mjtnet.com/manual/index.html?popupmenu.htm
Last edited by Rain on Thu Feb 28, 2013 2:38 pm, edited 1 time in total.

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Thu Feb 28, 2013 2:36 pm

Here are couple more examples.

This example illustrates how to show a tray icon pop up menu only when the right mouse button is clicked.

Code: Select all

//Set icon file and directory
Let>IconFile=C:\MyIcon.ico
//Add Tray icon
AddTrayIcon>%IconFile%,MyTrayIcon,Left or Right click to show popup menu
//Add Tray icon Handler to Show Pop up Menu
AddTrayHandler>MyTrayIcon,OnMouseDown,ShowPopupMenu


Label>ActionLoop
Wait>0.1
Goto>ActionLoop


SRT>ShowPopupMenu
//Get state of left mouse button
Let>VK_LBUTTON=2
LibFunc>user32,GetAsyncKeyState,MouseBtnState,VK_LBUTTON
IF>MouseBtnState<>0


  //Set Right Mouse Button Menu Items.
  //The hyphen (-) Is used to separate items
  Let>Items=Right Mouse Button Menu;-;Exit

  //Set Position of the po pup menu by getting the cursor position
  GetCursorPos>X,Y

  //Display pop up menu at the specified X,Y position
  PopupMenu>X,Y,Items,menres

  If>menres=0
  'MessageModal>Right Mouse Button Menu
  Endif

  If>menres=2
  Gosub>Quit
  ENDIF


ENDIF

END>ShowPopupMenu


SRT>Quit
  Exit>1
END>Quit

This example illustrates how to add a pop up menu when the right mouse button is clicked and how to show and hide a dialog when the left mouse button is clicked.

Code: Select all


Dialog>Dialog1
object Dialog1: TForm
  Left = 519
  Top = 178
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 212
  ClientWidth = 431
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,,OnClose,Quit
Show>Dialog1

//Set icon file and directory
Let>IconFile=C:\MyIcon.ico
//Add Tray icon
AddTrayIcon>%IconFile%,MyTrayIcon,Left or Right click to show popup menu
//Add Tray icon Handler to Show Pop up Menu
AddTrayHandler>MyTrayIcon,OnMouseDown,ShowPopupMenu


Label>ActionLoop
Wait>0.1
Goto>ActionLoop


SRT>ShowPopupMenu
//Get state of left mouse button
Let>VK_LBUTTON=2
LibFunc>user32,GetAsyncKeyState,MouseBtnState,VK_LBUTTON
IF>MouseBtnState<>0


  //Set Right Mouse Button Menu Items.
  //The hyphen (-) Is used to separate items
  Let>Items=Right Mouse Button Menu;-;Exit

  //Set Position of the po pup menu by getting the cursor position
  GetCursorPos>X,Y

  //Display pop up menu at the specified X,Y position
  PopupMenu>X,Y,Items,menres

  If>menres=0
  'MessageModal>Right Mouse Button Menu
  Endif

  If>menres=2
  Gosub>Quit
  ENDIF


ELSE


  //Show or Hide a dialog when the Tray icon is clicked
  //with the left mouse button by setting the 'Visible' property to true or false
  GetDialogProperty>Dialog1,,Visible,Res
  IF>Res=True
    //Hide Dialog1
    SetDialogProperty>Dialog1,,Visible,False
  ELSE
    //Show Dialog
    SetDialogProperty>Dialog1,,Visible,True
  ENDIF


ENDIF

END>ShowPopupMenu


SRT>Quit
  Exit>1
END>Quit

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts