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
@@ -102,16 +103,18 @@ 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
106108 WNDCLASS wc = {};
107109 wc.lpfnWndProc = DefWindowProc;
108110 wc.hInstance = hInstance;
109- wc.lpszClassName = class_name .c_str ();
111+ wc.lpszClassName = wclass_name .c_str ();
110112
111113 if (RegisterClass (&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 ,
117+ CreateWindow (wclass_name .c_str (), wtitle. c_str () , 0 , 0 , 0 , 0 , 0 ,
115118 HWND_MESSAGE, // Message-only window
116119 nullptr , hInstance, nullptr );
117120
@@ -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 ,
154+ std::wstring wicon = StringToWString (icon);
155+ hIcon = (HICON)LoadImage (nullptr , wicon.c_str (), IMAGE_ICON, 16 , 16 ,
152156 LR_LOADFROMFILE);
153157
154158 // If file path failed, try as resource
155159 if (!hIcon) {
156- hIcon = LoadIcon (GetModuleHandle (nullptr ), icon .c_str ());
160+ hIcon = LoadIcon (GetModuleHandle (nullptr ), wicon .c_str ());
157161 }
158162
159163 // If still failed, use default application icon
@@ -193,10 +197,10 @@ 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 , sizeof (pimpl_->nid_ .szTip ) / sizeof (WCHAR),
203+ wtooltip. c_str (), _TRUNCATE) ;
200204
201205 // Update if icon is visible (check if hIcon is set as indicator)
202206 if (pimpl_->nid_ .hIcon ) {
@@ -206,8 +210,8 @@ void TrayIcon::SetTooltip(std::optional<std::string> tooltip) {
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}
0 commit comments