Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 4th, 2005, 10:44 PM   #1
sham
Newbie
 
Join Date: Mar 2005
Posts: 29
Rep Power: 0 sham is on a distinguished road
how to Minimize application to taskbar ?

I have created software on Library database but when I minimize the program I want to display that as a icon in the system tray not on taskbar. Could you give me some idea?
sham is offline   Reply With Quote
Old Apr 5th, 2005, 2:18 PM   #2
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
Right...
1. Start a new standardEXE project
2. Add a picture box called picTrayClass and set Visible to false
3. Add a button called cmdHide and set Caption to "Hide to System Tray"
4. Put this code in:
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
Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_RBUTTONUP = &H205
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Dim TrayIcon As NOTIFYICONDATA
Const IDANI_OPEN = &H1
Const IDANI_CLOSE = &H2
Const IDANI_CAPTION = &H3
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Private Declare Function DrawAnimatedRects Lib "User32" (ByVal hwnd As Long, ByVal idAni As Long, lprcFrom As RECT, lprcTo As RECT) As Long
Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As Long, ByRef lpRect As RECT) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private rMe As RECT
Private rTray As RECT
Private Sub DoTrayHide()
    'animates tray icon minimize
    GetWindowRect Me.hwnd, rMe
    'gets the rect of this window
    GetWindowRect FindWindowEx(FindWindow("Shell_TrayWnd", vbNullString), FindWindowEx(FindWindow("Shell_TrayWnd", vbNullString), 0, "Button", vbNullString), "TrayNotifyWnd", vbNullString), rTray
    'gets the rect of the system tray
    Me.Hide
    DrawAnimatedRects Me.hwnd, IDANI_CLOSE Or IDANI_CAPTION, rMe, rTray
End Sub
Private Sub DoTrayShow()
    'animates tray icon restore
    GetWindowRect FindWindowEx(FindWindow("Shell_TrayWnd", vbNullString), FindWindowEx(FindWindow("Shell_TrayWnd", vbNullString), 0, "Button", vbNullString), "TrayNotifyWnd", vbNullString), rTray
    'gets the rect of the system tray
    DrawAnimatedRects Me.hwnd, IDANI_CLOSE Or IDANI_CAPTION, rTray, rMe
    Me.Show
End Sub
Private Sub cmdHide_Click()
    DoTrayHide
End Sub
Private Sub Form_Load()
    With TrayIcon
        .cbSize = Len(TrayI)
        .hwnd = picTrayClass.hwnd
        .uId = 1&
        .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
        .ucallbackMessage = WM_LBUTTONDOWN
        .hIcon = Me.Icon
        .szTip = "My Application Icon" & Chr$(0)
    End With
    Shell_NotifyIcon NIM_ADD, TrayIcon
End Sub
Private Sub Form_Unload(Cancel As Integer)
    With TrayIcon
        .cbSize = Len(TrayIcon)
        .hwnd = picTrayClass.hwnd
        .uId = 1&
        Shell_NotifyIcon NIM_DELETE, TrayIcon
    End With
End Sub
Private Sub picTrayClass_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Msg = X / Screen.TwipsPerPixelX
    If Msg = WM_LBUTTONDBLCLK Then
        'Left button double click
        If Not Me.Visible Then DoTrayShow
    ElseIf Msg = WM_RBUTTONUP Then
        'Right button click
        MsgBox "Right button"
    End If
End Sub
Click the button to hide, double click icon to restore.
This code is probably unecessarily complicated, and you don't actually need the animation but I thought it looked cool...
Rory is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC