Win32 C++ 中的 GetWindowText() 不起作用

Posted

技术标签:

【中文标题】Win32 C++ 中的 GetWindowText() 不起作用【英文标题】:GetWindowText() in Win32 C++ is not working 【发布时间】:2015-03-15 22:09:12 【问题描述】:

以下代码不起作用。我正在尝试从编辑控件中获取文本,但它不起作用。我厌倦了尝试所有可能的代码,在 msdn 等上查找文档...

case WM_COMMAND:
    switch(LOWORD(wParam))
    case 1:
        ::MessageBox(hwnd,"button clicked","message",MB_OK);
        break;
    case 2: 
        TCHAR t[20]; // 
            GetWindowText(text_box,t,19);// this is not working????????????????
        ::MessageBox(hwnd,t,t,MB_OK);
        cout<<t;



        break;

以下是完整的代码:

#include <windows.h>
#include<iostream>
using namespace std;

const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)

    HWND text_field, button , text_box;
    char text_saved[20];

    switch(msg)

       case WM_CREATE:
         text_field = CreateWindow("STATIC",
            "Hello World",
            WS_VISIBLE | WS_CHILD | WS_BORDER,
            20,20, 90,25,
            hwnd,
            NULL,NULL,NULL);
        button = CreateWindow("BUTTON",
            "push button",
            WS_VISIBLE | WS_CHILD | WS_BORDER,
            20,50, 100,20,
            hwnd,
            (HMENU)1,NULL,NULL  
            )  ;
        text_box = CreateWindow("EDIT",
            " ",
            WS_VISIBLE | WS_CHILD | WS_BORDER,
            20,80, 200,25,
            hwnd,
            NULL,NULL,NULL
            );
        CreateWindow("BUTTON",
            "Save",
            WS_VISIBLE | WS_CHILD | WS_BORDER,
            20,120, 100,20,
            hwnd,
            (HMENU)2,NULL,NULL  
            );





    break;
    case WM_COMMAND:
        switch(LOWORD(wParam))
        case 1:
            ::MessageBox(hwnd,"button clicked","message",MB_OK);
            break;
        case 2: 
            TCHAR t[20];
                GetWindowText(text_box,t,19);
            ::MessageBox(hwnd,t,t,MB_OK);
            cout<<t;



            break;



    
    break;

        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    
    return 0;


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)

    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500,500,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    
    return Msg.wParam;
   

【问题讨论】:

@Jonathan Potter 我尝试将变量设为全局但没有用:(它仍然给出空白的 msgbox 【参考方案1】:

您已将text_box(和其他变量)定义为WndProc 函数的本地变量,这意味着每次调用该函数来处理消息时它们的值都会丢失。如果您想将它们的值从一条消息保留到另一条消息,则需要将它们移到函数范围之外(或将它们设为 static)。

【讨论】:

由于这是 Windows 编程,更实用的解决方案是使用不同的方式来识别控件,例如通过数字控件 ID。 OP 可能会发现值得检查一下,例如GetDlgItem。是的,它可以用于任何窗口,而不仅仅是对话框。 @Cheersandhth.-Alf 在他们的编程生涯中,OP 了解变量的生命周期和范围可能更重要:) @jonathan 我试着让它再次成为全球没有运气:(。顺便说一句谢谢你的帮助【参考方案2】:

这是您的代码的工作重制版本,将局部变量 text_box(不保留从一次调用到下一次调用的信息)替换为数字控制 id 并使用 GetDlgItem,从 ANSI 更改为Unicode 文本,并修复了一些其他内容,同时主要保持格式:

#undef UNICODE
#define UNICODE
#include <windows.h>

#include<iostream>
#include <string>
using namespace std;

namespace g 
    const auto class_name = L"myWindowClass";

    const auto push_button_id   = 1;
    const auto save_button_id   = 2;
    const auto edit_field_id    = 3;

    const auto h_instance = ::GetModuleHandle( nullptr );
  // namespace g


// Step 4: the Window Procedure

void create_controls( const HWND hwnd )

    CreateWindow( L"STATIC",
                            L"Hello World",
                            WS_VISIBLE | WS_CHILD | WS_BORDER,
                            20,20, 90,25,
                            hwnd,
                            nullptr, g::h_instance, nullptr );
    CreateWindow( L"BUTTON",
                            L"push button",
                            WS_VISIBLE | WS_CHILD | WS_BORDER,
                            20,50, 100,20,
                            hwnd,
                            (HMENU) g::push_button_id, g::h_instance, nullptr
                            )  ;
    CreateWindow( L"EDIT",
                            L"",
                            WS_VISIBLE | WS_CHILD | WS_BORDER,
                            20,80, 200,25,
                            hwnd,
                            (HMENU) g::edit_field_id, g::h_instance, nullptr
                            );
    CreateWindow( L"BUTTON",
                            L"Save",
                            WS_VISIBLE | WS_CHILD | WS_BORDER,
                            20,120, 100,20,
                            hwnd,
                            (HMENU) g::save_button_id, g::h_instance, nullptr
                            );


LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )

    switch(msg)
    
    case WM_CREATE:
        create_controls( hwnd );
        break;
    case WM_COMMAND:
        switch(LOWORD(wParam)) 
        case 1:
            ::MessageBox( hwnd, L"button clicked", L"message", MB_SETFOREGROUND );
            break;
        case 2:
            const HWND text_box = GetDlgItem( hwnd, g::edit_field_id );
            const int n = GetWindowTextLength( text_box );
            wstring text( n + 1, L'#' );
            if( n > 0 )
            
                GetWindowText( text_box, &text[0], text.length() );
            
            text.resize( n );
            ::MessageBox(hwnd, text.c_str(), L"The text:", MB_SETFOREGROUND );
            break;
        
        break;
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    
    return 0;


int main()

    //Step 1: Registering the Window Class
    WNDCLASSEX wc =  sizeof( WNDCLASSEX ) ;

    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = g::h_instance;
    wc.hIcon         = LoadIcon(nullptr, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(nullptr, IDC_ARROW);
    wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW+1);
    wc.lpszMenuName  = nullptr;
    wc.lpszClassName = g::class_name;
    wc.hIconSm       = LoadIcon(nullptr, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    
        MessageBox(nullptr, L"Window Registration Failed!", L"Error!",
                   MB_ICONEXCLAMATION | MB_SETFOREGROUND );
        return E_FAIL;
    

    // Step 2: Creating the Window
    const HWND hwnd = CreateWindowEx(
               WS_EX_CLIENTEDGE,
               g::class_name,
               L"The title of my window",
               WS_OVERLAPPEDWINDOW,
               CW_USEDEFAULT, CW_USEDEFAULT, 500,500,
               nullptr, nullptr, g::h_instance, nullptr);

    if(hwnd == nullptr)
    
        MessageBox(nullptr, L"Window Creation Failed!", L"Error!",
                   MB_ICONEXCLAMATION | MB_SETFOREGROUND);
        return E_FAIL;
    

    ShowWindow(hwnd, SW_SHOWDEFAULT);   // Note: any other value is replaced.
    UpdateWindow(hwnd);                 // Not strictly necessary.

    // Step 3: The Message Loop
    MSG Msg;
    int get_message_result;
    while( (get_message_result = GetMessage(&Msg, nullptr, 0, 0)) > 0 )
    
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    
    return (get_message_result < 0? E_FAIL : Msg.wParam);

【讨论】:

感谢漂亮的代码,但是当我使用 gcc 运行它时,它会抛出一堆错误 可能这些是警告,而不是错误,因为我只用 g++ 编译了这段代码。由于您的评论,我现在检查它是否也可以使用 Visual C++ 编译。这两个编译器都不会在此处产生任何错误或警告,/W4 用于 Visual C++,-pedantic-errors 用于 g++。 嘿,这行得通 :) 一百万感谢您提供正确的解决方案。我用 dev c++ 编译器尝试了这个,它工作得非常完美。但是当我真的使用这样的命令行编译时:code g++ -o test.exe test.cpp code 它给出了错误你能告诉为什么吗? @llnspectable 也许你是对的,那么你应该在这里提供一个适合你的代码。他至少花时间阅读了我的完整代码并从头开始重写。我也愿意看到您的代码也将其作为样式指南发布在这里 @IInspectable:OP 是“试图从编辑控件中获取文本”。这个答案说它是“用数控ID替换局部变量”。 OP 将其理解为解决方案的原因可能是earlier discussion,但我认为几乎与您的所有其他声明一样,关于不理解答案的声明也是无稽之谈。我仍然在澄清这一点。不修复 OP 代码的 id 或其他问题(没有结束)。

以上是关于Win32 C++ 中的 GetWindowText() 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Win32 (C++) 中的选项卡损坏

删除 C++ Win32 中的标准窗口框架

Win32 C++ 中的 GetWindowText() 不起作用

将 C++ Win32 控制台项目类集成到 Visual Studio 2008 中的 Visual C++(Windows 窗体应用程序)项目中

无法从 Win32 C++ GDI+ 中的函数返回图形对象

win32 C++项目中的输入超时