1

我遵循了有关一些 win32、创建和交互的教程,但我不确定代码如何将“在此处写入”编辑框连接到消息框。

#define IDC_MAIN_BUTTON 101         // Button identifier
#define IDC_MAIN_EDIT   102         // Edit box identifier
HWND hEdit;

LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{
    WNDCLASSEX wClass;
    ZeroMemory(&wClass,sizeof(WNDCLASSEX));
    wClass.cbClsExtra=NULL;
    wClass.cbSize=sizeof(WNDCLASSEX);
    wClass.cbWndExtra=NULL;
    wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
    wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wClass.hIcon=NULL;
    wClass.hIconSm=NULL;
    wClass.hInstance=hInst;
    wClass.lpfnWndProc=(WNDPROC)WinProc;
    wClass.lpszClassName="Window Class";
    wClass.lpszMenuName=NULL;
    wClass.style=CS_HREDRAW|CS_VREDRAW;

    if(!RegisterClassEx(&wClass))
    {
        int nResult=GetLastError();
        MessageBox(NULL,
            "Window class creation failed\r\n",
            "Window Class Failed",
            MB_ICONERROR);
    }

    HWND hWnd=CreateWindowEx(NULL,
            "Window Class",
            "Windows application",
            WS_OVERLAPPEDWINDOW,
            200,
            200,
            640,
            480,
            NULL,
            NULL,
            hInst,
            NULL);

    if(!hWnd)
    {
        int nResult=GetLastError();

        MessageBox(NULL,
            "Window creation failed\r\n",
            "Window Creation Failed",
            MB_ICONERROR);
    }

    ShowWindow(hWnd,nShowCmd);

    MSG msg;
    ZeroMemory(&msg,sizeof(MSG));

    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
        {

// How does this edit box, connect to the button?

            // Create an edit box
            hEdit=CreateWindowEx(WS_EX_CLIENTEDGE,
                "EDIT",
                "",
                WS_CHILD|WS_VISIBLE|
                ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
                50,
                100,
                200,
                100,8
                hWnd,
                (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
            HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
            SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE,0));
            SendMessage(hEdit, WM_SETTEXT, NULL, (LPARAM)"Insert text here...");

            // Create a push button
            HWND hWndButton=CreateWindowEx(NULL,
                "BUTTON",
                "OK",
                WS_TABSTOP|WS_VISIBLE|
                WS_CHILD|BS_DEFPUSHBUTTON,
                50,
                220,
                100,
                24,
                hWnd,
                (HMENU)IDC_MAIN_BUTTON, GetModuleHandle(NULL), NULL);
            SendMessage(hWndButton, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE,0));
        }
        break;



        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDC_MAIN_BUTTON:
                {
                    char buffer[256];
                    SendMessage(hEdit, WM_GETTEXT, sizeof(buffer)/sizeof(buffer[0]), reinterpret_cast<LPARAM>(buffer));
                    MessageBox(NULL, buffer, "Information", MB_ICONINFORMATION);
                }
                break;
            }
            break;

        case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }
        break;
    }

    return DefWindowProc(hWnd,msg,wParam,lParam);
}

我真的不确定文本框如何将其信息发送到消息框。

4

3 回答 3

4

关键是这三行:

char buffer[256];
SendMessage(hEdit, WM_GETTEXT, sizeof(buffer)/sizeof(buffer[0]), reinterpret_cast<LPARAM>(buffer));
MessageBox(NULL, buffer, "Information", MB_ICONINFORMATION);

WM_GETTEXT消息执行以下操作:

将对应于窗口的文本复制到调用者提供的缓冲区中。

因此,第一次调用会将窗口中的SendMessage内容复制到您之前创建的行。hEditbuffer

然后下一行将的内容buffer写入MessageBox

于 2012-09-24T14:51:55.300 回答
1

当你这样做时CreateWindowEx,它会创建一个消息队列。然后任何人都可以使用它的句柄向这个窗口发送消息。

CreateWindowEx()你给 id 时IDC_MAIN_EDIT。因此,当您按下按钮时,这将作为命令发送到您的window-message-Queue。这是在您的proc()情况下处理的IDC_MAIN_EDIT:。

于 2012-09-24T15:04:35.137 回答
0

文本框不会“发送其信息”。单击按钮时,案例WM_COMMAND运行并从文本框中获取内容。这就是SendMessage(..., WM_GETTEXT, ...)电话。

于 2012-09-24T14:51:32.763 回答