简体   繁体   中英

Edit Control Text in WinAPI can't be changed

this code always worked, i don't know what is wrong, see below:

CreateWindowW(L"EDIT", L"Type Here!", WS_CHILD | WS_VISIBLE | WS_BORDER, 10, 10, 150, 25, hwnd, (HMENU)ID_TEXTBOX1, NULL, NULL);

when the program is running the control is created, is possible select the text and change using the SetWindowText function, but cannot change the text by typing, why?

i have seen this topic of stack overflow: win32 api edit control can't be selected or edited , but even using the SetFocus function or the EnableWindow, it still does not work.

this is the whole procedure function:

#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);
}

You are missing a call to TranslateMessage in your message loop, preventing keyboard input from generating WM_CHAR / WM_UNICHAR messages. This will make your Edit control look like it's not getting any input. See GetMessage for a standard message loop implementation.

As an aside, when allocating an array, you need to use the array delete operator, ie delete[] txt; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM