- HWND -> std::wstring
wstring hwnd2ws(HWND hWnd)
{
wchar_t wszHWndView[64] = {};
::wnsprintfW(wszHWndView, _countof(wszHWndView), L"%X", hWnd);
return wszHWndView;
}
- std::wstring -> HWND
HWND ws2hwnd(const wstring& wstr)
{
__int64 i64tmp = __int64(0);
std::wstring wstrWnd = wstr;
if (wstrWnd[0] != L'0' && (wstrWnd[0] != L'x' || wstrWnd[0] != L'X'))
wstrWnd = L"0x" + wstrWnd;
::StrToInt64ExW(wstrWnd.c_str(), STIF_SUPPORT_HEX, &i64tmp);
return (HWND)i64tmp;
}
wstring hwnd2ws(HWND hWnd)
{
wchar_t wszHWndView[64] = {};
::wnsprintfW(wszHWndView, _countof(wszHWndView), L"%X", hWnd);
return wszHWndView;
}HWND ws2hwnd(const wstring& wstr)
{
__int64 i64tmp = __int64(0);
std::wstring wstrWnd = wstr;
if (wstrWnd[0] != L'0' && (wstrWnd[0] != L'x' || wstrWnd[0] != L'X'))
wstrWnd = L"0x" + wstrWnd;
::StrToInt64ExW(wstrWnd.c_str(), STIF_SUPPORT_HEX, &i64tmp);
return (HWND)i64tmp;
}

本文介绍了如何在C++中实现窗口句柄(HWND)与宽字符串(wstring)之间的相互转换。通过具体代码示例,展示了从HWND到wstring及从wstring回转为HWND的过程,为跨类型数据交互提供了实用解决方案。
429

被折叠的 条评论
为什么被折叠?



