C++ WinAPI:HWND 到字符串返回十六进制

C++ WinAPI: HWND To String Returning Hex

我正在使用 WinAPI,我正在尝试制作一个允许您更改标题的程序。

#if defined(UNICODE) && !defined(_UNICODE)

#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>

#include <string>
#include <sstream>

using namespace std;

string HWNDToString(HWND inputA);
void setTitle(string inputA);

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
    HWND hwnd;
    MSG messages;
    WNDCLASSEX wincl;

    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof (WNDCLASSEX);
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(255,128,0));

    if (!RegisterClassEx (&wincl)) return 0;

    hwnd = CreateWindowEx
    (
        0,
        szClassName,
        _T("Title Changer"),
        WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        400 + 22,
        400 + 49,
        HWND_DESKTOP,
        NULL,
        hThisInstance,
        NULL
    );

    ShowWindow (hwnd, nCmdShow);
    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
    return messages.wParam;
}

HWND textout, titlebutton , powerbutton, textin;
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
            textout = CreateWindow("STATIC", "Enter new window title here:", WS_VISIBLE | WS_CHILD | WS_BORDER, 0, 0, 230, 20, hwnd, NULL, NULL, NULL);
            textin = CreateWindow("EDIT", "New Title", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 0, 20, 250, 25, hwnd, (HMENU) NULL, NULL, NULL);
            titlebutton = CreateWindow("BUTTON", "Set as New Window Title", WS_VISIBLE | WS_CHILD | WS_BORDER, 0, 45, 210, 25, hwnd, (HMENU) /*=*/1/*=*/, NULL, NULL);
            powerbutton = CreateWindow("BUTTON", "Power Off", WS_VISIBLE | WS_CHILD | WS_BORDER, 316, 0, 100, 25, hwnd, (HMENU) 2, NULL, NULL);
        break;

        case WM_COMMAND:
            if (LOWORD(wParam) == 1)
            {
                SetWindowText(hwnd, HWNDToString(textin).c_str());
                MessageBox(hwnd, string("Title changed to: " + HWNDToString(textin)).c_str(), "Title Changed", MB_OK | MB_ICONINFORMATION);
            }

            if (LOWORD(wParam) == 2)
            {
                PostQuitMessage(0);
            }
        break;

        case WM_DESTROY:
            PostQuitMessage(0);
        break;

        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
        break;
    }

    return 0;
}

string HWNDToString(HWND inputA)
{
    stringstream stringstreamBuffer;
    stringstreamBuffer << inputA;
    return stringstreamBuffer.str();
}

但程序将标题设置为随机 hex-like 字符串(例如,0x123abc)。

我定义的HWNDToString函数有什么问题?我需要使用 sprintf 将十六进制转换为字符串吗?

OS: Windows 7 终极版 x64
IDE:代码块
编译器:GNU GCC 编译器 (MinGW32)

HWND 是一个指针(struct HWND__*void*,分别取决于 STRICT 是启用还是禁用)。将这样的指针传递给基于 std::ostream 的 class 的 operator<< 将调用 operator<<(const void*),它将 pointed-to 内存地址格式化为十六进制字符串。

由于您尝试使用 EDIT 控件从用户那里接受一个字符串值,然后使用该字符串的值设置主 window 的标题,因此您应该使用GetWindowTextLength() and GetWindowText() 函数改为:

string HWNDToString(HWND inputA)
{
    string s;
    int len = GetWindowTextLength(inputA);
    if (len > 0)
    {
        s.resize(len + 1);
        len = GetWindowText(inputA, &s[0], s.size());
        s.resize(len);
    }
    return s;
}

case WM_COMMAND:
    if (LOWORD(wParam) == 1)
    {
        string s = HWNDToString(textin);
        SetWindowText(hwnd, s.c_str());
        MessageBox(hwnd, string("Title changed to: " + s).c_str(), "Title Changed", MB_OK | MB_ICONINFORMATION);
    }
    ...

附带说明一下,您的 "Power Off" 按钮应该向主 window 发送一条 WM_CLOSE 消息,而不是直接调用 PostQuitMessage()

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        ...
        case WM_COMMAND:
            ....
            if (LOWORD(wParam) == 2)
            {
                SendMessage(hwnd, WM_CLOSE, 0, 0);
            }
            break;

        // By default, DefWindowProc() handles WM_CLOSE by destroying the window
        // using DestroyWindow(). WM_CLOSE is also received when the user closes
        // the window manually. This allows you to close down your app correctly
        // regardless of how the window is closed.  You can handle WM_CLOSE
        // manually if you want to prompt the user before allowing the
        // window to be destroyed...
        /*
        case WM_CLOSE:
            if (MessageBox(hwnd, "Are you sure you want to power down?", "Power Down?", MB_YESNO) == IDYES)
                DestroyWindow(hwnd);
            break;
        */

        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hwnd, message, wParam, lParam);
    }

    return 0;
}

有关详细信息,请参阅 Destroying a Window 上的 MSDN 文档。

对于那些偶然发现这个问题的人,这里有一个非常简单的函数可以做到这一点:

#include <string>
#include <windows.h>

string HWNDToString(HWND input)
{
    string output = "";
    size_t sizeTBuffer = GetWindowTextLength(input) + 1;

    if(sizeTBuffer > 0)
    {
        output.resize(sizeTBuffer);
        sizeTBuffer = GetWindowText(input, &output[0], sizeTBuffer);
        output.resize(sizeTBuffer);
    }

    return output;
}