Hello guys,

I have a problem with Win32 C++ GUI. I'm creating a program and as one of the steps, I want to:
1. Let the user type somthing in the textbox,
2. Show it in another box/area,
3. Clear the typed text from the textbox.

It would be something like chat - in one window you type the message and it's shown in the second one.

So far, I have code with 2 windows (not much, I know) and I know that inside WM_COMMAND I should have some code that will take the inputed text and then will pass it to another window (where I want to show messages).

Here is my code:

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

#include <tchar.h>
#include <windows.h>

/*  Declare Windows procedure  */

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
HWND TextBox, SendButton, TextField;

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T("Messages"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           500,                 /* The programs width */
           370,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
        /* Translate virtual-key messages into character messages */
        /* Send message to WindowProcedure */

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;

/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    switch (message)                  /* handle the messages */
        case WM_CREATE:

            TextBox = CreateWindow("EDIT",
                                   WS_BORDER | WS_CHILD | WS_VISIBLE,
                                   10, 300, 390, 20,
                                   hwnd, (HMENU) 1, NULL, NULL);

            SendButton = CreateWindow("BUTTON",
                         WS_VISIBLE | WS_CHILD | WS_BORDER,
                         410, 300, 65, 20,
                         hwnd, (HMENU) 2, NULL, NULL);

            TextField = CreateWindow("EDIT",
                                     WS_VISIBLE | WS_CHILD | WS_BORDER | ES_READONLY,
                                     10, 90, 465, 200,
                                     hwnd, (HMENU) 3, NULL, NULL);

        case WM_COMMAND:
                case 1: // when button is clicked, this will happen:

                // what code should go here??


        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);

    return 0;

I will be very gratefull for helping me which functions I should use there or if you would be so helpful to add some code.

3 Years
Discussion Span
Last Post by triumphost

I don't understand what problem you are having doing this.. What have you tried :S

The solution is below:

//Get the text from box 1.
int len = GetWindowTextLength(TextBox) + 1;
char* text = new char[len];
GetWindowText(TextBox, &text[0], len);

//Append the text to box 2.
SendMessage(TextField, EM_SETSEL, -1, -1);
SendMessage(TextField, EM_REPLACESEL, 0, (LPARAM)&text[0]);
delete[] text;

//Delete the text from box 1.
SetWindowText(TextBox, "");

If you don't want to append the text then just use SetWindowText(TextField, text);

I've tested this before in my own chat program.

Edited by triumphost


triumphost, it works perfectly, thank you very much for help.

If it's possible, I have another question: What should I do if I'd like to put each new message in a new line?


To put a message on a new line, you must affix \r\n to the end of the previous string OR to the beginning or the new string and THEN append the result to your textbox.

Edited by triumphost

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.