Valhalla Legends Forums Archive | Battle.net Bot Development References | Minimize To Tray

AuthorMessageTime
Soul
I need the code for MiNiMiZe To Tray (IN VB) im newb at this lol It would be highly appreicated if i could get it from someone Thanks.


- Soul
January 18, 2003, 2:28 PM
Spht
First, create a module, and add the following:

[code]Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Long

Public Type NOTIFYICONDATA
   cbSize As Long
   hWnd As Long
   uID As Long
   uFlags As Long
   uCallbackMessage As Long
   hIcon As Long
   szTip As String * 64
End Type

Public Const PK_TRAYICON = &H401
Public Const NIF_ICON = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_TIP = &H4
Public Const NIM_ADD = &H0[/code]

For adding the icon, you can use this function I've created for you:

[code]Public Function AddTrayIcon&(hWnd&, uID&, hIcon&, Optional szTip$)
   Dim nid As NOTIFYICONDATA
   
   With nid
       .cbSize = Len(nid)
       .hWnd = hWnd
       .uID = uID
       .uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
       .uCallbackMessage = PK_TRAYICON
       .hIcon = hIcon
       If Not IsMissing(szTip) Then .szTip = szTip & vbNullChar
   End With
   AddTrayIcon = Shell_NotifyIcon(NIM_ADD, nid)
End Function[/code]

Usage: AddTrayIcon Me.hWnd, 1, Me.Icon, "Optional ToolTip"
That will add an icon to the system tray that has the focused form's icon.

If you want to remove the icon from the system tray, you can create a seperate function that uses NIM_DELETE (&H2) for dwMessage.
If you want to modify the icon, use NIM_MODIFY (&H1).

I hope this helps.
January 18, 2003, 3:07 PM
Soul
Woha, it adds icon to tray but it doesn't minimize to tray :S

-Soul
January 18, 2003, 3:18 PM
Spht
[quote]Woha, it adds icon to tray but it doesn't minimize to tray :S

-Soul[/quote]

Just do Me.Hide when you want to have it go to system tray. That way it will hide the form and it's appearance in the task bar so it will only be present in the system tray.
January 18, 2003, 3:24 PM
iago
put that in the OnMinimize or OnResize event or whatever it is..
January 18, 2003, 3:24 PM
Spht
Yes, you can do it that way if you're going to have it appear in system tray when user clicks Minimize.

[code]Private Sub Form_Resize()
   If Me.WindowState = vbMinimized Then Me.Hide
End Sub[/code]
January 18, 2003, 3:28 PM
Skywing
I'm sure many of you are familiar with programs annoyingly losing their system tray icons when Explorer is restarted.

Fortunately, there's a way for you to receive notifications of the shell restarting, so that you can recreate any tray icon(s) which you may have had up at the time... :

Call RegisterWindowMessage("TaskbarCreated") and store the return value - this is a message which the system will send to you when the shell restarts.  The value of this message is not constant, so you'll need to call RegisterWindowMessage when your program loads in order to receive the current value (it will not change after that point).  This message is sent to all top-level windows; when you receive it, you can assume that all system tray icons which you may have previously created are removed, and need to be re-added.

This feature is present with IE4 and later...
MSDN provides some sample code in C which demonstrates how to use this:
[code]LRESULT CALLBACK WndProc(HWND hWnd, UINT uMessage, WPARAM wParam,
   LPARAM lParam)
{
static UINT s_uTaskbarRestart;

switch(uMessage)
   {
   case WM_CREATE:
       s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated"));
       break;
       
   default:
       if(uMessage == s_uTaskbarRestart)
           AddTaskbarIcons();
       break;
   }
return DefWindowProc(hWnd, uMessage, wParam, lParam);
}[/code]
January 18, 2003, 4:06 PM
MesiaH
Thats pretty sweet, that always bugged the hell out of me lol
January 19, 2003, 12:03 AM
RhiNo
Or doing Hide.Me with it you could just use this

Add this to the form Command1_Click being your button or whatever it is you need to click in order for it to minimize.
[Code]

Private Sub Command1_Click()
Call AddToTray(Me.Icon, Me.Caption, Me)
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If RespondToTray(X) <> 0 Then Call ShowFormAgain(Me)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call RemoveFromTray
End Sub

[/code]

Then add this to your module

[Code]

     Type NOTIFYICONDATA
        cbSize As Long
        hwnd As Long
        uId As Long
        uFlags As Long
        uCallBackMessage As Long
        hIcon As Long
        szTip As String * 64
     End Type

     Global Const NIM_ADD = &H0
     Global Const NIM_MODIFY = &H1
     Global Const NIM_DELETE = &H2
     Global Const WM_MOUSEMOVE = &H200
     Global Const NIF_MESSAGE = &H1
     Global Const NIF_ICON = &H2
     Global Const NIF_TIP = &H4
     Global Const WM_LBUTTONDBLCLK = &H203   'Double-click
     Global Const WM_LBUTTONDOWN = &H201     'Button down
     Global Const WM_LBUTTONUP = &H202       'Button up

     'Right-click constants.
     Global Const WM_RBUTTONDBLCLK = &H206   'Double-click
     Global Const WM_RBUTTONDOWN = &H204     'Button down
     Global Const WM_RBUTTONUP = &H205       'Button up

     Declare Function Shell_NotifyIcon Lib "shell32" _
        Alias "Shell_NotifyIconA" _
        (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
     Global nid As NOTIFYICONDATA

Sub AddToTray(TrayIcon, TrayText As String, TrayForm As Form)
        'Set the individual values of the NOTIFYICONDATA data type.
        nid.cbSize = Len(nid)
        nid.hwnd = TrayForm.hwnd
        nid.uId = vbNull
        nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
        nid.uCallBackMessage = WM_MOUSEMOVE
        nid.hIcon = TrayIcon 'You can replace form1.icon with loadpicture=("icon's file name")
        nid.szTip = TrayText & vbNullChar

        'Call the Shell_NotifyIcon function to add the icon to the taskbar
        'status area.
        Shell_NotifyIcon NIM_ADD, nid
        TrayForm.Hide
End Sub

Sub ModifyTray(TrayIcon, TrayText As String, TrayForm As Form)
        nid.cbSize = Len(nid)
        nid.hwnd = TrayForm.hwnd
        nid.uId = vbNull
        nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
        nid.uCallBackMessage = WM_MOUSEMOVE
        nid.hIcon = TrayIcon
        nid.szTip = TrayText & vbNullChar
        Shell_NotifyIcon NIM_MODIFY, nid
End Sub

Sub RemoveFromTray()
Shell_NotifyIcon NIM_DELETE, nid
End Sub

Function RespondToTray(X As Single)
         RespondToTray = 0
         Dim msg As Long
         Dim sFilter As String
         If Form1.ScaleMode <> 3 Then msg = X / Screen.TwipsPerPixelX Else: msg = X
         Select Case msg
            Case WM_LBUTTONDOWN
            Case WM_LBUTTONUP
            Case WM_LBUTTONDBLCLK 'Left button double-clicked
            RespondToTray = 1
            Case WM_RBUTTONDOWN 'Right button pressed
            RespondToTray = 2
            Case WM_RBUTTONUP
            Case WM_RBUTTONDBLCLK
         End Select
End Function

     Sub ShowFormAgain(TrayForm As Form)
     Call RemoveFromTray
     TrayForm.Show
     End Sub

[/code]

^^That is alot of typing but its all good and it works pretty damn good in my oppinion
January 19, 2003, 2:12 PM
Soul
??? Complie Error: Abigous Name Detected: Form_Unload  ???
January 20, 2003, 1:47 AM
dRAgoN
you have more then 1 instance of form_unload
January 20, 2003, 2:10 AM
Zakath
People need to learn what "ambiguous" means. :-/
January 20, 2003, 2:12 AM
Blade_360
I totally agree
January 20, 2003, 12:52 PM
UserLoser
What about a system tray icon.ocx?  I have one and i use that...
me.hide
Sysicon.ShowNormalIcon
January 20, 2003, 1:31 PM
Soul
How do i get that to work? i have no idea how to add a OCX to it man.
January 22, 2003, 1:13 AM
MesiaH
an ocx is totally not needed for something so simple. shellnotifyicon api is not hard at all.
January 22, 2003, 1:59 AM
ILurker
Ctrl+t > browse > and select the file
doublic click on the icon on the toolbar on your main form

[code]
private sub mnuName_Click
me.hide
flshtray1.visible = true
[/code]

[code]
private sub flshtray1_click()
me.show
flshtray1.visible = false
[/code]
February 9, 2003, 4:06 PM

Search