[英]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
消息。 這將使您的Edit控件看起來沒有任何輸入。 有關標准消息循環實現,請參見GetMessage 。
另外,分配數組時,需要使用數組刪除操作符,即delete[] txt;
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.