Thread: Problem with text field.

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212

    Question Problem with text field.

    How do I change my code so that after typing in the text field, when I press enter, it has the same effect as pressing the "Translate!" button.

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <time.h>
    #define compare(a1,b1) !stricmp(a1,b1)
    #define ID_OF_BUTTON 1101
    #define ID_MYEDIT 3460 
    const char g_szClassName[] = "myWindowClass";
    
    HWND hWndMyEdit;
    
    int doSwilly(char *buf)
    {
     // this what happens when you click translate
     return 0;
    }
    
    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        char buf[500];
    
        switch(msg)
        {
            case WM_CLOSE:
                  DeleteFile("temp.t");
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
             DeleteFile("temp.t");
                PostQuitMessage(0);
            break;
            case WM_COMMAND: 
            switch(LOWORD(wParam))
            {
            case ID_OF_BUTTON:
            GetWindowText(hWndMyEdit, buf, 500);
            doSwilly(buf);
            break;
            }
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
        //Step 1: Registering the Window Class
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
        srand(time(NULL));
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "Swilly translator, version 2.",
            WS_SYSMENU,
            CW_USEDEFAULT, CW_USEDEFAULT, 580, 50,
            NULL, NULL, hInstance, NULL);
            hWndMyEdit = CreateWindow("EDIT",
                          "", WS_CHILD | WS_VISIBLE,
                          0, 0, 489,20, hwnd,
                          (HMENU) ID_MYEDIT,
                          hInstance, NULL);
    
        CreateWindow(
         "button",
         "Translate!",
         WS_CHILD | WS_VISIBLE,
         489, 0,
         80, 20,
         hwnd,
         (HMENU)ID_OF_BUTTON,
         0,
         NULL
        );
    
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        // Step 3: The Message Loop
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Add this to your windows procedure;

    Code:
    case WM_KEYDOWN:
    	if((int) wParam == VK_RETURN){
    	GetWindowText(hWndMyEdit, buf, 500);
    	doSwilly(buf);
    	}
    	else return DefWindowProc(hwnd, msg, wParam, lParam);
    	break;

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    That works, cheers

    Oh, actually it doesn't

    Damn my premptive posting.

    It works as long as the text field aint selected
    Last edited by Brian; 03-13-2002 at 12:15 PM.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Whoa....I see...when you are typing the focus is on the edit so it wont work....give me a minute

  5. #5
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    ...and maybe you could tell me how I make it so the text field is automatically in focus? please?

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I guess the easyies way would be to subclass the edit window......


    I dont think standard controls send parent notifications...


    I have something I have to do right now, but if nobody has answered before I get back I will look at it again...sorry..

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Sorry for the delay mate...had to go online to do my weekly shop or I'd be eating carrots all damn week


    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <time.h>
    #define compare(a1,b1) !stricmp(a1,b1)
    #define ID_OF_BUTTON 1101
    #define ID_MYEDIT 3460
    const char g_szClassName[] = "myWindowClass";
    
    HWND hWndMyEdit;
    WNDPROC MainWindProc;
    
    
    int doSwilly(char *buf)
    {
     // this what happens when you click translate
    	MessageBox(0,"","",MB_OK);
     return 0;
    }
    
    LRESULT CALLBACK EditProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	
    	char buf[500];
    	switch(msg){
    		case WM_CHAR:
    			if((int) wParam == VK_RETURN){
    				GetWindowText(hwnd, buf, 500);
    				doSwilly(buf);
    				break;
    			}
    			else return CallWindowProc(MainWindProc,hwnd, msg, wParam, lParam);
    			break;
    	}
    	return CallWindowProc(MainWindProc,hwnd, msg, wParam, lParam);
    }
    
    
    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        char buf[500];
    
        switch(msg)
        {
    		case WM_KEYDOWN:
    			if((int) wParam == VK_RETURN){
    				GetWindowText(hWndMyEdit, buf, 500);
    				doSwilly(buf);
    				break;
    			}
    			else return DefWindowProc(hwnd, msg, wParam, lParam);
    			break;
            case WM_CLOSE:
                  DeleteFile("temp.t");
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
             DeleteFile("temp.t");
                PostQuitMessage(0);
            break;
            case WM_COMMAND:
            switch(LOWORD(wParam))
            {
            case ID_OF_BUTTON:
            GetWindowText(hWndMyEdit, buf, 500);
            doSwilly(buf);
            break;
            }
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
        //Step 1: Registering the Window Class
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
        srand(time(NULL));
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "Swilly translator, version 2.",
            WS_SYSMENU,
            CW_USEDEFAULT, CW_USEDEFAULT, 580, 50,
            NULL, NULL, hInstance, NULL);
            
    	hWndMyEdit = CreateWindow("EDIT",
                          "", WS_CHILD | WS_VISIBLE,
                          0, 0, 489,20, hwnd,
                          (HMENU) ID_MYEDIT,
                          hInstance, NULL);
    
        CreateWindow(
         "button",
         "Translate!",
         WS_CHILD | WS_VISIBLE ,
         489, 0,
         80, 20,
         hwnd,
         (HMENU)ID_OF_BUTTON,
         0,
         NULL
        );
    
    	MainWindProc =  (WNDPROC)SetWindowLong(hWndMyEdit,GWL_WNDPROC,(DWORD)EditProc);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        // Step 3: The Message Loop
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
    I have used SetWindowLong to sublass your edit box and filter out all of the enter key presses........have a go and see if its ok for you

  8. #8
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Thanks very much, that does work.


    Now how can I make it automatically focus the text field when the program starts?

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        char buf[500];
    
    
        switch(msg)
        {
    		
                        case WM_ACTIVATE:
    			SetFocus(hWndMyEdit);
    			break;
    		case WM_KEYDOWN:
    			if((int) wParam == VK_RETURN){
    				GetWindowText(hWndMyEdit, buf, 500);
    				doSwilly(buf);
    				break;
    			}
    			else return DefWindowProc(hwnd, msg, wParam, lParam);
    			break;
            case WM_CLOSE:
                  DeleteFile("temp.t");
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
             DeleteFile("temp.t");
                PostQuitMessage(0);
            break;
            case WM_COMMAND:
            switch(LOWORD(wParam))
            {
            case ID_OF_BUTTON:
            GetWindowText(hWndMyEdit, buf, 500);
            doSwilly(buf);
            break;
            }
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Thanks again, you've been a great help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input a Hex number and output hex number to a text field
    By zoobaby in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 11:26 AM
  2. Input From a Text File - A Specific Problem.
    By Eddie K in forum C Programming
    Replies: 4
    Last Post: 03-09-2006, 03:50 PM
  3. Problem with making static text using my class
    By Dae in forum Windows Programming
    Replies: 4
    Last Post: 06-17-2005, 12:02 AM
  4. Replies: 1
    Last Post: 07-13-2002, 05:45 PM
  5. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 04:44 PM