gpt4 book ai didi

c++ - 反射(reflect) SendMessage 的 Windows API 编辑框

转载 作者:行者123 更新时间:2023-11-30 01:53:57 28 4
gpt4 key购买 nike

我正在尝试学习 Windows API。我设法创建了一个带有按钮和编辑框的窗口。我想尝试在单击按钮时更改编辑框中的文本。

这是主循环:

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

这里是windows回调

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 分配给 局部变量,当 WM_CREATE 已完成处理。您的 WM_COMMAND 处理程序正在使用来自不同范围的 hWndEdit 变量,但该变量从未使用编辑控件的 HWND 进行初始化。这就是为什么您的文字永远不会出现。

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

关于c++ - 反射(reflect) SendMessage 的 Windows API 编辑框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22571902/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com