Click here to Skip to main content
15,893,904 members
Articles / Desktop Programming / MFC

Windows subclassing and hooking with C++ classes

Rate me:
Please Sign up or sign in to vote.
4.38/5 (24 votes)
9 Oct 2003CPOL10 min read 151.2K   3.8K   59  
This article put forwards a proposal for structured window subclassing and hooking
#pragma once

#include "Smartptr.h"

namespace GE_{namespace Utils{

	//provide a way to sublass a window.
	//override the virtual OnWndProcSub to process messages
	
	//NOTE: the object must be always on heap
	// to keep track of message recursion. 

	//ex. if the sublaccer is associateed to a CFrameWnd or CView,
	// WM_NCDESTROY will cause the deletion of the CFrameWnd or CView.
	//	howver, there are other message on the stack (for ex. WM_SYSCOMMAND) that still need to be processed 

	/// alwaus allocate on the heap and manage with strong pointers

	class CWndSubclasser;
	typedef Safe::PtrStrong<CWndSubclasser> PWndSubclasser;

	class CWndSubclasser: public Safe::ObjStrong
	{
	private:
		LPVOID _token;
		bool _bCleared;
	protected:
		HWND _hWnd; //referred window
	public:
		CWndSubclasser();
		virtual ~CWndSubclasser(void);
		bool Subclass(HWND hWnd);
		bool Clear();
		static LRESULT Call(LPVOID token);
		LRESULT Default();
		LRESULT Default(WPARAM wParam, LPARAM lParam);
		CWnd* GetWnd() {return CWnd::FromHandle(_hWnd);}
	protected:
		virtual LRESULT WndProcSub(UINT uMsg, WPARAM wParam, LPARAM lParam);
	};


}}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Italy Italy
Born and living in Milan (Italy), I'm an engineer in electronics actually working in the ICT department of an important oil/gas & energy company as responsible for planning and engineering of ICT infrastructures.
Interested in programming since the '70s, today I still define architectures for the ICT, deploying dedicated specific client application for engineering purposes, working with C++, MFC, STL, and recently also C# and D.

Comments and Discussions