Solved

Multiple subclassing in SDK ...

Posted on 2001-08-16
3
382 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Losing latter half of command line in Visual Studio C++ online program 10 97
Setting nameservers after res_init fails doing res_query 2 138
c++ syntax question 9 66
Outlook 13 100
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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. …
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 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.

734 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