无法更改 WinAPI 中的编辑控件文本
Edit Control Text in WinAPI can't be changed
这段代码总是有效,我不知道出了什么问题,见下文:
CreateWindowW(L"EDIT", L"Type Here!", WS_CHILD | WS_VISIBLE | WS_BORDER, 10, 10, 150, 25, hwnd, (HMENU)ID_TEXTBOX1, NULL, NULL);
当程序运行时创建控件时,可以选择文本并使用SetWindowText函数进行更改,但不能通过键入来更改文本,为什么?
我已经看到了堆栈溢出的主题:无法选择或编辑Win32 API编辑控件,但即使使用SetFocus函数或EnableWindow,它仍然不起作用。
这是整个过程的功能:
#include <windows.h>
// IDs dos controles
#define ID_TEXTBOX1 1000
#define ID_BUTTON1 1001
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR pCmdLine, int nCmdShow)
{
MSG msg;
HWND hwnd;
WNDCLASSW wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.lpszClassName = L"WINDOW";
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpszMenuName = NULL;
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassW(&wc);
hwnd = CreateWindowW(L"WINDOW", L"Janela",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 100, 200, 200, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while( GetMessage(&msg, NULL, 0, 0)) {
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
// Here is creted the edit control
CreateWindowW(L"EDIT", L"Type Here!", WS_CHILD | WS_VISIBLE | WS_BORDER, 10, 10, 150, 25, hwnd, (HMENU)ID_TEXTBOX1, NULL, NULL);
// Functions that i tried
//EnableWindow(GetDlgItem(hwnd, ID_TEXTBOX1), true);
//SetFocus(GetDlgItem(hwnd, ID_TEXTBOX1));
// Here is create a button
CreateWindowW(L"BUTTON", L"Show Text", WS_CHILD | WS_VISIBLE, 10, 45, 100, 20, hwnd, (HMENU)ID_BUTTON1, NULL, NULL);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_BUTTON1:
int len = GetWindowTextLengthW(GetDlgItem(hwnd, ID_TEXTBOX1)) + 1;
wchar_t *txt = new wchar_t[len];
GetWindowText(GetDlgItem(hwnd, ID_TEXTBOX1), txt, len);
//
MessageBox(NULL, txt, L"Info", MB_OK);
delete txt;
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
您在消息循环中缺少对 TranslateMessage 的调用,从而阻止键盘输入生成WM_CHAR
/WM_UNICHAR
消息。这将使"编辑"控件看起来像没有获得任何输入。有关标准消息循环实现,请参阅 GetMessage。
顺便说一句,在分配数组时,您需要使用数组删除运算符,即 delete[] txt;
.
相关文章:
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 我的主窗口在创建时或单击更新区域时是否会收到编辑控件?
- 当光标位置在编辑控件 MFC 中更改时是否有通知代码?
- 为什么WS_TABSTOP不适用于丰富的编辑控件?
- 编辑控件无法正常工作
- 使用 GetWindowTextW 从编辑控件获取希伯来语文本
- 如何使用面向对象的技术在 win32 和 c++ 中创建编辑控件
- 如何检查编辑控件的滚动条是否位于底部
- C 本机Win32简单的编辑控件
- WinAPI - 如何打印文本以编辑控件
- MFC Rich编辑控件2.0文本颜色
- 编辑控件边框和WM_CTLCOLOREDIT
- 如何在对话框首次启动时禁用编辑控件的焦点?
- 十进制数字的区域设置感知编辑控件子类化(格式[sign][xxx..][decimal separator][yy.])
- 无法在编辑控件上设置字体
- c++ MFC 将编辑控件中的字符串转换为十六进制
- Win32 API:如何在编辑控件中捕获转义键?
- 如何在不显示文本的情况下分配Win32编辑控件的窗口名称
- C MFC创建编辑控件可以拥有的最大数字
- mfc c++通过编辑控件设置另一个应用程序的HWND地址