从文本框中获取文本

时间:2015-03-29 00:43:15

标签: c++ button textbox

我一直在环顾四周,尝试各种各样的事情,但我似乎无法让它发挥作用。

我想从文本框中获取文本,并在按下其中一个按钮时将其显示在弹出窗口中。有人可以让我知道如何从文本框中获取文本吗?

#include <windows.h>

#define BUTTON_1    101
#define BUTTON_2    102

const char g_szClassName[] = "myWindowClass";

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
            {
                HWND textBox1 = CreateWindow(TEXT("edit"), TEXT(""),     WS_VISIBLE | WS_CHILD | WS_BORDER, 20, 20, 140, 20, hWnd, (HMENU) 1, NULL,     NULL);
                HWND button1 = CreateWindowEx(NULL, "BUTTON", "Button   1",WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 180, 18, 100, 24, hWnd, (HMENU)BUTTON_1, NULL, NULL);
                HWND button2 = CreateWindowEx(NULL, "BUTTON", "Button 2",WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 180, 48, 100, 24, hWnd, (HMENU)BUTTON_2, NULL, NULL);
        }
    break;
    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case BUTTON_1:
            {
                GetDlgItemTextA(hWnd, 1, text, 32767);
                MessageBox(NULL, "You pressed the button", "Pressed Button", MB_ICONINFORMATION | MB_OK);
            }
            break;
            case BUTTON_2:
            {
                MessageBox(NULL, "You pressed the second button", "Pressed Button", MB_ICONINFORMATION | MB_OK);
            }
            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;

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;
}

hWnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_szClassName,
    "Test Window",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
    NULL, NULL, hInstance, NULL);

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

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

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

0 个答案:

没有答案