@@ -39,6 +39,7 @@ THE SOFTWARE.
3939#include " dokan/cryptdokan.h"
4040#include " util/LockZeroBuffer.h"
4141#include " util/util.h"
42+ #include " ../libcommonutil/commonutil.h"
4243#include " dokan/MountPointManager.h"
4344#include " ui/uiutil.h"
4445#include " ui/CryptSettings.h"
@@ -48,6 +49,9 @@ THE SOFTWARE.
4849
4950// CryptPropertySheet
5051
52+ static const int kSysTrayiTimerId = 1 ;
53+ static const int kSysTrayTimerInterval = 2000 ; // milliseconds
54+
5155IMPLEMENT_DYNAMIC (CCryptPropertySheet, CPropertySheet)
5256
5357CCryptPropertySheet::CCryptPropertySheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
@@ -106,6 +110,7 @@ BOOL CCryptPropertySheet::CanClose()
106110 }
107111}
108112
113+ UINT CCryptPropertySheet::WM_TASKBARCREATED = ::RegisterWindowMessage(_T(" TaskbarCreated" ));
109114
110115BEGIN_MESSAGE_MAP (CCryptPropertySheet, CPropertySheet)
111116 ON_WM_NCCREATE()
@@ -119,12 +124,22 @@ BEGIN_MESSAGE_MAP(CCryptPropertySheet, CPropertySheet)
119124// ON_WM_QUERYENDSESSION()
120125ON_WM_ENDSESSION()
121126ON_WM_POWERBROADCAST()
127+ // custom messages
128+ ON_REGISTERED_MESSAGE(WM_TASKBARCREATED, OnTaskbarCreated)
129+ ON_WM_TIMER()
130+ ON_WM_DESTROY()
122131END_MESSAGE_MAP()
123132
124133
125134// CryptPropertySheet message handlers
126135
127136
137+ LRESULT CCryptPropertySheet::OnTaskbarCreated(WPARAM wParam, LPARAM lParam)
138+ {
139+ theApp.RecreateSystemTrayIcon ();
140+ return 0 ;
141+ }
142+
128143BOOL CCryptPropertySheet::OnInitDialog ()
129144{
130145
@@ -144,6 +159,14 @@ BOOL CCryptPropertySheet::OnInitDialog()
144159 if (pWnd)
145160 pWnd->ShowWindow (SW_HIDE);
146161
162+ if (theApp.IsRunningAsAdministrator ()) {
163+
164+ // We want to know if explorer gets restarted so we can re-create our system tray icon.
165+ // However, if we're running as administrator, we never get any TaskbarCreated messages,
166+ // so we need to poll using a timer.
167+
168+ SetTimer (kSysTrayiTimerId , kSysTrayTimerInterval , nullptr );
169+ }
147170
148171 return bResult;
149172}
@@ -401,3 +424,43 @@ BOOL CCryptPropertySheet::PreTranslateMessage(MSG* pMsg)
401424
402425 return CPropertySheet::PreTranslateMessage (pMsg);
403426}
427+
428+
429+ void CCryptPropertySheet::OnTimer (UINT_PTR nIDEvent)
430+ {
431+ // TODO: Add your message handler code here and/or call default
432+
433+ if (nIDEvent == kSysTrayiTimerId ) {
434+ const HWND hWnd = ::FindWindow (L" Shell_TrayWnd" , NULL );
435+
436+ if (hWnd)
437+ {
438+ if (m_bDoRecreateSysTrayIcon)
439+ {
440+ m_bDoRecreateSysTrayIcon = false ;
441+ theApp.RecreateSystemTrayIcon ();
442+ }
443+ else if (hWnd != m_hSysTrayWnd)
444+ {
445+ m_hSysTrayWnd = hWnd;
446+ // We seem to need some sort of delay before recreating it.
447+ m_bDoRecreateSysTrayIcon = true ;
448+ }
449+ }
450+ }
451+
452+ CPropertySheet::OnTimer (nIDEvent);
453+ }
454+
455+
456+ void CCryptPropertySheet::OnDestroy ()
457+ {
458+ CPropertySheet::OnDestroy ();
459+
460+ // TODO: Add your message handler code here
461+
462+ if (theApp.IsRunningAsAdministrator ())
463+ {
464+ KillTimer (kSysTrayiTimerId );
465+ }
466+ }
0 commit comments