Skip to content

Commit f318be0

Browse files
committed
use Unicode for window names
1 parent 662df32 commit f318be0

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

ShaderGlass/Helpers.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#define MAX_WINDOW_TITLE 200
4+
35
inline wchar_t* convertCharArrayToLPCWSTR(const char* charArray)
46
{
57
wchar_t* wString = new wchar_t[4096];
@@ -24,14 +26,11 @@ inline BOOL IsAltTabWindow(HWND hwnd)
2426
return hwndWalk == hwnd;
2527
}
2628

27-
inline std::string GetWindowStringText(HWND hwnd)
29+
inline std::wstring GetWindowStringText(HWND hwnd)
2830
{
29-
int len = GetWindowTextLength(hwnd) + 1;
30-
std::vector<wchar_t> buf(len);
31-
GetWindowText(hwnd, &buf[0], len);
32-
std::wstring wide = &buf[0];
33-
std::string s(wide.begin(), wide.end());
34-
return s;
31+
wchar_t title[MAX_WINDOW_TITLE];
32+
GetWindowText(hwnd, title, MAX_WINDOW_TITLE);
33+
return std::wstring(title);
3534
}
3635

3736
inline bool Is1903()

ShaderGlass/Options.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ struct OutputScaleInfo
6262

6363
struct CaptureWindow
6464
{
65-
CaptureWindow(HWND hwnd, const std::string& name) : hwnd {hwnd}, name {name} { }
66-
HWND hwnd;
67-
std::string name;
65+
CaptureWindow(HWND hwnd, const std::wstring& name) : hwnd {hwnd}, name {name} { }
66+
HWND hwnd;
67+
std::wstring name;
6868
};
6969

7070
struct CaptureDisplay

ShaderGlass/ShaderWindow.cpp

+20-13
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bool ShaderWindow::LoadProfile(const std::wstring& fileName)
2626

2727
std::string shaderCategory;
2828
std::string shaderName;
29-
std::optional<std::string> windowName;
29+
std::optional<std::wstring> windowName;
3030
std::optional<std::string> desktopName;
3131
std::optional<bool> transparent;
3232
std::optional<bool> clone;
@@ -43,8 +43,10 @@ bool ShaderWindow::LoadProfile(const std::wstring& fileName)
4343
return true;
4444
}
4545
else if(key == "CaptureWindow")
46-
{
47-
windowName = value;
46+
{
47+
wchar_t wideName[MAX_WINDOW_TITLE];
48+
MultiByteToWideChar(CP_UTF8, 0, value.c_str(), -1, wideName, MAX_WINDOW_TITLE);
49+
windowName = std::wstring(wideName);
4850
}
4951
else if(key == "CaptureDesktop")
5052
{
@@ -400,7 +402,12 @@ void ShaderWindow::SaveProfile(const std::wstring& fileName)
400402
outfile << "InputArea \"" << std::to_string(m_captureOptions.inputArea.left) << " " << std::to_string(m_captureOptions.inputArea.top) << " "
401403
<< std::to_string(m_captureOptions.inputArea.right) << " " << std::to_string(m_captureOptions.inputArea.bottom) << "\"" << std::endl;
402404
if(m_captureOptions.captureWindow)
403-
outfile << "CaptureWindow " << std::quoted(GetWindowStringText(m_captureOptions.captureWindow)) << std::endl;
405+
{
406+
auto windowTitle = GetWindowStringText(m_captureOptions.captureWindow);
407+
char utfName[MAX_WINDOW_TITLE];
408+
WideCharToMultiByte(CP_UTF8, 0, windowTitle.c_str(), -1, utfName, MAX_WINDOW_TITLE, NULL, NULL);
409+
outfile << "CaptureWindow " << std::quoted(utfName) << std::endl;
410+
}
404411
else if(m_captureOptions.monitor)
405412
{
406413
MONITORINFOEX info;
@@ -504,7 +511,7 @@ void ShaderWindow::ScanWindows()
504511
UINT i = 0;
505512
for(const auto& w : m_captureWindows)
506513
{
507-
AppendMenu(m_windowMenu, MF_STRING, WM_CAPTURE_WINDOW(i++), convertCharArrayToLPCWSTR(w.name.c_str()));
514+
AppendMenu(m_windowMenu, MF_STRING, WM_CAPTURE_WINDOW(i++), w.name.c_str());
508515
if(m_captureOptions.captureWindow == w.hwnd)
509516
CheckMenuItem(m_windowMenu, WM_CAPTURE_WINDOW(i - 1), MF_CHECKED | MF_BYCOMMAND);
510517
}
@@ -826,7 +833,7 @@ void ShaderWindow::UpdateTitle()
826833
const auto& aspectRatio = aspectRatios.at(WM_ASPECT_RATIO(m_selectedAspectRatio));
827834
const auto& shader = m_captureManager.Presets().at(m_captureOptions.presetNo);
828835

829-
char windowName[26];
836+
wchar_t windowName[26];
830837
windowName[0] = 0;
831838
if(m_captureOptions.captureWindow)
832839
{
@@ -839,22 +846,22 @@ void ShaderWindow::UpdateTitle()
839846
}
840847
if(captureTitle.size() > 20)
841848
{
842-
captureTitle = captureTitle.substr(0, 20) + "...";
849+
captureTitle = captureTitle.substr(0, 20) + _T("...");
843850
}
844-
captureTitle += ", ";
845-
strncpy_s(windowName, captureTitle.c_str(), 26);
851+
captureTitle += _T(", ");
852+
wcsncpy_s(windowName, captureTitle.c_str(), 26);
846853
}
847854
}
848855

849-
char title[200];
856+
wchar_t title[200];
850857
const char* scaleString = m_captureOptions.freeScale ? "free" : outputScale.mnemonic;
851858
const auto fps = (int)roundf(m_captureManager.FPS());
852-
snprintf(title, 200, "ShaderGlass (%s%s, %spx, %s%%, ~%s, %dfps)", windowName, shader->Name, pixelSize.mnemonic, scaleString, aspectRatio.mnemonic, fps);
853-
SetWindowTextA(m_mainWindow, title);
859+
_snwprintf_s(title, 200, _T("ShaderGlass (%s%S, %Spx, %S%%, ~%S, %dfps)"), windowName, shader->Name, pixelSize.mnemonic, scaleString, aspectRatio.mnemonic, fps);
860+
SetWindowTextW(m_mainWindow, title);
854861
}
855862
else
856863
{
857-
SetWindowTextA(m_mainWindow, "ShaderGlass (stopped)");
864+
SetWindowTextW(m_mainWindow, _T("ShaderGlass (stopped)"));
858865
}
859866
}
860867

0 commit comments

Comments
 (0)