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
First, create a module, and add the following:
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
For adding the icon, you can use this function I've created for you:
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
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.
Woha, it adds icon to tray but it doesn't minimize to tray :S
-Soul
QuoteWoha, it adds icon to tray but it doesn't minimize to tray :S
-Soul
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.
put that in the OnMinimize or OnResize event or whatever it is..
Yes, you can do it that way if you're going to have it appear in system tray when user clicks Minimize.
Private Sub Form_Resize()
If Me.WindowState = vbMinimized Then Me.Hide
End Sub
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:
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);
}
Thats pretty sweet, that always bugged the hell out of me lol
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.
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
Then add this to your module
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
^^That is alot of typing but its all good and it works pretty damn good in my oppinion
??? Complie Error: Abigous Name Detected: Form_Unload ???
you have more then 1 instance of form_unload
People need to learn what "ambiguous" means. :-/
I totally agree
What about a system tray icon.ocx? I have one and i use that...
me.hide
Sysicon.ShowNormalIcon
How do i get that to work? i have no idea how to add a OCX to it man.
an ocx is totally not needed for something so simple. shellnotifyicon api is not hard at all.
Ctrl+t > browse > and select the file
doublic click on the icon on the toolbar on your main form
private sub mnuName_Click
me.hide
flshtray1.visible = true
private sub flshtray1_click()
me.show
flshtray1.visible = false