Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Multiple subclassing in SDK ...

Posted on 2001-08-16
3
Medium Priority
?
405 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 600 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

885 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