Bleh...

This is a discussion on Bleh... within the Windows Programming forums, part of the Platform Specific Boards category; Alright, so I've got a regular window and a single-line edit box. I just want to capture the enter key, ...

  1. #1
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230

    Bleh...

    Alright, so I've got a regular window and a single-line edit box. I just want to capture the enter key, so I subclasses it. I ran it through the debugger and foudn out that I subclassed it just fine, but for some reason or another my edit isn't generating any messages (WM_KEYDOWN, WM_CHAR, WM_CREATE, WM_SIZE....nothin') so is there some other way I can do this?

    Also, I'm trying to figure out how to make the edit non-resizable (and keep a thick frame around it), but seeing how it doesn't generate any messages that's proving difficult

    Any help? I can always post some code if you want, but then I'd have to go upstairs and take it off my laptop...not in the mood right now :-/

  2. #2
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Are you using CallWindowProc() with a pointer to the regular procedure rather than DefWindowProc() under your default message case?

    Any help? I can always post some code if you want, but then I'd have to go upstairs and take it off my laptop
    What? You don't have current backups on your other computers? Bad, very bad.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    Yup, using CallWindowProc...I took the subclassing code sout of Petzold.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Ken,

    Sounds quite strange to me. You mentioned that you have a normal window, do you mean you used CreateWindow() or CreateWindowEx()???? If so, it seems funny that you're not receiving the keypress messages.

    If you have the control on a dialog box of any kind, then maybe you should try this:

    Code:
    BOOL CALLBACK MyEditSubclassProc(HWND control, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	if(msg == WM_GETDLGCODE)
    	{
    		MSG* pMsg = (MSG*)lParam;
    		if(pMsg && pMsg->message == WM_KEYDOWN)
    		{
    			// if the key press is the enter key
    			if(pMsg->wParam == VK_RETURN)
    			{
    				// tell windows we are going to handle it ourselves.
    				return(DLGC_WANTALLKEYS);
    			}
    		}
    	}
    
    	return(CallWindowProc(previousProc, control, msg, wParam, lParam));
    }
    But if it IS a window and you're still not getting messages, could ya post some code for me?

    good luck!
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    Hmm....well one thing I sure as hell don't have is this:

    MSG* pMsg = (MSG*)lParam;
    if(pMsg && pMsg->message == WM_KEYDOWN)

    but it is just a regular (CreateWindowEx) window...so do I only need that if it's a dialog or should I have that in there?

    Tomorrow, if I get some time, I'll post up some code...until then I'll just mangle...err...fiddle with my code a bit more

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Try this int the message loop to be sure:

    Code:
    while(GetMessage(&messages, NULL, 0, 0))
     {
      if(messages.message == WM_KEYDOWN)
        if(LOWORD(messages.wParam) == VK_RETURN)
          MessageBox("Enter was pressed.", "Note: This Edit Is Sending Messages...", 0);
      TranslateMessage(&messages);
      DispatchMessage(&messages);
     }
    return messages.wParam;
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #7
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    I'll try it out...thanks.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    this source code to a little project i made a while ago captures the enter key from an edit box...
    http://www.blankwebsite.com/swilly2.txt
    sorry but the code is a bit of a mess.

    and you can see it in action
    http://www.blankwebsite.com/swilly.exe

  9. #9
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    Ahhh...but you subclass the exact same way I do...damnit...I'll try it out, though...thanks.

  10. #10
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    That code is HORRIDLY mangled you perform the same catch 3 different times....hmmm...wonder which one actually works? ::quickly removes some code:: Hmm...well it looks like I called SetWindowLong in the wrong spot...maybe I'll try changing that around...hmm....although one appearance of your enter-key-catching code is entirely useless............

    thanks anyhow

  11. #11
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    This is how it can be done:
    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,(DWO
    RD)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;
    }
    Take a look at these threads for the full information on this code:
    http://www.cprogramming.com/cboard/s...=SetWindowLong
    http://www.cprogramming.com/cboard/s...highlight=edit

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  12. #12
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    Allow me to reiterate the title of this thread: BLEH....you'll never guess what the problem was....I subclassed the wrong damn edit window. And since I never allow focus to be set to the other, the code would still never execute

    Isn't it amazing how "hard" bugs turn out to be the simplest things? I once had a bug that I searched for for about a month, and when I finally figured out it was something like this...

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Funny. Similar to my little bugs like:


    printf("Debugging String: ", string);

    me: "why won't the string print, dammit!!"
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I fix these annoying warnings and errors?
    By zyphirr in forum C Programming
    Replies: 18
    Last Post: 11-12-2008, 05:19 PM
  2. Printing a number of HTML documents
    By kasemo in forum C# Programming
    Replies: 0
    Last Post: 10-25-2007, 05:10 PM
  3. pointer to structure question
    By matthughes in forum C Programming
    Replies: 8
    Last Post: 05-19-2007, 01:07 AM
  4. computer science rant
    By cozman in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-10-2001, 11:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21