gpt4 book ai didi

c++ - 无法更改 WinAPI 中的编辑控制文本

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

这段代码一直有效,我不知道哪里出了问题,见下文:

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 edit control can't be selected or edited ,但即使使用 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消息。这将使您的 Edit 控件看起来没有任何输入。参见 GetMessage用于标准消息循环实现。

顺便说一句,在分配数组时,需要使用数组删除运算符,即delete[] txt;

关于c++ - 无法更改 WinAPI 中的编辑控制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27800050/

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