CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2012
    Posts
    2

    Question Prevent VK_RETURN from being processed in subclassed edit control.

    I have subclassed an Edit control for the purpose of detecting the enter key being pressed then calling a function accordingly. All that part seems to work just fine so far. The problem I am having is that I then want the Edit control to not move to the next line. I have tried simply returning 0 after calling my procedure that should trigger on each press of the enter key, but this does not seem to prevent the Edit box from moving down to the next line.

    This is what I have thus far:
    Code:
    // ...
    HWND g_hEdit;
    WNDPROC g_EditProc;
    // ...
    
    // Subclassed edit window procedure.
    LRESULT WINAPI EditProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    // Check for Enter key to start sending chunks.
    switch (msg)
    {
        case WM_KEYDOWN:
    	switch (LOWORD(wParam))
    	{
    	    case VK_RETURN:
    		SendChat();
    		break; // tried replacing with 'return 0;' and it did not have the desired effect.
    	}
    	break;
        case WM_CLOSE:
    	SetWindowLong(g_hEdit, GWL_WNDPROC, (DWORD)g_EditProc);
    	break;
        case WM_DESTROY:
    	SetWindowLong(g_hEdit, GWL_WNDPROC, (DWORD)g_EditProc);
    	break;
    }
    return CallWindowProc(g_EditProc, hWnd, msg, wParam, lParam);
    }
    
    // Main window procedure.
    LRESULT CALLBACK MainWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    RECT rcMain;
    
    switch (msg)
    {
        // Build child windows.
        case WM_CREATE:
    	g_hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
    	WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL,
    	0, 0, 290, 170, hWnd, NULL, GetModuleHandle(NULL), NULL);
    	g_EditProc = (WNDPROC)SetWindowLong(g_hEdit, GWL_WNDPROC, (DWORD)EditProc);
    	break;
    // ...
    I have searched online and found some examples on how to disable Ctrl+V and Ctrl+Alt+Del and the like. These seem to be a bit advanced and outside the scope of something that to me must be much simpler. I also found examples on how to prevent normal roman characters and numbers from being sent, they suggested using return 0. This solution doesn't seem to work in regards to Enter though.

  2. #2
    Join Date
    Oct 2012
    Posts
    8

    Re: Prevent VK_RETURN from being processed in subclassed edit control.

    I think the return of 1 not zero will block return

  3. #3
    Join Date
    Oct 2012
    Posts
    8

    Re: Prevent VK_RETURN from being processed in subclassed edit control.

    That's wrong, I was thinking of a hook process. Maybe you need to trap a line feed as well as the carriage return.

  4. #4
    Join Date
    Dec 2012
    Posts
    2

    Re: Prevent VK_RETURN from being processed in subclassed edit control.

    guestgulkan from cplusplus.com forums wrote:

    I tried your code and it does do as you say - so I thought the edit box
    might still be recieving the WM_CHAR with carriage return - so I did this:

    Code:
    // Subclassed edit window procedure.
    LRESULT WINAPI EditProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        // Check for Enter key to start sending chunks.
        switch (msg)
        {
        case WM_KEYDOWN:
            switch (LOWORD(wParam))
            {
            case VK_RETURN:
                SendChat();
                //break; // tried replacing with 'return 0;' and it did not have the desired effect.
                return 0;
            }
    
        case WM_CHAR:
            if (wParam == 0x0d) //check for carriage return
                return 0;
            break;
    
        case WM_CLOSE:
            SetWindowLong(g_hEdit, GWL_WNDPROC, (DWORD)g_EditProc);
            break;
        case WM_DESTROY:
            SetWindowLong(g_hEdit, GWL_WNDPROC, (DWORD)g_EditProc);
            break;
        }
        return CallWindowProc(g_EditProc, hWnd, msg, wParam, lParam);
    }
    It seemed to do the trick for me.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured