77#include " ../../menu.h"
88#include " ../../tray_icon.h"
99#include " ../../tray_icon_event.h"
10+ #include " string_utils_windows.h"
1011
1112namespace nativeapi {
1213
@@ -80,7 +81,7 @@ class TrayIcon::Impl {
8081
8182 ~Impl () {
8283 if (hwnd_) {
83- Shell_NotifyIcon (NIM_DELETE, &nid_);
84+ Shell_NotifyIconW (NIM_DELETE, &nid_);
8485 }
8586 if (icon_handle_) {
8687 DestroyIcon (icon_handle_);
@@ -102,18 +103,20 @@ TrayIcon::TrayIcon() : id(-1), pimpl_(std::make_unique<Impl>()) {
102103 static UINT next_class_id = 1 ;
103104 std::string class_name =
104105 " NativeAPITrayIcon_" + std::to_string (next_class_id++);
106+ std::wstring wclass_name = StringToWString (class_name);
105107
106- WNDCLASS wc = {};
108+ WNDCLASSW wc = {};
107109 wc.lpfnWndProc = DefWindowProc;
108110 wc.hInstance = hInstance;
109- wc.lpszClassName = class_name .c_str ();
111+ wc.lpszClassName = wclass_name .c_str ();
110112
111- if (RegisterClass (&wc)) {
113+ if (RegisterClassW (&wc)) {
112114 // Create hidden message-only window
115+ std::wstring wtitle = StringToWString (" NativeAPI Tray Icon" );
113116 HWND hwnd =
114- CreateWindow (class_name .c_str (), " NativeAPI Tray Icon " , 0 , 0 , 0 , 0 , 0 ,
115- HWND_MESSAGE, // Message-only window
116- nullptr , hInstance, nullptr );
117+ CreateWindowW (wclass_name .c_str (), wtitle. c_str () , 0 , 0 , 0 , 0 , 0 ,
118+ HWND_MESSAGE, // Message-only window
119+ nullptr , hInstance, nullptr );
117120
118121 if (hwnd) {
119122 // Generate unique icon ID
@@ -148,12 +151,13 @@ void TrayIcon::SetIcon(std::string icon) {
148151 hIcon = LoadIcon (nullptr , IDI_APPLICATION);
149152 } else if (!icon.empty ()) {
150153 // Try to load as file path first
151- hIcon = (HICON)LoadImage (nullptr , icon.c_str (), IMAGE_ICON, 16 , 16 ,
152- LR_LOADFROMFILE);
154+ std::wstring wicon = StringToWString (icon);
155+ hIcon = (HICON)LoadImageW (nullptr , wicon.c_str (), IMAGE_ICON, 16 , 16 ,
156+ LR_LOADFROMFILE);
153157
154158 // If file path failed, try as resource
155159 if (!hIcon) {
156- hIcon = LoadIcon (GetModuleHandle (nullptr ), icon .c_str ());
160+ hIcon = LoadIconW (GetModuleHandle (nullptr ), wicon .c_str ());
157161 }
158162
159163 // If still failed, use default application icon
@@ -176,7 +180,7 @@ void TrayIcon::SetIcon(std::string icon) {
176180
177181 // Update the icon if it's currently visible
178182 if (IsVisible ()) {
179- Shell_NotifyIcon (NIM_MODIFY, &pimpl_->nid_ );
183+ Shell_NotifyIconW (NIM_MODIFY, &pimpl_->nid_ );
180184 }
181185 }
182186}
@@ -193,21 +197,21 @@ std::optional<std::string> TrayIcon::GetTitle() {
193197
194198void TrayIcon::SetTooltip (std::optional<std::string> tooltip) {
195199 if (pimpl_->hwnd_ ) {
196- const char * tooltip_str = tooltip.has_value () ? tooltip-> c_str () : " " ;
197- strncpy_s (pimpl_-> nid_ . szTip , tooltip_str,
198- sizeof (pimpl_->nid_ .szTip ) - 1 );
199- pimpl_-> nid_ . szTip [ sizeof (pimpl_-> nid_ . szTip ) - 1 ] = ' \0 ' ;
200+ std::string tooltip_str = tooltip.has_value () ? * tooltip : " " ;
201+ std::wstring wtooltip = StringToWString ( tooltip_str);
202+ wcsncpy_s (pimpl_->nid_ .szTip , _countof (pimpl_-> nid_ . szTip ),
203+ wtooltip. c_str (), _TRUNCATE) ;
200204
201205 // Update if icon is visible (check if hIcon is set as indicator)
202206 if (pimpl_->nid_ .hIcon ) {
203- Shell_NotifyIcon (NIM_MODIFY, &pimpl_->nid_ );
207+ Shell_NotifyIconW (NIM_MODIFY, &pimpl_->nid_ );
204208 }
205209 }
206210}
207211
208212std::optional<std::string> TrayIcon::GetTooltip () {
209- if (pimpl_->hwnd_ && pimpl_->nid_ .szTip [0 ] != ' \0 ' ) {
210- return std::string (pimpl_->nid_ .szTip );
213+ if (pimpl_->hwnd_ && pimpl_->nid_ .szTip [0 ] != L ' \0 ' ) {
214+ return WCharArrayToString (pimpl_->nid_ .szTip );
211215 }
212216 return std::nullopt ;
213217}
@@ -251,10 +255,10 @@ bool TrayIcon::SetVisible(bool visible) {
251255
252256 if (visible && !currently_visible) {
253257 // Show the tray icon
254- return Shell_NotifyIcon (NIM_ADD, &pimpl_->nid_ ) == TRUE ;
258+ return Shell_NotifyIconW (NIM_ADD, &pimpl_->nid_ ) == TRUE ;
255259 } else if (!visible && currently_visible) {
256260 // Hide the tray icon
257- return Shell_NotifyIcon (NIM_DELETE, &pimpl_->nid_ ) == TRUE ;
261+ return Shell_NotifyIconW (NIM_DELETE, &pimpl_->nid_ ) == TRUE ;
258262 } else {
259263 // Already in the desired state
260264 return true ;
0 commit comments