1 Attachment(s)
Mutex...Really nice solution
Quote:
Originally posted by hspc
anyway this is my solution for this prolem :
[....]
How does it work ?
it creates a mutex (shared object that hace a uniqe name)..
if you try to recreate it you get the error :
ERROR_ALREADY_EXISTS
this is the whole idea...
I like your solution.
Another way (not this good, however)
is to have a check in sub main for a particular form
(FindWindow api will find it even if it is not visible)
you load and do not show. If you find it, program is
already running, if not, it is first instance
ie attached code
But your solution is better...
:)
Re: When If App.PrevInstance doesn't detect a previous instance
this works for me but only shows a message:
If App.PrevInstance Then
MsgBox "Application is already running"
n i need to load the previnstance, here's the code for getting the program to the taskbar:
Private Sub Form_Resize()
If Me.WindowState = 1 Then
'The user has minimized his window
Call Shell_NotifyIcon(NIM_ADD, IconData)
' Add the form's icon to the tray
Me.Hide
' Hide the button at the taskbar
End If
End Sub
Private Sub Form_Load()
With IconData
.cbSize = Len(IconData)
' The length of the NOTIFYICONDATA type
.hIcon = Me.Icon
' A reference to the form's icon
.hwnd = Me.hwnd
' hWnd of the form
.szTip = "Cyber-Net Lite" & Chr(0)
' Tooltip string delimited with a null character
.uCallbackMessage = WM_MOUSEMOVE
' The icon we're placing will send messages to the MouseMove event
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
' It will have message handling and a tooltip
.uID = vbNull
' uID is not used by VB, so it's set to a Null value
End With
End Sub
Private Sub mnuExit_Click()
Unload Me
' Unload the form
End
' Just to be sure the program has ended
End Sub
Private Sub mnuShow_Click()
Me.WindowState = vbNormal
Shell_NotifyIcon NIM_DELETE, IconData
Me.Show
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim msg As Long
msg = X
' The message is passed to the X value
' You must set your form's ScaleMode property to pixels in order to get the correct message
If msg = WM_LBUTTONDBLCLK Then
' The user has double-clicked your icon
Call mnuShow_Click
' Show the window
ElseIf msg = WM_RBUTTONDOWN Then
' Right-click
PopupMenu mnuPopup
' Popup the menu
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Shell_NotifyIcon NIM_DELETE, IconData
Cancel = -1
End Sub
Can anybody please help me?
thnx