Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm hanging on to my edit control window handle and calling GetWindowText every time I get a WM_COMMAND with EN_CHANGE on the parent window

From there I don't get any reasonable value back. It's just an empty string.

How the heck do I get the text from an edit control if not with GetWindowText?

Why doesn't the code in what I tried work?

What I have tried:

C++
static LRESULT CALLBACK WindowProcGpio(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    if(uMsg==WM_CREATE) {
        gpio_t& g = *(gpio_t*)((LPCREATESTRUCT) lParam)->lpCreateParams;
        SetWindowLongPtrW(hWnd,GWLP_USERDATA,(LONG_PTR)&g);
        HWND hwnd_st_u, hwnd_ed_u;
        int x, w, y, h;
        y = 10; h = 20;
        x = 10; w = 50;
        hwnd_st_u = CreateWindowW(L"static", L"ST_U",
                              WS_CHILD | WS_VISIBLE | WS_TABSTOP,
                              x, y, w, h,
                              hWnd, (HMENU)(501),
                              (HINSTANCE) GetModuleHandle(NULL), NULL);
        SetWindowTextW(hwnd_st_u, L"Value:");

        x += w; w = 60;
        hwnd_ed_u = CreateWindowW(L"edit", L"",
                                    WS_CHILD | WS_VISIBLE | WS_TABSTOP | (WS_DISABLED*(!g.is_input()))
                                    | ES_LEFT | WS_BORDER,
                                    x, y, w, h,
                                    hWnd, (HMENU)(502),
                                    (HINSTANCE) GetModuleHandle(NULL), NULL);
        wchar_t val[64];
        if(g.value==HIGH) {
            wcscpy(val,L"HIGH");
        } else if(g.value==LOW) {
            wcscpy(val,L"LOW");
        } else {
            _itow(g.value,val,10);
        }
        SetWindowTextW(hwnd_ed_u, val);
        g.hwnd_text = hwnd_ed_u;
        
    }
    if(uMsg==WM_COMMAND) {
        if(HIWORD (wParam) == EN_CHANGE) {
            uint8_t gpio = GetWindowLongPtrW(hWnd,GWLP_USERDATA);
            wchar_t sz[1024];
            gpio_t& g = gpios[gpio];
            GetWindowTextW(g.hwnd_text,sz,sizeof(sz)/sizeof(wchar_t));
            char ssz[1024];
            GetWindowTextA(g.hwnd_text,ssz,sizeof(ssz));
            Serial.print(ssz);
            Serial.print("\r\n");
            sz[63]=0;
            if(0==wcsicmp(sz,L"LOW")) {
                g.value=LOW;
            } else if(0==wcsicmp(sz,L"HIGH")) {
                g.value=HIGH;
            } else {
                size_t l = wcslen(sz);
                bool isnum = true;
                for(int i = 0;i<l;++i) {
                    if(!iswdigit(sz[i])) {
                        isnum = false;
                        break;
                    }
                }
                if(isnum) {
                    g.value = _wtoi(sz);
                }
            }
        }
        return 0;
    }
    if (uMsg == WM_CLOSE) {
        gpio_t& g = *(gpio_t*)GetWindowLongPtrW(hWnd,GWLP_USERDATA);
        g.hwnd_text = NULL;
    }
   
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
Posted

1 solution

You need to use the HWND in the lParam of the WindowProc instead of the HWND retrieved from CreateWindowEx on the edit control.
 
Share this answer
 
Comments
Richard MacCutchan 22-Sep-23 5:33am    
They should both be the same, since you save the EDIT control's HWND after you create it. You may like to check that woith the debugger.
honey the codewitch 22-Sep-23 7:00am    
Should be, but aren't, and I don't care, because the other HWND works for SetWindowText. Good enough for me. GDB doesn't like me anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900