Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ WiNAP:HWND到字符串返回十六进制_C++_String_Winapi_Converters_Hwnd - Fatal编程技术网

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

C++ WiNAP:HWND到字符串返回十六进制,c++,string,winapi,converters,hwnd,C++,String,Winapi,Converters,Hwnd,我正在使用WinAPI,我正在尝试制作一个允许您更改标题的程序 #if defined(UNICODE) && !defined(_UNICODE) #define _UNICODE #elif defined(_UNICODE) && !defined(UNICODE) #define UNICODE #endif #include <tchar.h> #include <windows.h> #include <string&

我正在使用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();
}
但是程序将标题设置为一个随机的十六进制字符串,例如0x123abc

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

操作系统:Windows 7 Ultimate x64 IDE:代码块
编译器:GNU GCC编译器MinGW32

HWND是指针结构HWND_u*或void*,具体取决于是启用还是禁用。将这样一个指针传递给操作符对于那些偶然发现这个问题的人,下面是一个非常简单的函数:

#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;
}

HWNDToString将窗口句柄作为字符串返回给您。你希望它做什么?如果您想要窗口的标题,您需要使用GetWindowText来读取它。@JonathanPotter我正在尝试获取textin的内容并将其转换为字符串。如果我要在其他地方使用HWND到字符串转换,我希望能够在不必读取窗口标题的情况下进行转换。我现在正在使用DestroyWindowhwnd来关闭电源按钮,所有东西都会立即关闭,谢谢。@FluorescentGreen5如果我在其他地方使用HWND到字符串转换,我希望能够做到这一点,而不必阅读窗口标题。您发布的原始代码确实将HWND转换为其指针值的字符串表示形式。但很明显,您的意思是编辑控件的内容,这就是上面答案中的GetWindowText返回的内容。@FluorescentGreen5在编辑控件上调用GetWindowText将检索它的内容,而不是它没有的标题。只有在具有标题的顶级窗口上调用GetWindowText时,才会检索标题。GetWindowText文档中明确说明了这一点:将指定窗口标题栏的文本(如果有)复制到缓冲区中。如果指定的窗口是控件,则会复制该控件的文本。我想我应该更清楚地说明我自己。我希望代码将窗口标题设置为我在textin文本框中指定的内容,并在消息框中显示textin的内容,而不必依赖于获取窗口标题的内容。要解决我的问题,我需要做的就是弄清楚如何获取从HWNDToString返回的十六进制字符串所指向的数据。答案已经给出给了您,让您完全按照自己的意愿去做。
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);
    }
    ...
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;
}
#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;
}