Solved

Multiple subclassing in SDK ...

Posted on 2001-08-16
3
378 Views
Last Modified: 2008-03-17
Hi,

I'm writing a Windows SDK application (no MFC). It is a dialog box based app with 5 controls - 2 textbox controls, 2 buttons and 1 ListView control. I want to handle the key strokes on both the text boxes - which is possible by subclassing. The problem I am facing is that I'm able to subclass only one of the text box control. Here is a portion of my code:

WNDPROC fnOldProc;

BOOL WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
{
        HWND hDlg = 0;
     MSG msg = {0};
     
     hDlg = CreateDialog (hInstance, MAKEINTRESOURCE
        (IDD_SEARCHDLG), 0, (DLGPROC)SearchDlgProc);
     if (hDlg == 0)
     {
          MessageBox (0, "Unable to Initialize
                Search Box", "Error", MB_OK);
          return -1;
     }

     ShowWindow (hDlg, SW_SHOW);
     UpdateWindow (hDlg);
     
     while (GetMessage (&msg, hDlg ,0, 0) > 0  )
     {
           if (hDlg == 0 || !IsDialogMessage (hDlg, &msg))
        {
             TranslateMessage(&msg);
             DispatchMessage(&msg);
        }
     }
     
     return msg.wParam;
}

/* call back function for the dialog box */
BOOL CALLBACK SearchDlgProc (HWND hWnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
     case WM_INITDIALOG:
            HWND hText1;
            hText1 = GetDlgItem(hWnd, IDC_TEXT1);
            fnOldProc = (WNDPROC) SetWindowLong(hText1,
            GWL_WNDPROC, (LONG)Text1Proc);
            return TRUE;
    }
    return FALSE;
}

/* call back function for IDC_TEXT1 */
BOOL CALLBACK Text1Proc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
     
     switch (msg)
     {
     case WM_CHAR:
          /* handle key strokes */
          break;
     }
     
     return CallWindowProc (fnOldProc, hWnd, msg,
        wParam, lParam);
}

When the dialog box initializes, the IDC_TEXT1 gets subclassed. Now, how do I handle the key strokes for IDC_TEXT2 - where do I add the subclassing code for the IDC_TEXT2? I have the call back function ready.


0
Comment
Question by:smnaha
3 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 150 total points
ID: 6396433
Hi smnaha,

simply do it at the same place and in the same manner as you do it for IDC_TEXT1, i.e.:

 case WM_INITDIALOG:
   HWND hText1;
   hText1 = GetDlgItem(hWnd, IDC_TEXT1);
   fnOldProc = (WNDPROC) SetWindowLong(hText1, GWL_WNDPROC, (LONG)Text1Proc);
   HWND hText2 = GetDlgItem( hWnd, IDC_TEXT2 );
   fnOldProc2 = (WNDPROC) SetWindowLong( hText2, GWL_WNDPROC, (LONG)Text2Proc);
   return TRUE;
                       
hope that helps,

ZOPPO
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6405361
One way is to have subclassed window proc handle both edit boxes:

WNDPROC afnOldProc[2];
BOOL CALLBACK MyEdProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    int nRsrcID= GetWindowLong( hWnd, GWL_ID );
    WNDPROC fnOldProc;
    if ( nRsrcID == IDC_EDIT1 ) fnOldProc= afnOldProc[0];
    if ( nRsrcID == IDC_EDIT2 ) fnOldProc= afnOldProc[1];

    switch (msg) {
        case WM_CHAR:
            if ( nRsrcID == IDC_EDIT1 ) {
                AfxMessageBox("hi from edit box 1");
            }
            if ( nRsrcID == IDC_EDIT2 ) {
                AfxMessageBox("hi from edit box 2");
            }
        break;
    }
    return CallWindowProc (fnOldProc, hWnd, msg, wParam, lParam );
}

=-=-=-=-=-=-=- in WM_INITDIALOG handler
    HWND hwndEdit;
    hwndEdit= ::GetDlgItem(m_hDlg, IDC_EDIT1);
    afnOldProc[0]= (WNDPROC)SetWindowLong( hwndEdit, GWL_WNDPROC, (LONG)MyEdProc);
    hwndEdit= ::GetDlgItem(m_hDlg, IDC_EDIT2);
    afnOldProc[1]= (WNDPROC)SetWindowLong( hwndEdit, GWL_WNDPROC, (LONG)MyEdProc );

=-=-=-=-=-=-=-
The key to reusing the same subclassing fn is...
    int nRsrcID= GetWindowLong( hWnd, GWL_ID );

That obtains the identifier of the edit control so you can do different things depending upon which edit control is being handled.

-- Dan
0
 
LVL 1

Author Comment

by:smnaha
ID: 6412152
Thanks a lot. Dan Rollins comments were also useful.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 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