Domanda

I have an edit control:

    HWND hInput = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", 
        WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL |
           ES_WANTRETURN, 
    0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_INPUT, GetModuleHandle(NULL), NULL);

And when a button is pressed I'm getting the text from it and try to replace it with empty string:

        TCHAR buff[2048];
        memset(buff,0,2048);
        GetWindowText(hInput, buff, 2048);
        SetWindowText(hInput,"");

But after that in the edit control there is a new line left.

Any ideas how to remove this new line? Thanks in advance.

EDIT: Actually then the button is pressed it's ok, no new lines.

The edit control has a message procedure which captures the enter key and does the same thing as the button when it's pressed. Here is the procedure:

LRESULT CALLBACK SubClassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
    switch (msg) 
    { 
    case WM_KEYDOWN: 
        switch (wParam) 
        {
        case VK_RETURN: 
            TCHAR buff[2048];
            memset(buff,0,2048);
            GetWindowText(hInput, buff, 2048);
            SetWindowText(hInput,"");
            break;
        } 
        break; 

    } 
    return CallWindowProc(DefProc, hwnd, msg, wParam, lParam); 
} 

But here it leaves the new line.

È stato utile?

Soluzione

When you process the WM_KEYDOWN, you successfully clear the edit control.

Then you pass that message on to the base class, which is the edit control, and it inserts a carriage return into the text. That's one theory. If it's correct, then simply not sending the WM_KEYDOWN message on to the base class will solve the problem.

Another theory is that the WM_KEYDOWN is followed by a WM_CHAR (synthesized by the TranslateMessage), and the edit control is added the carriage return based on that message. If it's correct, then you have an interesting situation where you've said you wanted a multiline edit control, but you're trying to clear the content every time the user tries to create a new line.

I misspoke about the ES_WANTRETURN. That works with the dialog box code to make sure it doesn't steal the carriage return keypresses altogether. That's why I changed my original answer.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top