Click to See Complete Forum and Search --> : beginner


aska_chage
April 24th, 2001, 03:19 AM
hello, i want to ask about UI

i want my VB program to minimize to the icon tray at the right bottom (like volume, clock, ICQ, MSN does)

how to do it? i really have no idea at all...

thanks in advance

regards

chris

Cimperiali
April 24th, 2001, 04:37 AM
I find this code at Planet-source-code (sorry, do not remember author or direct link). Have a look

1 standard bas module
1 form with just a menu on it to quit.
Option Explicit
'standard bas code
'----------------------------------------------------------------------
'WIN32 API Constants
'-----------------------------------------------------------------------
Public Const GW_CHILD = 5
Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
Public Const GWL_WNDPROC = (-4)
Public Const IDANI_OPEN = &H1
Public Const IDANI_CLOSE = &H2
Public Const IDANI_CAPTION = &H3
Public Const NIF_ICON = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_TIP = &H4
Public Const NIM_ADD = &H0
Public Const NIM_DELETE = &H2
Public Const NIM_MODIFY = &H1
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_LBUTTONUP = &H202
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_MOUSEMOVE = &H200
Public Const WM_RBUTTONDBLCLK = &H206
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Public Const WM_USER = &H400

'----------------------------------------------------------------------------
'Application Defined Constants
'----------------------------------------------------------------------------
Public Const OPT_COPY = 0
Public Const OPT_MOVE = 1
Public Const OPT_DELETE = 2
Public Const OPT_RENAME = 3
Public Const WM_CALLBACK_MSG = WM_USER Or &HF

'----------------------------------------------------------------------------
'WIN32 Type Declares
'----------------------------------------------------------------------------
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

Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

'-------------------------------------------------------------------------------------
'Application defined enumerations
'-------------------------------------------------------------------------------------
Public Enum ZoomTypes
ZOOM_FROM_TRAY
ZOOM_TO_TRAY
End Enum

'-------------------------------------------------------------------------------------
'WIN32 DLL Declares
'-------------------------------------------------------------------------------------
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, _
ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function DrawAnimatedRects Lib "user32" (ByVal hwnd As Long, ByVal idAni As Long, lprcFrom As RECT, _
lprcTo As RECT) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Declare Function Shell_NotifyIconA Lib "shell32.dll" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long

'-------------------------------------------------------------------------------------
'Public Variables
'-------------------------------------------------------------------------------------
Public lngPrevWndProc As Long 'Original WNDPROC address.
Public lngWndID As Long 'Our unique icon identifier.
Public lngHwnd As Long 'The hwnd of program.
Public nidTray As NOTIFYICONDATA 'Global Icon Info Structure.
Public strToolTip As String 'The string we use in the tip text fro the tray.
Public intOptionChoice As Integer 'Option button tracker.

Public rctFrom As RECT, rctTo As RECT
Public lngTrayHand As Long
Public lngStartMenuHand As Long, lngChildHand As Long
Public strClass As String * 255
Public lngClassNameLen As Long
Public lngRetVal As Long

Public Function WndProcMain(ByVal hwnd As Long, ByVal message As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
'-------------------------------------------------------------------------------------
'Window Proc for System Tray. Used for sub-classing.
'-------------------------------------------------------------------------------------
If lngWndID = wParam Then
Select Case lParam 'Button Responses
Case WM_LBUTTONDBLCLK
'Left Double-Click Procedure Here
Case WM_LBUTTONDOWN
'Left Mouse Down Procedure Here
Case WM_LBUTTONUP
'Left Mouse Up Procedure Here
ClearTray
RestoreWindow
Case WM_RBUTTONDBLCLK
'Right Double-Click Procedure Here
Case WM_RBUTTONDOWN
'Right Mouse Down Procedure Here
Case WM_RBUTTONUP
'Right Mouse Up Procudure Here
'Show our popup menu in the tray.
frmMain.PopupMenu frmMain.mnuPopup
End Select
End If

'Call the original WNDPROC (with the passed in values in tact) to handle any messages we ignored.
WndProcMain = CallWindowProc(lngPrevWndProc, hwnd, message, wParam, lParam)

End Function


Public Function ZoomForm(zoomToWhere As ZoomTypes, hwnd As Long) As Boolean
'-------------------------------------------------------------------------------------
'This function 'zooms' a window.
'-------------------------------------------------------------------------------------

'Select the type of zoom to do.
Select Case zoomToWhere

'Zoom the window into the tray.
Case ZOOM_FROM_TRAY
'Get the handle to the start menu.
lngStartMenuHand = FindWindow("Shell_TrayWnd", vbNullString)

'Get the handle to the first child window of the start menu.
lngChildHand = GetWindow(lngStartMenuHand, GW_CHILD)

'Loop through all siblings until we find the 'System Tray' (A.K.A. --> TrayNotifyWnd)
Do
lngClassNameLen = GetClassName(lngChildHand, strClass, Len(strClass))

'If it is the tray then store the handle.
If InStr(1, strClass, "TrayNotifyWnd") Then
lngTrayHand = lngChildHand
Exit Do
End If

'If we didn't find it, go to the next sibling.
lngChildHand = GetWindow(lngChildHand, GW_HWNDNEXT)

Loop

'Get the RECT of our form.
lngRetVal = GetWindowRect(hwnd, rctFrom)

'Get the RECT of the Tray.
lngRetVal = GetWindowRect(lngTrayHand, rctTo)

'Zoom from the tray to where our form is.
lngRetVal = DrawAnimatedRects(frmMain.hwnd, IDANI_CLOSE Or IDANI_CAPTION, rctTo, rctFrom)

Case ZOOM_TO_TRAY
'Get the handle to the start menu.
lngStartMenuHand = FindWindow("Shell_TrayWnd", vbNullString)
'Get the handle to the first child window of the start menu.
lngChildHand = GetWindow(lngStartMenuHand, GW_CHILD)
'Loop through all siblings until we find the 'System Tray' (A.K.A. --> TrayNotifyWnd)
Do
lngClassNameLen = GetClassName(lngChildHand, strClass, Len(strClass))

'If it is the tray then store the handle.
If InStr(1, strClass, "TrayNotifyWnd") Then
lngTrayHand = lngChildHand
Exit Do
End If

'If we didn't find it, go to the next sibling.
lngChildHand = GetWindow(lngChildHand, GW_HWNDNEXT)

Loop
'Get the RECT of our form.
lngRetVal = GetWindowRect(hwnd, rctFrom)

'Get the RECT of the Tray.
lngRetVal = GetWindowRect(lngTrayHand, rctTo)

'Zoom from where our form is to the tray .
lngRetVal = DrawAnimatedRects(frmMain.hwnd, IDANI_OPEN Or IDANI_CAPTION, rctFrom, rctTo)

End Select

End Function

Public Sub SendToTray()
lngHwnd = frmMain.hwnd 'Get the hwnd from of the form.
lngWndID = App.hInstance 'ID used for callback.
strToolTip = frmMain.Caption & Chr(0) 'Initialize the icon tip.
lngPrevWndProc = SetWindowLong(lngHwnd, GWL_WNDPROC, AddressOf WndProcMain) 'Subclass the window.

'Set Tray Icon Information
SetTrayInfo
'Zoom to Tray
ZoomForm ZOOM_TO_TRAY, lngHwnd
'Call windows to add the icon to the tray.
lngRetVal = Shell_NotifyIconA(NIM_ADD, nidTray)

'Hide our form.
frmMain.Hide
End Sub

Public Sub ClearTray()
'If the user clicked the tray icon then release the hook on the window.
'In other words un-subclass it by returning it's original WNDPROC address
'that we captured in the original call to SetWindowLong.
SetWindowLong lngHwnd, GWL_WNDPROC, lngPrevWndProc
'Delete our icon from the tray.
Shell_NotifyIconA NIM_DELETE, nidTray
End Sub

Public Sub RestoreWindow()
'Zoom to show form
ZoomForm ZOOM_FROM_TRAY, frmMain.hwnd
'frmMain.WindowState = vbNormal
frmMain.Show
frmMain.SetFocus
End Sub

Public Sub SetTrayInfo()
'Set all Tray Icon info.
nidTray.hwnd = lngHwnd
nidTray.cbSize = Len(nidTray)
nidTray.hIcon = frmMain.Icon
nidTray.szTip = strToolTip
nidTray.uCallbackMessage = WM_CALLBACK_MSG
nidTray.uID = lngWndID
nidTray.uFlags = NIF_MESSAGE Or NIF_TIP Or NIF_ICON
End Sub

'----form code
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'UnloadMode possibilities:
'0 The user has chosen the Close command from the Control-menu box on the form.
'1 The Unload method has been invoked from code.
'2 The current Windows-environment session is ending.
'3 The Microsoft Windows Task Manager is closing the application.
'4 An MDI child form is closing because the MDI form is closing.

If UnloadMode = 0 Then
SendToTray
Cancel = True
End If

Me.Hide
End Sub

Private Sub mnuExit_Click()
Unload Me
End
End Sub

Private Sub mnuShow_Click()
ClearTray
RestoreWindow
End Sub

Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.

shree
April 24th, 2001, 08:18 AM
There's a SysTray control that you can use. But if you are a beginner, it is no less intimidating than the API solution that Cimperiali presents because it contains no documentation.


- Copy the SysTray control sample from the Tools\Unsupprt directory of the Visual Basic 5.0 CD-ROM, or the Common\Tools\VB\Unsupprt directory of the Visual Basic 6.0 CD-ROM, to your hard drive.

- Open the project and compile the control. When complete, close the project.

What you get is a control with the properties InTray, TrayIcon, TrayTip, and the methods MouseDblClick, MouseDown, MouseMove and MouseUp.

- Create a new Standard EXE project.

- From the Project menu, click Components and check "System Tray Icon Control". Click OK.

Once you do this much, you'll have a much easier time with icons in the SysTray.

Cimperiali
April 24th, 2001, 08:23 AM
You're great.
:-)

Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.

midnightservice
September 19th, 2001, 09:09 PM
okay i got the systray icon now how do i go about adding it to the start up group in my install program..i want the icon there..when they want the program itself the click on it and it loads up.......any ideas...

by the way thanx to you both for the great info

midnightservice

John G Duffy
September 20th, 2001, 06:49 AM
Theer are a couple of ways to add a program to the Startup process.
A). if you are using Windows 98 or Windows ME (The only two I know about). During this process, hold the mouse button down until you get to the final resting place.
1) Using Explorer, locate the .exe file you wish to add to startup.
2) Left click and drag it to the "Start" button (Lower left corner). Hold it there until the start menu opens.
3) Drag it to "Programs". Hold it there until Program opens.
4) Find "Startup" menu item and drag it there. 5) When it opens, drag the icon to somewhere within this folder.
5) Then release the mouse button.
Your program will now start when Windows starts or restarts.
'
'
B). A slightly more complex way.
Click "Start"
click "Settings"
click "TaksBar and Start Menu"
Click "advanced" tab
Click "advanced" button
Open "Programs" in the treeview
Locate "Start" on the open folder
With "Start" visible, locate your .exe fiel and drag/Drop it to this folder

John G