Solved

Multiple subclassing in SDK ...

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

19 Experts available now in Live!

Get 1:1 Help Now