Click here to Skip to main content
Click here to Skip to main content

CHookWnd v1.02

, 3 Mar 2000
Rate this:
Please Sign up or sign in to vote.
A freeware MFC class to support MFC subclassing
  • Download source files - 19 Kb
  • Introduction

    Welcome to CHookWnd, a freeware MFC class to support subclassing of any CWnd instance. The problem it fixes is that you cannot have 2 CWnd instances attached to the one HWND at the same time. This becomes a problem when you want to derive one of your classes from 2 base classes which are both derived from CWnd themselves. An example is, suppose you have 2 classes namely CBitmapMenuFrameWnd, which is derived from CFrameWnd and implements bitmap menus ALA Office 97, and CReBarFrameWnd, which implements command bar menus ALA IE4. When you are implementing your own CMainFrame class you then have the problem that you can only derive from one of these classes and you must copy the source from the other one into your class.

    This class fixes this problem by subclassing the window before MFC gets to it using a plugin method. The code is based in part on a class developed by Paul DiLascia namely "CSubclassWnd" for the MSJ magazine.

    Features
    Usage
    History
    API Reference
    Planned Enhancements
    Contacting the Author


    Features

    • Simple and clean C++ interface. Just derive your plugin class from CHookWnd and override one virtual function to implement your functionality.
    • Multi-thread safe class.
    • Unicode enabled, supports linking to MFC statically and all code compiles cleanly at warning level 4.


    Usage

    • To use the class in your code include hookwnd.cpp in your project and #include hookwnd.h in which ever of your modules needs to make calls to the class.
    • Derive your class from CHookWnd and override the virtual function WindowProc() just like you did when you wrote your first Windows SDK app.
    • Install the plugin class by calling the Hook function in the class you want to subclass.
    • Your code will need to include MFC either statically or dynamically.
    • You will also need to have afxmt.h in your precompiled header. The code will warn you if you haven't this set up.
    • To see the code in action have a look at the sample app which installs 3 hooks, one to report on mouse messages, another to report on keyboard messages and a third one just for good luck.


    History

    V1.0 (25 February 1999)
    • Initial public release.

    V1.01 (29th March 1999)

    • Minor update to remove some unnecessary comments from the code.

    V1.02 (10th May 1999)

    • Minor update to fix a small bug in the demo app. No changes to the actual HookWnd code itself.


    API Reference

    The API consists of the following member functions of the class CHookWnd

    CHookWnd::CHookWnd
    CHookWnd::~CHookWnd
    CHookWnd::Hook
    CHookWnd::UnHook
    CHookWnd::Default
    CHookWnd::WindowProc
    CHookWnd::IsHooked
    CHookWnd::FirstInChain
    CHookWnd::LastInChain
    CHookWnd::MiddleOfChain
    CHookWnd::SizeOfHookChain


    CHookWnd::CHookWnd

    BOOL CHookWnd::CHookWnd();

    Remarks:
    Standard C++ constructor.


    CHookWnd::~CHookWnd

    CHookWnd::~CHookWnd();

    Remarks:
    Standard C++ destructor.


    CHookWnd::Hook

    void CHookWnd::Hook(CWnd* pWnd);

    Parameters:

    • pWnd -- The CWnd instance which you want to subclass.

    Remarks:
    Call this member function to subclass the HWND currently subclassed by the MFC CWnd instance "pWnd". Messages will be routed to the WindowProc method of this instance prior to being passed of to any other installed CHookWnd's in the chain before eventually being routed back to standard MFC message maps..

    See Also:
    CHookWnd::UnHook


    CHookWnd::UnHook

    void CHookWnd::UnHook();

    Remarks:
    Removes this instance from the chain of hooks which are handling the subclassing.

    See Also:
    CHookWnd::Hook


    CHookWnd::Default

    LRESULT CHookWnd::Default();

    Return Value:
    The standard LRESULT which window procedures return. The actual value will depend on the message sent.

    Remarks:
    You can call this function in your derived WindowProc if you want to continue routing of the message. This will pass the message on to any other CHookWnd's in the chain and eventually back to MFC and your message maps.


    CHookWnd::WindowProc

    virtual LRESULT CHookWnd::WindowProc(UINT nMsg, WPARAM wParam, LPARAM lParam);

    Return Value:
    The standard LRESULT which window procedures return. The actual value will depend on the message sent.

    Parameters:

    • nMsg -- Specifies the Windows message to be processed.
    • wParam -- Provides additional information used in processing the message. The parameter value depends on the message.
    • lParam -- Provides additional information used in processing the message. The parameter value depends on the message.

    Remarks:
    This is the function which you should override in your derived class to implement your plugin functionality. This would appear structured very similar to an SDK window proc. For any messages which you do not handle, you should call Default for these.


    CHookWnd::IsHooked

    BOOL CHookWnd::IsHooked() const;

    Return Value:
    TRUE if this instance is currently hooking a CWnd otherwise FALSE.

    Remarks:
    This function is used internally in CHookWnd as an assert in functions where you first should have call Hook.


    CHookWnd::FirstInChain

    BOOL CHookWnd::FirstInChain() const;

    Return Value:
    TRUE if this instance is the first in the chain of CHookWnd's handling subclassing otherwise FALSE.


    CHookWnd::LastInChain

    BOOL CHookWnd::LastInChain() const;

    Return Value:
    TRUE if this instance is the last in the chain of CHookWnd's handling subclassing otherwise FALSE.


    CHookWnd::MiddleOfChain

    BOOL CHookWnd::MiddleOfChain() const;

    Return Value:
    TRUE if this instance is the somewhere in the chain but is not the first or last item in it.


    CHookWnd::SizeOfHookChain

    int CHookWnd::SizeOfHookChain() const;

    Return Value:
    The number of CHookWnd's in the chain which is currently handling the subclassing.



    Planned Enhancements

    • Implement support for installing hooks at the end and middle of the hook chain.
    • Provide a better sample app. At the moment, it's very much a test program which tests all of the functions.
    • If you have any other suggested improvements, please let me know so that I can incorporate them into the next release.


    Contacting the Author

    PJ Naughter
    Email: pjn@indigo.ie
    Web: http://www.naughter.com
    10th May 1999

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    PJ Naughter

    United States United States
    No Biography provided

    Comments and Discussions

     
    GeneralHooking PinsussAnonymous19-Jun-00 4:54 
    Generalrc=5 (Access Denied) when attempting hook of IExplorer Window PinsussMark Findlay20-May-00 8:27 
    GeneralAlso available in MSJ! PinsussShanker16-Mar-00 14:05 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Mobile
    Web02 | 2.8.140721.1 | Last Updated 4 Mar 2000
    Article Copyright 2000 by PJ Naughter
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid