Solved

Multiple subclassing in SDK ...

Posted on 2001-08-16
3
366 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

911 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

17 Experts available now in Live!

Get 1:1 Help Now