Thread: Subclassing an edit box

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    "...i have looked at a truckload of websites on google relating to subclassing an editbox and none offer a simple clear-cut code example..."

    Not?? Have you seen the win32 reference manual on the section 'Subclassing a window'? Have you searched here in cboard? Myself was involved in a thread about subclassing an edit control on a listview to catch the enter keypress... yes the same subclass you are looking for.

    Subclassing is easy and helpful (also very funny to prog), have several steps: first create the control you want to subclass, then get its procedure and copy it to recoverable location and assign a new procedure; in that new procedure you have to check if you use some of its skills, if not then you have to redirect to the default and previously stored control procedure. A little bit of code:

    Code:
    WNDPROC previous_boring_subclassed_edit_proc;//global variable to save a copy of the default control procedure
    
    void subclass_the_edit_control(HWND h_edit) {
        previous_boring_subclassed_edit_proc = (WNDPROC)GetWindowLong(h_edit, GWL_WNDPROC);//get the default procedure
        SetWindowLong(h_edit, GWL_WNDPROC, (LONG_PTR)yeah_my_new_subclassed_edit_proc);//set your custom procedure :)
    }
    your new control procedure will look like a standard window procedure, except it will be expecting and responding to WM_KEYDOWN message for VK_RETURN key press; some code:

    Code:
    LRESULT CALLBACK yeah_my_new_subclassed_edit_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
        switch(msg) {
            case WM_KEYDOWN: {
                switch (wParam) {
                    case VK_RETURN: {
                        //simulate a button press, see below
                        return 0;//if the procedure responds the action, finish the control procedure
                    }
                    break;
                }
            }
            break;
        }
        return CallWindowProc(previous_boring_subclassed_edit_proc, hwnd, msg, wParam, lParam);
    }
    As you can see, if the program reaches the last function line is because the user haven't pressed enter, son you will have to redirect to the default procedure to go on with the default message processing (paint messages, other keydown messages, etc stuffs)

    Now you have to simulate the button press, just get the parent handler and send it a WM_COMMAND with BUTTON id as low order first message argument, the explanation is hard but the code is pretty simple:

    Code:
    HWND h_parent = (HWND)GetWindowLong(hwnd, GWL_HWNDPARENT);//get parent handler
    SendMessage(h_parent, WM_COMMAND, (WPARAM)MAKELPARAM(BUTTON, 0), 0);//simulate a control action


    "...What is the purpose of this code: if (lParam && LOWORD (wParam) == TXTBOX2)..."
    Good question, the purpose is to check if the user attempts to access the TXTBOX2 control, if you see the next code lines you'll see that the response to that is a focus jump to main window, is a way of preventing the user to enter data to the TXTBOX2 edit control. Is not necessary, just set ES_READONLY as control style when create it and delete this part of code; or since you have learned to subclass a control, you can subclass the TXTBOX2 control to reject whatever keypress message


    Hope that helps
    Niara
    Last edited by Niara; 05-26-2012 at 11:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an Edit Box (Subclassing)
    By csonx_p in forum Windows Programming
    Replies: 9
    Last Post: 05-05-2008, 06:36 AM
  2. Subclassing
    By alex0751 in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2008, 06:39 PM
  3. WTL Subclassing
    By Tonto in forum Windows Programming
    Replies: 1
    Last Post: 06-05-2007, 11:46 AM
  4. Subclassing
    By Mithoric in forum Windows Programming
    Replies: 5
    Last Post: 11-22-2003, 05:30 PM
  5. Subclassing edit control - slight problem
    By Shag in forum Windows Programming
    Replies: 3
    Last Post: 11-03-2002, 12:33 AM