反映SendMessage的Windows API编辑框



我正在尝试学习windows API。我已经设法创建了一个带有按钮和编辑框的窗口。当我点击按钮时,我想尝试改变编辑框中的文本。

这是主循环:

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

这里是windows call back

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

            HWND hWndEdit   = CreateWindowEx(WS_EX_CLIENTEDGE,
                                    "EDIT","",
                                    WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
                                    50,100,200,100,hwnd,
                                    (HMENU) IDC_EDITBOX,
                                    GetModuleHandle(NULL),
                                    NULL);
            HWND hWndButton = CreateWindowEx(NULL,
                                "BUTTON",
                                "OK",
                                WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
                                50, 220, 100, 24, hwnd,
                                (HMENU)IDC_BUTTON,
                                GetModuleHandle(NULL),
                                NULL);
        }
        break;
    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
        case IDC_BUTTON:
            {
                SendMessage(hWndEdit,WM_SETTEXT,NULL,(LPARAM)"BUTTON");
            }
            break;
        case IDC_EDITBOX:
        {
            MessageBox(NULL,"EDIT","editbox", MB_ICONINFORMATION|MB_OK);
        }
        break;
        default:
            MessageBox(NULL,"default","Command",MB_ICONINFORMATION|MB_OK);
            break;
        }
        break;
    case WM_SETTEXT:
        {
                MessageBox(NULL,"SetTEXT","BOX",MB_ICONINFORMATION|MB_OK);
        }
            break;
    case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
    case WM_DESTROY:
            PostQuitMessage(0);
            break;
    default:
        return DefWindowProc(hwnd,msg,wParam,lParam);
    }
    return 0;
}

当我点击按钮时,我调用SendMessage(...),所以不应该在我的主循环中拾取并发送给WndProc()吗?如果是这样,为什么我的开关箱没有感染?如果没有,我如何为这个编辑框设置回调函数?

编辑:完整代码

#include <windows.h>

#define IDC_BUTTON 101
#define IDC_EDITBOX 102
HWND hWndEdit;
const char g_szClassName[] = "myWindowClass";
//Step 4: the Window Proc
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_CREATE:
        {

            HWND hWndEdit   = CreateWindowEx(WS_EX_CLIENTEDGE,
                                    "EDIT","",
                                    WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
                                    50,100,200,100,hwnd,
                                    (HMENU) IDC_EDITBOX,
                                    GetModuleHandle(NULL),
                                    NULL);
            HWND hWndButton = CreateWindowEx(NULL,
                                "BUTTON",
                                "OK",
                                WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
                                50, 220, 100, 24, hwnd,
                                (HMENU)IDC_BUTTON,
                                GetModuleHandle(NULL),
                                NULL);
        }
        break;
    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
        case IDC_BUTTON:
            {
                //MessageBox(NULL,"EDIT","editbox", MB_ICONINFORMATION|MB_OK);
                SendMessage(hWndEdit,WM_SETTEXT,NULL,(LPARAM)"BUTTON");
            }
            break;
        case IDC_EDITBOX:
        {
            MessageBox(NULL,"EDIT","editbox", MB_ICONINFORMATION|MB_OK);
        }
        break;
        default:
            MessageBox(NULL,"default","Command",MB_ICONINFORMATION|MB_OK);
            break;
        }
        break;
    case WM_SETTEXT:
        {
                MessageBox(NULL,"SetTEXT","BOX",MB_ICONINFORMATION|MB_OK);
        }
            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;
    //Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = CS_HREDRAW|CS_VREDRAW;
    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);
    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;
    }
    //Creating the Window
    hwnd = CreateWindowEx(
        0,
        //WS_EX_CLIENTEDGE,
        g_szClassName,
        "Inventory",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
        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;
}

您对CreateWindowEx()的调用将返回的HWND s分配给本地变量,这些变量在WM_CREATE完成处理时超出范围。您的WM_COMMAND处理程序正在使用来自不同作用域的hWndEdit变量,但该变量从未使用编辑控件的HWND初始化。这就是为什么你的文本永远不会出现。

关于WM_SETTEXT,您的主窗口将不会收到该消息。它被直接发送到编辑控件,该控件没有为其分配自定义WndProc(),因此发送给它的所有消息都将通过DefWindowProc()。您可以将WM_SETTEXT发送到编辑控件,它将按预期更新(由DefWindowProc()),但您的MessageBox()将不会出现。一个编辑控件发送WM_COMMAND消息到它的父窗口的各种EN_...通知,如EN_CHANGE,所以你的主窗口WndProc()将调用MessageBox() WM_COMMAND消息与IDC_EDITBOX相关。

每个HWND对象都有自己的窗口过程来处理该窗口的消息。您的WndProc与主窗口相关联,而不是编辑文本窗口或按钮窗口。这就是为什么你没有捕获WM_SETTEXT消息——DispatchMessage将它发送到编辑控件的窗口过程。

控件发送WM_COMMAND(对于user32控件)或WM_NOTIFY(对于commctrl控件)通知到父窗口(例如按钮单击),因此父窗口可以处理来自这些控件的事件而无需子类化控件。

我不知道是否有一个与WM_SETTEXT相关联的回调(见http://msdn.microsoft.com/en-us/library/windows/desktop/bb775458%28v=vs.85%29.aspx在通知部分的支持通知列表),所以你可能需要子类化的子控件。

变化

HWND hWndEdit   = CreateWindowEx(WS_EX_CLIENTEDGE,
                                    "EDIT","",
                                    WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
                                    50,100,200,100,hwnd,
                                    (HMENU) IDC_EDITBOX,
                                    GetModuleHandle(NULL),
                                    NULL);

hWndEdit   = CreateWindowEx(WS_EX_CLIENTEDGE,
                                    "EDIT","",
                                    WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
                                    50,100,200,100,hwnd,
                                    (HMENU) IDC_EDITBOX,
                                    GetModuleHandle(NULL),
                                    NULL);

不需要重新声明hWndEdit,因为你已经全局声明了句柄

相关内容

  • 没有找到相关文章

最新更新