Solved

Making An "EDIT" Box Created With Createwindow(), Listen To VK_RETURN (ENTER) Key?

Posted on 2004-04-01
4
1,101 Views
Last Modified: 2013-12-03
Greetings Everybody

I am using Win32 API, no MFC, and on the Main Window only (ghwndAPP), I have created one Edit Box (no dialog-box) Using,

hwndeditbox=CreateWindow("EDIT",NULL,WS_VISIBLE | WS_CHILD | ES_LEFT ,240,510,316,24,ghwndApp,NULL,(HINSTANCE)GetWindowLong(ghwndApp,GWL_HINSTANCE),NULL);

I can enter text, can retrieve text etc things, using edit box messages. What right now I am doing is after the user enters the text, there is one button pressing which, the text entered goes to a txt file.
What I actually need now is in the text box even if the user presses ENTER Key, the required functionality(text going to txt file) should happen.

I am reading MSDN, using WM_KEYDOWN, WM_COMMAND etc things but not able to achieve.
Might be I am facing problem, because the TEXT Box, is not in a Dialog-Box, or might be I am not getting things right in interpreting the right Sequence of events and messages.

Please guide me, and if 10-12 lines of template code can be posted that will really great.

Thanks you for your help

Regards,
Bonns
0
Comment
Question by:bonnseye
  • 2
4 Comments
 

Expert Comment

by:narendj
ID: 10731457
Bonns,

You mentioned you don't want the user to be able to hit the "Enter" Key... So I presume you want only a single line of Input.

With that assumption, you can do the following

> Create the Edit box for single line input - you have already done this.
> Add "BS_DEFPUSHBUTTON" style to that "button".

Let me know how it goes.

Happy coding.
 
0
 
LVL 6

Expert Comment

by:joghurt
ID: 10740227
Or: Subclass the Edit window for your own edit control. It surely works, more versatily (you can capture any other keys, etc.) though a bit more complicated.

If you have only one or a few such edit controls in your dialog it may be simpler to override the WNDPROC only for that control(s). If you have more edit controls with this behaviour, it's more simple to change it for the "EDIT" window class and create all the controls afterwards.

I can supply you with an example for both methods if you want. Just tell in which are you interested.
0
 
LVL 6

Accepted Solution

by:
joghurt earned 100 total points
ID: 10753146
static WNDPROC      g_lpOldEditProc = NULL;


static long FAR PASCAL EditSubProc (HWND hWnd, UINT wMessage, WPARAM wParam, LPARAM lParam)
{
      HWND      hDlg = GetParent (hWnd);

      switch (wMessage) {
            case WM_GETDLGCODE:
//                  return (DLGC_WANTALLKEYS | CallWindowProc (g_lpOldEditProc, hWnd, wMessage, wParam, lParam));
                  return (DLGC_WANTCHARS | CallWindowProc (g_lpOldEditProc, hWnd, wMessage, wParam, lParam));
                  // break;

            case WM_CHAR: {
/*                  //Process this message to avoid message beeps.
                  if ((wParam == VK_RETURN) || (wParam == VK_TAB))
                        return 0;*/
                  break;
            }

            case WM_KEYDOWN:
/*                  if ((wParam == VK_RETURN) || (wParam == VK_TAB)) {
                        if (hDlg != NULL)
                              PostMessage (hDlg, WM_NEXTDLGCTL, 0, 0L);
                        return FALSE;
                  }*/
                  break;

            default:
                  break;
      }

      return CallWindowProc (g_lpOldEditProc, hWnd, wMessage, wParam, lParam);
}


and:
HWND      hWndEdit = CreateWindow ("Edit", "", 0, 0, 0, 20, 20, hMainWnd, NULL, hInst, NULL);
g_lpOldEditProc = reinterpret_cast<WNDPROC> (GetClassLongPtr (hWndEdit, GCLP_WNDPROC));
SetClassLongPtr (hWndEdit, GCLP_WNDPROC, reinterpret_cast<LONG_PTR> (EditSubProc));
DestroyWindow (hWndEdit);
0
 

Author Comment

by:bonnseye
ID: 10768512
Sorry, it took time, more than expected, but the tip was perfect.
Thank you
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question