Minimize Workbook to sys tray (64bit) -

misioo

New Member
Joined
Feb 23, 2015
Messages
1
Hello All,
I have a problem with adjust code to Win7 64bit. It's many examples for Win32.
I found this example:
Code:
Option Explicit

Declare PtrSafe Function SetForegroundWindow Lib "USER32" (ByVal hWnd As LongPtr) As LongPtr
Declare PtrSafe Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" ( _
  ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Declare PtrSafe Function CallWindowProc Lib "USER32" Alias "CallWindowProcA" (ByVal _
  lpPrevWndFunc As Long, ByVal hWnd As LongPtr, ByVal Msg As Long, ByVal wParam _
  As Long, ByVal lParam As Long) As LongPtr
Declare PtrSafe Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName _
  As String, ByVal lpWindowName As String) As Long
Declare PtrSafe Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst _
  As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Declare PtrSafe Function SetWindowLongPtr Lib "USER32" Alias    "SetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As LongPtr,    ByVal dwNewLong As LongPtr) As LongPtr


Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_LBUTTONDBL = &H203
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Public Const WM_ACTIVATEAPP = &H1C
Public Const NIF_ICON = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_TIP = &H4
Public Const NIM_ADD = &H0
Public Const NIM_DELETE = &H2
Public Const MAX_TOOLTIP As Integer = 128
Public Const GWL_WNDPROC = (-4)

Type NOTIFYICONDATA
 cbSize As Long
 hWnd As LongPtr
 uID As Long
 uFlags As Long
 uCallbackMessage As Long
 hIcon As Long
 szTip As String * MAX_TOOLTIP
End Type

Public nfIconData As NOTIFYICONDATA

Private FHandle As LongPtr
Private WndProc As LongPtr
Private Hooking As Boolean

Public Sub Hook(Lwnd As LongLong)
 If Hooking = False Then
  FHandle = Lwnd
  WndProc = SetWindowLongPtr(Lwnd, GWL_WNDPROC, AddressOf WindowProc)
  Hooking = True
 End If
End Sub

Public Sub Unhook()
 If Hooking = True Then
  SetWindowLongPtr FHandle, GWL_WNDPROC, WndProc
  Hooking = False
 End If
End Sub

Public Function WindowProc(ByVal hw As LongPtr, ByVal uMsg As LongPtr, ByVal wParam _
  As LongPtr, ByVal lParam As LongPtr) As LongPtr
 If Hooking Then
  If lParam = WM_LBUTTONDBL Then
   UserForm1.Show 1
   WindowProc = True
'   Unhook
   Exit Function
  End If
  WindowProc = CallWindowProc(WndProc, hw, uMsg, wParam, lParam)
 End If
End Function
 
Public Sub RemoveIconFromTray()
 Shell_NotifyIcon NIM_DELETE, nfIconData
End Sub

Public Sub AddIconToTray(MeHwnd As LongPtr, MeIcon As LongPtr, MeIconHandle As LongPtr, _
  Tip As String)
 With nfIconData
  .hWnd = MeHwnd
  .uID = MeIcon
  .uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
  .uCallbackMessage = WM_RBUTTONUP
  .hIcon = MeIconHandle
  .szTip = Tip & Chr$(0)
  .cbSize = Len(nfIconData)
 End With
 Shell_NotifyIcon NIM_ADD, nfIconData
End Sub

Function FindWindowd(ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
 FindWindowd = FindWindow(lpClassName, lpWindowName)
End Function

Function ExtractIcond(ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal _
  nIconIndex As Long) As LongPtr
 ExtractIcond = ExtractIcon(hInst, lpszExeFileName, nIconIndex)
End Function

Sub ShowUserForm()
 Application.Visible = False
 UserForm1.Show 1
End Sub
'******************************************************'
'****************** END MODULE CODE *******************'
'******************************************************'


but Excel crashes after line:
Code:
WndProc = SetWindowLongPtr(Lwnd, GWL_WNDPROC, AddressOf WindowProc)

Did you have the same problem?
Maybe you know how to fix it?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,215,053
Messages
6,122,882
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top