Results 1 to 9 of 9

Thread: VB6 - Sample Tray Activation

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    VB6 - Sample Tray Activation

    Attached is a sample program that uses a Tray Icon to activate a program. It uses dilettante's "NotifyIcon" program.

    http://www.vbforums.com/showthread.p...ght=notifyicon

    I have left his explanations in the User Control intact. When first activated, the "Tray" program starts as a Icon in the system tray surrounded by red. A balloon will appear stating "Connecting to Server". It will normally time out, but I am using a timer to simulate establishing a connection. This causes the balloon to disappear and the red background on the Icon to also disappear. Moving the mouse over the Icon will show "Connected to Server". Ten seconds later, a second timer is used to simulate an incoming message, which will flash with instructions.

    Clicking on the Tray Icon will activate a program called "Sample.exe". You will have to compile that program first before it can be activated.

    To return the "Tray" program to it's normal state, or to exit the program, right click on the Tray Icon.

    J.A. Coutts

    Updated 12/06/2017
    Attached Files Attached Files
    Last edited by couttsj; Dec 6th, 2017 at 02:37 PM.

  2. #2
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    673

    Re: VB6 - Sample Tray Activation

    thanks.good example

    load sample.exe,read the message. Close the program , at the same time start the time2 control again,may need Inter-process communication
    Last edited by xxdoc123; Nov 22nd, 2017 at 10:38 PM.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Re: VB6 - Sample Tray Activation

    This sample program has been modified to change the window that is being subclassed from the UserControl to the main form. The reason for doing this is because only the top level form receives "WM_POWERBROADCAST" messages from the operating system. To test this function, run the Tray Icon program and then let the system go to sleep. When you wake up the system, you should see 2 or 3 Debug messages (4, 7, & 18) depending on how the system was resumed. According to Microsoft, you have about 2 seconds to perform a function after receiving a "PBT_APMSUSPEND" (&H4) message.

    Also, the flashing balloon was changed to a steady balloon because of the way that Windows 10 now handles "Toasts".

    J.A. Coutts

  4. #4
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    673

    Re: VB6 - Sample Tray Activation

    your code have a bug

    UserControl_Initialize

    return hwnd=0

    fix

    Code:
    Public Sub Show()
      
       ' MsgBox UserControl.Parent.hwnd '& "----" & UserControl.Container.hwnd
        Debug.Print FrmHandle 
        
        SaveSetting "S Msg", "Settings", "H_Msg", CStr(FrmHandle)
        SetProp FrmHandle, "12345", UserControl.hWnd
        SubclassMe FrmHandle, Me
    now can return hwnd right.
    Last edited by xxdoc123; Dec 6th, 2017 at 10:16 PM.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Re: VB6 - Sample Tray Activation

    Quote Originally Posted by xxdoc123 View Post
    your code have a bug

    UserControl_Initialize

    return hwnd=0
    Not a bug. That code was experimental and unused. Initialize should be:
    Code:
    Private Sub UserControl_Initialize()
        nid.cbSize = LenB(nid)
    End Sub
    
    Public Sub Show()
        SubclassMe FrmHandle, Me
        With nid
            .hWnd = FrmHandle
            .uId = FrmHandle
            .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE Or NIF_INFO
            .hIcon = mpicIcon.Handle
            .uCallBackMessage = WM_APP_NIF
            .szTip = mstrToolTip & vbNullChar
        End With
        Shell_NotifyIcon NIM_ADD, VarPtr(nid)
        mblnShown = True
    End Sub
    Unlike the UserControl handle, the form handle is not available at the time the UserControl is initialized.

    J.A. Coutts
    Last edited by couttsj; Dec 6th, 2017 at 11:32 PM.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Re: VB6 - Sample Tray Activation

    Attached is a simplified version of the Tray Activation program. It does not use a User Control or subclassing, and is based on PCNotifyIcon by Richard L. McCutchen. Instead it uses a single Class module. The User interface with the Tray Icon is achieved through the only visible part; the icon image. As far as I know, the only thing directly usable is the MouseMove event, and from that he was able to determine Left and Right Button Down. The original code used the Double Click events, but I was unable to make those work on Win 8.1, and I really didn't need them. I then added the Context Menu and the Balloon routines. Usage is pretty much the same as the original sample. So far I have tested it on Win Vista, Win 8.1, & Win 10.

    J.A. Coutts
    Attached Files Attached Files

  7. #7
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: VB6 - Sample Tray Activation

    Thanks for sharing.
    It is working fine in XP
    However the Sample project gives run time error 13, on this line -
    IcoHwnd = GetSetting("S Msg", "Settings", "H_Msg")
    This also happens when I run Sample project directly (not being called from the Tray application)
    I have never used the Registry, so the solution is not jumping off the page at me.

    Thanks,
    Rob

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Re: VB6 - Sample Tray Activation

    Quote Originally Posted by Bobbles View Post
    Thanks for sharing.
    It is working fine in XP
    However the Sample project gives run time error 13, on this line -
    IcoHwnd = GetSetting("S Msg", "Settings", "H_Msg")
    This also happens when I run Sample project directly (not being called from the Tray application)
    I have never used the Registry, so the solution is not jumping off the page at me.

    Thanks,
    Rob
    Any reference to IcoHwnd can be commented out. It was used to test a different program.

    J.A. Coutts

  9. #9
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: VB6 - Sample Tray Activation

    Thanks JA,
    appears to have fixed the problem

    Rob

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width