Rabu, 27 Agustus 2014

Using FoxTray ActiveX control: System Tray Icon and menu attached to VFP form

Before you begin:
Download an archive file with FoxTray.ocx. Check the Readme file.



Register control before testing the VFP code below:
REGSVR32 FoxTray.ocx (use full path to the file).

This control has been created in VB6 and so -- alas! -- requires certain VB support files installed on your computer: MSVBVM60.DLL

See also:
  • Adding icon to the systray (requires VFP9) 
  • Adding user-defined items to the Control Menu of VFP form (requires VFP9)
  •  
    #DEFINE ccIcon  "house.ico"  && replace with valid ICO file name
     
    PUBLIC objForm
    objForm = CreateObject("Tform")
    objForm.Visible = .T.
     
    DEFINE CLASS Tform As Form
        Width=400
        Height=240
        MaxButton=.F.
        MinButton=.F.
        Autocenter=.T.
        Caption = " Using Systray icon and menu"
     
        ADD OBJECT cmdShowIcon As CommandButton WITH;
        Caption="Show Icon", Width=100, Height=27,;
        Left=20, Top=20
     
        ADD OBJECT cmdHideIcon As CommandButton WITH;
        Caption="Hide Icon", Width=100, Height=27,;
        Left=20, Top=56
     
        ADD OBJECT chPopup As CheckBox WITH;
        Caption=" Popup enabled", Value=.T.,;
        Left=240, Top=20, Autosize=.T., BackStyle=1
     
    PROCEDURE Init
        THIS.AddTrayCtrl
     
    PROCEDURE AddTrayCtrl
        LOCAL lErr
        ON ERROR lErr = .T.
        THIS.AddObject("FoxTray", "TFoxTray")
        ON ERROR
        IF lErr
            = MessageB("ActiveX control not registered   " + Chr(13) +;
                "or VB support not available.            " + Chr(13) + Chr(13) +;
                "Class: FoxTrayCtl.cFoxTray              " + Chr(13) +;
                "File: FoxTray.ocx   " + Chr(13) +;
                "VB support: msvbvm60.dll   ", 48, " FoxTray Control")
        ENDIF
     
    PROCEDURE cmdShowIcon.Click
        WITH ThisForm.FoxTray
            .IconSource = ccIcon
            .ShowIcon
        ENDWITH
     
    PROCEDURE cmdHideIcon.Click
        ThisForm.FoxTray.HideIcon
    ENDDEFINE
     
    DEFINE CLASS TFoxTray As OLEControl
        OleClass="FoxTrayCtl.cFoxTray"
     
    PROCEDURE Init
        WITH THIS
            .IconSource = ccIcon
            .IconTip = "FoxPro App"
            .ShowIcon
     
            * setting popup items, max number = 5
            .GetPopupItem(1).Caption = "Settings"
            .GetPopupItem(2).Caption = "About"
            .GetPopupItem(3).Caption = "-"  && separator
            .GetPopupItem(4).Caption = "Close form"
            .GetPopupItem(5).Caption = "\Exit"  && item disabled
        ENDWITH
     
    PROCEDURE BeforePopupActivate
    PARAMETERS lResult
        lResult = ThisForm.chPopup.Value && .F. cancels popup activation
     
    PROCEDURE OnPopupItemSelected
    LPARAMETERS lnItem, lcCaption
        DO CASE
        CASE lnItem = 2
            = MessageB("System Tray Icon and Menu Control   ", 64, " About")
        CASE lnItem = 4
            ThisForm.Release
        CASE lnItem = 5
            IF MessageB("Exit FoxPro?   ", 32+4, " FoxTray Control") = 6
                QUIT
            ENDIF
        OTHER
            = MessageB("Popup item selected: " + LTRIM(STR(lnItem)) +;
                ", [" + lcCaption + "]")
        ENDCASE
    ENDDEFINE
     
    * * *
    *|The FoxPro code contains no Win32 functions, all calls moved 
    *|to the ActiveX part:
    *|
    *| LoadImage 
    *| ExtractAssociatedIcon 
    *| DestroyIcon 
    *| GetModuleFileName 
    *| Shell_NotifyIcon 
    *| CallWindowProc 
    *| GetWindowLong 
    *| SetWindowLong 
    *| CopyMemory 
    *| GetCursorPos 
    *| SetRectEmpty 
    *| CreatePopupMenu 
    *| TrackPopupMenuEx 
    *| InsertMenuItem 

    Tidak ada komentar:

    Posting Komentar