wilton
January 3rd, 2000, 10:16 PM
How do I place an icon in the task tray when my program is minimized?
|
Click to See Complete Forum and Search --> : Taskbar and Tray wilton January 3rd, 2000, 10:16 PM How do I place an icon in the task tray when my program is minimized? FStocker January 4th, 2000, 12:15 AM place this into a module: 'user defined type required by Shell_NotifyIcon API call 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 'constants required by Shell_NotifyIcon API call: Public Const NIM_ADD = &H0 Public Const NIM_MODIFY = &H1 Public Const NIM_DELETE = &H2 Public Const NIF_MESSAGE = &H1 Public Const NIF_ICON = &H2 Public Const NIF_TIP = &H4 Public Const WM_MOUSEMOVE = &H200 Public Const WM_LBUTTONDOWN = &H201 'Button down Public Const WM_LBUTTONUP = &H202 'Button up Public Const WM_LBUTTONDBLCLK = &H203 'Double-click Public Const WM_RBUTTONDOWN = &H204 'Button down Public Const WM_RBUTTONUP = &H205 'Button up Public Const WM_RBUTTONDBLCLK = &H206 'Double-click Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" _ (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean Public nid As NOTIFYICONDATA place this into Form_load(): 'ShowTray-Icon! Me.Show Me.Refresh With nid .cbSize = Len(nid) .hwnd = Me.hwnd .uId = vbNull .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE .uCallBackMessage = WM_MOUSEMOVE .hIcon = Me.Icon .szTip = "Stundenabrechnungsprogramm" & vbNullChar End With Shell_NotifyIcon NIM_ADD, nid and the 3rd step: Private Sub Form_Resize() 'this is necessary to assure that the minimized window is hidden End Sub If Me.WindowState = vbMinimized Then Me.Hide End Sub wilton January 4th, 2000, 02:51 AM The only question I have now is how do I capture the event of the user clicking on my status area icon? Do you know of any good documentation on such things? Chris Eastwood January 4th, 2000, 03:47 AM That code *almost* works how you want it - here's a refined version that'll do what you want: In a BAS module, place the following code : option Explicit 'user defined type required by Shell_NotifyIcon API call 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 'constants required by Shell_NotifyIcon API call: public Const NIM_ADD = &H0 public Const NIM_MODIFY = &H1 public Const NIM_DELETE = &H2 public Const NIF_MESSAGE = &H1 public Const NIF_ICON = &H2 public Const NIF_TIP = &H4 public Const WM_MOUSEMOVE = &H200 public Const WM_LBUTTONDOWN = &H201 'Button down public Const WM_LBUTTONUP = &H202 'Button up public Const WM_LBUTTONDBLCLK = &H203 'Double-click public Const WM_RBUTTONDOWN = &H204 'Button down public Const WM_RBUTTONUP = &H205 'Button up public Const WM_RBUTTONDBLCLK = &H206 'Double-click public Declare Function SetForegroundWindow Lib "user32" (byval hwnd as Long) as Long public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" _ (byval dwMessage as Long, pnid as NOTIFYICONDATA) as Boolean public nid as NOTIFYICONDATA Now take a form (Form1) and place a PictureBox on it (Picture1). This picturebox will handle all the events from the task bar. Set the Picture1.visible property to false from the ide. Now paste in the following code : option Explicit private Sub Form_Load() ' ' Create our system tray icon from the Form's icon ' SetupSysTrayIcon End Sub private Sub Form_Resize() ' ' If minimized, then hide the form ' If me.WindowState = vbMinimized then me.Visible = false End Sub private Sub Form_Unload(Cancel as Integer) ' ' Remember to kill the systray icon, otherwise it hangs around ' till we move a mouse pointer over it ' KillSysTrayIcon End Sub private Sub SetupSysTrayIcon() on error GoTo vbErrorHandler ' ' Setup the System Tray Icon ' Dim tTrayStuff as NOTIFYICONDATA With tTrayStuff .cbSize = len(tTrayStuff) .hwnd = Picture1.hwnd .uId = 1& .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE .uCallBackMessage = WM_MOUSEMOVE .hIcon = me.Icon .szTip = "Your Tooltip Goes Here" & vbNullChar Shell_NotifyIcon NIM_ADD, tTrayStuff End With Exit Sub vbErrorHandler: MsgBox Err.Number & " " & Err.Description & " " & _ Err.Source & "::Form1_SetupSysTrayIcon", , App.ProductName End Sub private Sub KillSysTrayIcon() Dim t as NOTIFYICONDATA ' ' Kill the icon in the system tray ' With t .cbSize = len(t) .hwnd = Picture1.hwnd .uId = 1& End With Shell_NotifyIcon NIM_DELETE, t End Sub private Sub Picture1_MouseMove(Button as Integer, Shift as Integer, x as Single, y as Single) ' ' Here's where we handle the Icon Tray Messages ' Dim lMsg as Long ' ' Because of possible recursion - use a static flag ' static bInHere as Boolean on error GoTo vbErrorHandler ' ' The actual message is stored in the 'x' value - the correct ' way to get the messages is to subclass, but this hack works ' in VB5/6 so it'll do for now. ' lMsg = x / Screen.TwipsPerPixelX If bInHere then Exit Sub bInHere = true Select Case lMsg Case WM_LBUTTONDBLCLK: ' ' on Mouse DoubleClick - Restore the window ' on error resume next me.Visible = true If me.WindowState = vbMinimized then me.WindowState = vbDefault End If me.ZOrder me.Visible = true End Select bInHere = false Exit Sub vbErrorHandler: MsgBox Err.Number & " " & Err.Description & " " & Err.Source & _ " test::picture1_MouseMove", , App.ProductName End Sub - That's it. Chris Eastwood CodeGuru - the website for developers http://codeguru.developer.com/vb wilton January 4th, 2000, 03:55 AM I do I use subclassing if that's actually the correct way of doing this? How do I create a right click menu? I understand how to implement it but not how to create it. Thank you to those that have helped me. BrewGuru99 January 4th, 2000, 01:29 PM Making a popup menu is easy. Use the menu builder and create the menu that you would like to popup (if you already have a working menu that you would like to use, then that'll work). Then on the mouse down, click, whatever events just use me.popupmenu Peice of cake! :) Brewguru99 FStocker January 5th, 2000, 12:15 AM forgot yesterday (sorry): private Sub Form_MouseMove(Button as Integer, Shift as Integer, X as Single, Y as Single) 'callbacks from the System Tray icon. Dim Result as Long Dim msg as Long 'the value of X will vary depending upon the scalemode setting If me.ScaleMode = vbPixels then msg = X else msg = X / Screen.TwipsPerPixelX End If Select Case msg Case WM_LBUTTONUP '514 restore form window me.WindowState = vbNormal Result = SetForegroundWindow(me.hwnd) me.Show Case WM_LBUTTONDBLCLK '515 restore form window me.WindowState = vbNormal Result = SetForegroundWindow(me.hwnd) me.Show Case WM_RBUTTONUP '517 display popup Menu Result = SetForegroundWindow(me.hwnd) PopupMenu mnuFile End Select End Sub codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |