Solved

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

Posted on 2004-04-01
4
1,093 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry, it took time, more than expected, but the tip was perfect.
Thank you
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now