Hello


I have made a simple win32 program that displays a window that has a button & a text area.

When I click the button, my program grabs the text from the text area & displays the grabbed text in a MessageBox.

My Problem:
When my program grabs the text from the text area, the text comes out as jobberish or nothing at all?

I believe the problem occurs with the code to send a message to the text area window (known as hEdit) to grab the text in it & store it in a char array.

Here is the simple code snippets where the problems occur & below that is the full program:

Window Proceedure: Creation of the controls - button & text area

case WM_CREATE: {
             hEdit   = CreateWindowEx(  
                    WS_EX_CLIENTEDGE,  
                    "Edit",  
                    "edit box example",  
                    WS_BORDER | WS_CHILD | WS_VISIBLE,  
                    10, 10,  
                    200, 100,  
                    hwnd, (HMENU)IDM_TEXT,  
                    gInstance,  
                    NULL);  
                    
             button = CreateWindowEx(
                            
                       0,
                       "Button",
                       "Capture Text",
                       WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                       110,120,
                       100,20,
                       hwnd, 
                       (HMENU)IDM_BUTTON,  // this is the custom made message sent when button is pushed
                       gInstance,
                       NULL);
                         
             }
        break;

code to grab the text from text area: this is from the windows proceedure:

case WM_COMMAND:
             
             switch(LOWORD(wParam)) {
                    // Grab text from text area if button is pushed
                    case IDM_BUTTON: 
                    {
              
                         char buffer[300];
                         SendMessage(hEdit,WM_GETTEXT,sizeof(buffer),(LPARAM)buffer);
                         MessageBox(NULL,buffer,"Captured Text",MB_OK);
                    }
                    break;
                    default:
                    break;     
             }
        break;

Full Program:

#include <windows.h>
#include <stdio.h>

// Define custom messages
#define IDM_BUTTON 1
#define IDM_TEXT 2

const char g_szClassName[] = "myWindowClass";
static HINSTANCE gInstance;

// Functions List //
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);


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     = gInstance;
    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 registration of main class fails
    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, 400, 400,
        NULL, NULL, gInstance, 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;
}

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    HWND button, hEdit;
          
    switch(msg)
    {
        case WM_CREATE: {
             hEdit   = CreateWindowEx(  
                    WS_EX_CLIENTEDGE,  
                    "Edit",  
                    "edit box example",  
                    WS_BORDER | WS_CHILD | WS_VISIBLE,  
                    10, 10,  
                    200, 100,  
                    hwnd, (HMENU)IDM_TEXT,  
                    gInstance,  
                    NULL);  
                    
             button = CreateWindowEx(
                            
                       0,
                       "Button",
                       "Capture Text",
                       WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                       110,120,
                       100,20,
                       hwnd, 
                       (HMENU)IDM_BUTTON,  // this is the custom made message sent when button is pushed
                       gInstance,
                       NULL);
                         
             }
        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        case WM_COMMAND:
             
             switch(LOWORD(wParam)) {
                    // Grab text from text area if button is pushed
                    case IDM_BUTTON: 
                    {
                         HWND findWindow = GetWindow(hwnd,GW_CHILD);
                         char buffer[300];
                         SendMessage(findWindow,WM_GETTEXT,sizeof(buffer),(LPARAM)buffer);
                         MessageBox(NULL,buffer,"Captured Text",MB_OK);
                    }
                    break;
                    default:
                    break;     
             }
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

Edited 6 Years Ago by gretty: n/a

Your code runs without a problem for me, maybe it's a compiler setting?
Also, you can use the GetWindowText and SetWindowText to make life simpler.

I think it maybe my compiler also. I use dev c++, do you know how I can fix it? What I have to fix?

I did to your code as William suggested and changed your SendMessage(GET_TEXT) to GetWindowText() calls. Much easier.

Also, I removed your two global variables. The szClassName only needs to be defined in the proc where the WndClass is registered. Also, there are a half dozen ways to get hInstance anywhere in your code you need it. I used GetModuleHandle() in your WM_CREATE handler. Also, no need to define specific handles for the HWND button and edit box. They go out of scope right after the WM_CREATE handler anyway.

here is you modified code

#include <windows.h>
#include <stdio.h>

// Define custom messages
#define IDM_BUTTON 1
#define IDM_TEXT   2

// Functions List //
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    char szClassName[] = "myWindowClass";
    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 = szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    // if registration of main class fails
    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,
        szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 400,
        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;
}

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    HWND hCtl;
          
    switch(msg)
    {
        case WM_CREATE: {
             hCtl   = CreateWindowEx(  
                    WS_EX_CLIENTEDGE,  
                    "Edit",  
                    "edit box example",  
                    WS_BORDER | WS_CHILD | WS_VISIBLE,  
                    10, 10,  
                    200, 100,  
                    hwnd, (HMENU)IDM_TEXT,  
                    GetModuleHandle(0),  
                    NULL);  
                    
             hCtl = CreateWindowEx(
                            
                       0,
                       "Button",
                       "Capture Text",
                       WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                       110,120,
                       100,20,
                       hwnd, 
                       (HMENU)IDM_BUTTON,  // this is the custom made message sent when button is pushed
                       GetModuleHandle(0),
                       NULL);
                         
             }
        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        case WM_COMMAND:
             
             switch(LOWORD(wParam)) {
                    // Grab text from text area if button is pushed
                    case IDM_BUTTON: 
                    {
                         HWND findWindow = GetWindow(hwnd,GW_CHILD);
                         char buffer[300];
                         //SendMessage(findWindow,WM_GETTEXT,sizeof(buffer),(LPARAM)buffer);
                         GetWindowText(findWindow,buffer,300);
                         MessageBox(NULL,buffer,"Captured Text",MB_OK);
                    }
                    break;
                    default:
                    break;     
             }
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

I don't know what's going on that you can't compile it. You know, setting up a Windows GUI program to compile isn't the same as with old DOS or even console mode programs. You really, really, really, have to make sure a whole lot of compiler switches and lib references are right. If you are command line compiling it does take some knowledge of how things work. However, with any of the modern development suites such as CodeBlocks, Dev-C++, Visual Studio, etc., all you need to do is go through a few Wizard screens that define the PROJECT as a Win32 GUI project and the IDE will set it all up to compile correctly. However, if you are laboring under the mistaken notion that all you have to do is open up the IDE, open a blank code window, paste code into it and compile, then ITS NOT LIKELY GOING TO WORK! That's not what you are doing is it?

The reason I'm going on about this is that lately folks have been posting Win32 programs saying that they copied the code out of a tutorial somewhere, but the code won't compile, and why not? When folks here run the programs, they compile fine. I think that's what's going on.

Edited 6 Years Ago by Frederick2: add comment

This code runs fine using visual studio 2005 without modification here.
I dont have dev c++ here, will install it and try later.

[edit]Just tried it in dev c++ and still no problems!

Edited 6 Years Ago by stevee1984: Update

I tried your code in VS 2005 and VS 2010 with some changes for portability.. It works fine. I dont have any Dev c++ compilor .. Wioll try with that if got one..

Shine Joseph.

Edited 2 Years Ago by pritaeas: Removed fake signature.

Hi guys,

I have preety same code as the above, but I would like to take the text from the textarea and instead of showing it in a message box, show it in some area next to the textarea from where it was taken (something like chat: user inputs text and it's shown in a window above). Does anybody have any clue how to do that?

Thanks fot help in advance.

This article has been dead for over six months. Start a new discussion instead.