Microsoft KB Archive/145865

= FIX: Cannot Receive WM_HELP for a Subclassed Control =

Article ID: 145865

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft Foundation Class Library 4.2, when used with:
 * Microsoft Visual C++ 4.0 Standard Edition

-



This article was previously published under Q145865



SYMPTOMS
WM_HELP is not received by the parent window of a control that has been subclassed by MFC.



CAUSE
CWnd, the base class of all MFC windows, has a handler for WM_HELP. This handler intercepts the WM_HELP message in the control itself and thus the message will not be sent to the parent window as expected.



RESOLUTION
Subclass all the controls in the CDialog, CFormView, CPropertyPage, or other CWnd derived class that contains the controls which have been subclassed by MFC. The "Sample Code" below iterates through the child windows of a given parent window and checks their WindowProc. If their WindowProc is AfxWndProc (MFC's WindowProc), it subclasses the controls again so that another window procedure is called first. In this WindowProc, we check for WM_HELP and call DefWindowProc instead of passing the message to MFC's AfxWndProc.



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Visual C++ 4.1.



MORE INFORMATION
CWnd::OnHelpInfo (handler for WM_HELP) sends a WM_COMMAND with ID_HELP to the application's main frame window. This message can be intercepted in the main frame, but at this point, the HELPINFO structure has been lost. The main frame will not have any way of determining which help context should be used.



Sample Code
/* Compile options needed: Default

//////////////////////////////////////////////////////////////////////// // FixHelp.h - Include this file in your dialog's or form view's .CPP

void FixHelp(CWnd* pWnd); // Call this from your OnInitDialog for a                         // dialog or OnInitialUpdate for a CFormView

//////////////////////////////////////////////////////////////////////// // FixHelp.cpp - Insert this file into your project


 * 1) include "stdafx.h"

LRESULT CALLBACK ControlFixProc( HWND hwnd, UINT  uMsg, WPARAM wParam,                                                        LPARAM  lParam);

void FixHelp(CWnd* pWnd) {   // search all child windows. If their window proc // is AfxWndProc, then subclass with our window proc CWnd* pWndChild = pWnd->GetWindow(GW_CHILD); while(pWndChild != NULL) {       if (GetWindowLong(pWndChild->GetSafeHwnd, GWL_WNDPROC) == (LONG)AfxWndProc) {           SetWindowLong(pWndChild->GetSafeHwnd, GWL_WNDPROC,                                                (LONG)ControlFixProc); }       pWndChild = pWndChild->GetWindow(GW_HWNDNEXT); } }

LRESULT CALLBACK ControlFixProc(HWND hwnd, UINT  uMsg, WPARAM wParam,                                                        LPARAM  lParam) {   if (uMsg == WM_HELP) {       // bypass MFC's handler, message will be sent to parent return DefWindowProc(hwnd, uMsg, wParam, lParam); }   return AfxWndProc(hwnd,uMsg,wParam,lParam); }

////////////////////////////////////////////////////////////////////////

Additional query words: kbVC400bug 4.00 4.10 SubclassWindow SubclassDlgItem kbSweptVC600

Keywords: kbarchitecture kbbug kbfix kbnoupdate kbuidesign kbvc410fix KB145865

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.