簡體   English   中英

Win32 C ++中的GetWindowText()無法正常工作

[英]GetWindowText() in Win32 C++ is not working

以下代碼不起作用。我正在嘗試從編輯控件中獲取文本,但它不起作用。嘗試通過所有可能的代碼來查找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;
 }  

您已將text_box (和其他變量)定義為WndProc函數的本地變量,這意味着每次調用該函數來處理消息時,它們的值都會丟失。 如果要將它們的值從一條消息保留到下一條消息,則需要將它們移出函數范圍(或使其變為static )。

這是代碼的有效版本,用數字控件ID和GetDlgItem用法替換了本地變量text_box (不會保留從一個調用到下一個調用的信息),從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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM