Click to See Complete Forum and Search --> : Start a program


ands_au
April 25th, 2001, 02:14 PM
How will you automatically start a program in hidden form when the PC is booted and this application icon is available on the bottom right hand side of the task bar?Thanks.

John G Duffy
April 25th, 2001, 02:48 PM
To start a program when Windows is booted requires you install a shortcut to your program in the Windows Start folder. This process varies from Windows release to release. To get your program to minimize to the system tray instead of the task bar, integrate the following code into your program. This sample program requires two command buttons and a Menu structure created by the Menu Editor. The command buttons are named cmdExit and cmdHideInTray.
The menu structure is:
File mnuFile Visible = False
... Restore mnuRestore
... Exit mnuExit


'Please see pertinent notes about the windows messaging system below (NOTE 2).

private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (byval dwMessage as Long, lpData as NOTIFYICONDATA) as Long
private 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

' Values for uFlags
private Const NIF_ICON = &H2
private Const NIF_MESSAGE = &H1
private Const NIF_TIP = &H4

' Values for dwMessage
private Const NIM_MODIFY = &H1
private Const NIM_ADD = &H0
private Const NIM_DELETE = &H2

' Value for uCallbackMessage
private Const WM_LBUTTONDOWN = &H201

private Sub Form_MouseDown(Button as Integer, Shift as Integer, X as Single, Y as Single)
If Button = vbLeftButton then
'Be aware that when you want the user to click on something on your form with the
' same coordinates as you receive from the icon, it isn't possible to find out
' which one is clicked.
'You also need to be aware of when you change the ScaleMode property of your form,
' the coordinates received from the icon will also be influenced. When you set the
' ScaleMode property to 3 - Pixel, the values will be:
' 7710 / Screen.TwipsPerPixelX,
' 7755 / Screen.TwipsPerPixelX and
' 7800 / Screen.TwipsPerPixelY.

'You could also just divide by 15, instead of screen.twipsper.. etc. - same difference.

' If scalemode = Twip(1) 7710 = Left Button
' If scalemode = Twip(1) 7755 = Right Button
' If scalemode = Twip(1) 7800 = Middle Button
'Or,
If X = 7710 / 15 then 'X = 7710 / 15 is used for scalemode Pixel(3)
MsgBox "Left Button"
ElseIf X = 7755 / 15 then
PopupMenu mnuFile
ElseIf X = 7800 / 15 then
MsgBox "Middle Button"
End If
End If
End Sub

private Sub cmdExit_Click() 'Form's 'Exit' button.
Call RemoveFromTray
Unload me
End Sub

private Sub cmdHideInTray_Click() 'Form's 'Hide' button.
me.Visible = false
HideInTray
End Sub

private Sub mnuExit_Click() 'Menu's 'Exit' item.
Call RemoveFromTray
Unload me
End Sub

private Sub mnuRestore_Click() 'Menu's 'Restore' item.
Call RemoveFromTray
me.Show
End Sub

public Sub RemoveFromTray() 'Procedure to remove icon from tray.
Dim nid as NOTIFYICONDATA
With nid
.cbSize = len(nid)
.hwnd = me.hwnd
End With
Shell_NotifyIcon NIM_DELETE, nid
End Sub

public Sub HideInTray() 'Procedure to add icon to tray.
Dim nid as NOTIFYICONDATA 'The structure

With nid
.cbSize = len(nid) 'Size of the structure.
.hwnd = me.hwnd 'The form is owner / parent.
.szTip = "Simple System Tray Example" & Chr$(0) 'Tool tip when cursor hovers over icon in tray.
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP 'The flags.
.uCallbackMessage = WM_LBUTTONDOWN 'See NOTE 1 below...
.hIcon = me.Icon 'The owner form's icon will be shown in the tray.
End With

'Now, We call the Shell_NotifyIcon function, and
' specify we want to add an icon and that the
' information can be found in the nid-structure
' we just filled in.
Shell_NotifyIcon NIM_ADD, nid

'NOTE 1:

'We want the icon to send a (fake) left mousedown message when the user moves
'over or clicks the icon with whatever button he uses.

'NOTE 2:

' Windows works with messages. (Almost) everything the user does is translated into a message
' by Windows and send to the open applications. So if the user moves the mouse, a WM_MOUSEMOVE
' message is sent, if the user presses the left mousebutton a WM_LBUTTONDOWN message is sent,
' and if the current window needs to be repainted, a WM_PAINT message is sent. The runtime DLL's
' that must be shipped with every VB program convert these messages, and call the VB subs where
' we can write our code in. So if the user presses the left mousebutton in our program a
' WM_LBUTTONDOWN message is sent to our program. The runtime DLL's receive this message and
' translate it into the Form_MouseDown(vbLeftButton, x, y, ...) sub.
' The messages have two (optional) parameters: WM_xxxxx(wParam, lParam). These two parameters
' contain information about the message. In case of the WM_LBUTTONDOWN message, the wParam
' contains information about the other mousebuttons and CTRL, ALT and SHIFT keys (pressed/not pressed)
' and the lParam contains the x and y coordinate of mousecursor. This is the information you need
' to know about the Windows message system for our tray example.

'Now let's see what our program does. When the user does something with the icon in the tray
' (move over, click, etc.) we let Windows generate a fake WM_LBUTTONDOWN. In C++ you could specify
' a self-created message, but because in VB the runtime DLL's translate the messages they would
' just ignore it. We have to use an existing message. The icon will sent the specified message
' with information in the two parameters. Because this information is arranged differently from
' the 'real' WM_LBUTTONDOWN function, the Form_MouseDown function will receive the information,
' but in a weird order. The Button parameter will contain the vbLeftButton constant, because a
' fake Left buttondown message is send. So we only need to act when: Button = vbLeftButton. next,
' the x parameter contains the following information: did the user press a button? which button?
' did he double-click? This information will be neatly arranged in C++, but it's all stored in
' the x parameter in VB. (don't confuse the vbLeftButton with the real button the user presses!!
' The vbLeftButton constant must always be set when the user messes with the icon in the tray,
' and next we need to figure out what the user did!)

'The x parameter will specify the following values:

' - 7710
' The user pressed the left button on the icon.
' - 7755
' The user pressed the right button on the icon.
' - 7800
' The user pressed the middle button on the icon.

'So each time we receive a left mousedown at 'coordinates' (7755,0) we know that the user pressed
' the RIGHT button on the icon, and just the same for the other coodinates. I know that this
' looks kinda long-winded, but as long as it isn't possible to use other messages we need to do
' it this way.

End Sub




John G