我正在尝试学习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;
}
答案 0 :(得分:4)
当CreateWindowEx()
完成处理时,您对HWND
的调用会将返回的WM_CREATE
分配给超出范围的本地变量。您的WM_COMMAND
处理程序正在使用来自不同范围的hWndEdit
变量,但该变量永远不会使用编辑控件的HWND
进行初始化。这就是你的文字永远不会出现的原因。
关于WM_SETTEXT
,您的主窗口将不会收到该消息。它会直接发送到编辑控件,编辑控件没有分配自定义WndProc()
,因此发送给它的所有邮件都将通过DefWindowProc()
。您可以将WM_SETTEXT
发送到修改控件,并按预期更新(DefWindowProc()
),但不会显示MessageBox()
。编辑控件会将WM_COMMAND
条消息发送到其父窗口,以显示各种EN_...
通知,例如EN_CHANGE
,因此您的主窗口WndProc()
会为{MessageBox()
调用WM_COMMAND
1}}与IDC_EDITBOX
相关的消息。
答案 1 :(得分:0)
每个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以获取支持的通知列表),因此您可能需要子类控制子类
答案 2 :(得分:0)
更改
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