How to have a button that does something?

So, I have a text edit box (hEdit; HMENU is "IDC_MAINEDIT") and a button (button; HMENU is "IDC_MAINBUTTON"). All I am trying to do, is type something in hEdit, click a button, and have a messagebox come up saying what you typed. In WM_COMMAND, I have the switch statement with (LOWORD(wParam)) in it's parameters. In case IDC_MAINBUTTON, I have the messagebox. I can't seem to get the text out of WM_CREATE and into WM_COMMAND. My code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_CREATE:
        hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", L"",
                               WS_CHILD | WS_VISIBLE | ES_MULTILINE, 11, 22, 100, 100, hwnd,
 (HMENU)IDC_MAINEDIT, GetModuleHandle(NULL), NULL);
        button = CreateWindowEx(WS_EX_CLIENTEDGE, L"BUTTON", L"CLick Me.",
                                WS_CHILD | WS_VISIBLE | WS_BORDER, 120, 100, 100, 30, hwnd,
 (HMENU)IDC_MAINBUTTON, GetModuleHandle(NULL), NULL);
        if(hEdit == NULL){
            MessageBox(hwnd, L"Could not create edit box", L"Error", MB_OK | MB_ICONERROR);
        }
        GetDlgItemText(hEdit, IDC_MAINEDIT, thing, MAX_PATH);
        GetWindowText(hEdit, thing, MAX_PATH);
        break;
    case WM_COMMAND:
    {
        LPWSTR thing;
        switch(LOWORD(wParam)){
        case IDC_MAINBUTTON:

            MessageBox(NULL, thing, L"What you said:", MB_OK | MB_ICONINFORMATION);

            break;
        }
    }
        break;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}



*Note: I only put in my Window Procedure. I don't think anyone wants to surf through 200 lines of code. The IDC_MAINEDIT and IDC_MAINBUTTON are defined at the top, with IDC_MAINEDIT being 101 and IDC_MAINBUTTON 102. *
Last edited on
First, both GetDlgItemText and GetWindowText do the same thing so you only need to use one of them. If you decide to use GetDlgItemText the first paramater needs to be a handle to the parent of the control, which in this case the parent of the edit box is hwnd
The problem is with the variable thing. You have declared it as a pointer to a wide string, but in GetWindowText(hEdit, thing, MAX_PATH); you have specified MAX_PATH. What you need to do is declare the thing variable like this
 
WCHAR thing[MAX_PATH];

And the problem is also with the positioning of the thing variable declaration. If you want to use it in the WM_CREATE case, either declare it as a global variable or declare it after the WM_CREATE like this
1
2
case WM_CREATE:
WCHAR thing[MAX_PATH];

You dont need to declare the thing variable after the WM_COMMAND unless what you're trying to do is display text each time you type it in the edit box. In that case you need to use again GetWindowText(hEdit, thing, MAX_PATH); after the IDC_MAINBUTTON case
And if u dont create thing as a global variable, you need to create is after WM_COMMAND

Last edited on
If i have understood right this code might help you... Try it!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <windows.h>

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

char szClassName[ ] = "TEXT_STORING";
char TextMsg[20];
HWND TextBox;

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;             
    MSG messages;           
    WNDCLASSEX wincl; 

    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;           
    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) COLOR_BACKGROUND;

    if (!RegisterClassEx (&wincl))
        return 0;

    hwnd = CreateWindowEx (
           0,                   
           szClassName,         
           "Windows App",      
           WS_OVERLAPPEDWINDOW,
           CW_USEDEFAULT,      
           CW_USEDEFAULT,       
           544,               
           375,                
           HWND_DESKTOP,        
           NULL,               
           hThisInstance,     
           NULL                
           );

    ShowWindow (hwnd, nFunsterStil);

 
    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
    return messages.wParam;
}


LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  
    {
           case WM_CREATE:
                {
                          TextBox = CreateWindow(TEXT("EDIT"), TEXT(""),
                                    WS_VISIBLE | WS_CHILD | WS_BORDER,
                                    10,10,400,20,// you can use the dimensions you want
                                    hwnd, NULL, NULL, NULL
                          );
                          CreateWindow(TEXT("BUTTON"), TEXT("OK"),
                                      WS_VISIBLE | WS_CHILD | WS_BORDER,
                                      410,10,80,20,// you can use the dimensions you want
                                       hwnd, (HMENU) 1, NULL, NULL             
                          );
                          break;
                }
                
                case WM_COMMAND:
                {
                     switch(LOWORD(wParam))
                     {
                         case 1:
                              {
                                       int Getmsg = 0;
                                       char *t = &TextMsg[0];
                                       Getmsg = GetWindowText(TextBox, t, 20);
                                       
                                       MessageBox(hwnd, TextMsg, "TITLE", MB_OK);
                                          
                                       break;
                              }
                     }
                     break;
                }
        case WM_DESTROY:
            PostQuitMessage (0);     
            break;
        default:                     
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

I hope my advice was helpful!!! :)
Last edited on
Okay, I didn't copy your code into mine, Son, but what I did do is get rid of the GetWindowText() function I called (line 20), changed thing from a LPWSTR to a wchar_t, and only put it in WM_CREATE. Anyway, what happened was MessageBox, regardless of the text field's contents (even if empty) just displayed some random character, kinda looking like a pi symbol. I would post a picture of it, but I have no idea how to do that on this website :|
I think I am about to try Son's code, or change thing to a different datatype. Thanks for everything so far

EDIT: I changed thing to just a normal char, and now nothing is outputted to the MB

EDIT 2: @Son, thanks your code worked.
Last edited on
I' m happy I could help!!! :) :) :)
Topic archived. No new replies allowed.