A simple subclass window tool class

Source: Internet
Author: User

In the design and implementation of multi-label View class CTabView: http://www.bkjia.com/kf/201112/113805.html, CTabView inherits from CBasicSubClassWnd private, rewrite its virtual function SubWindowProc, capture WM_DRAWITEM and TTN_GETDISPINFO messages, the implementation of the DrawItem and updatemediltiptext virtual function callback mechanisms, supports custom processing of derived classes, and CBasicSubClassWnd is a subclass window class, the principle is very simple, it is the message processing process for the hook to replace the target window. The design here is to capture messages by multiple CBasicSubClassWnd objects for the same target window, A CBasicSubClassWnd object can only capture messages in one target window. the return value of SubWindowProc determines whether the message is transmitted to the next CBasicSubClassWnd object or the original window for processing. TRUE Indicates that the message is allowed to be transmitted. Otherwise, the message is not allowed. The CBasicSubClassWnd class is implemented based on api + stl, which is easy to use but not complete. For example, it does not consider supporting window capture between different processes and thread security of the class. These things need to be further solved. Let's take a look at its implementation code.

1 // basic_subclasswnd.h
2
3 # ifndef _ BASIC_SUBCLASSWND_H
4 # define _ BASIC_SUBCLASSWND_H
5
6 # include <map>
7 # include <list>
8
9 class CBasicSubClassWnd
10 {
11 friend class CBasicWndInfo;
12
13 public:
14 CBasicSubClassWnd ();
15 void Hook (HWND hWnd );
16 void Unhook ();
17
18 protected:
19 virtual BOOL SubWindowProc (UINT msg, WPARAM wParam, LPARAM lParam );
20
21 private:
22 HWND m_hWnd;
23 };
24
25 class CBasicWndInfo
26 {
27 typedef std: list <CBasicSubClassWnd *> CBasicSubClassWndList;
28 friend class CBasicSubClassWnd;
29
30 private:
31 CBasicWndInfo (HWND hWnd );
32 void Add (CBasicSubClassWnd * pHandler );
33 void Remove (CBasicSubClassWnd * pHandler );
34 void RemoveAll ();
35
36 typedef std: map <HWND, CBasicWndInfo> CBasicSubClassWndMap;
37 typedef CBasicSubClassWndMap: iterator MapIter;
38
39 static CBasicSubClassWndMap & GetHookMap ();
40 static LRESULT HookWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
41
42 CBasicSubClassWndList m_list_scw;
43 WNDPROC m_oldWndProc;
44 HWND m_hWnd;
45 };
46
47 # endif
48
49 // basic_subclasswnd.cpp
50 # include "stdafx. h"
51 # include "basic_subclasswnd.h"
52 using namespace std;
53
54 CBasicSubClassWnd: CBasicSubClassWnd ()
55: m_hWnd (NULL)
56 {
57}
58
59 void CBasicSubClassWnd: Hook (HWND hWnd)
60 {
61 assert (hWnd );
62 if (m_hWnd & m_hWnd! = HWnd)
63 Unhook ();
64 m_hWnd = hWnd;
65
66 CBasicWndInfo: MapIter iter = CBasicWndInfo: GetHookMap (). find (hWnd );
67 if (iter = CBasicWndInfo: GetHookMap (). end ())
68 {
69 iter = CBasicWndInfo: GetHookMap (). insert (make_pair (hWnd, CBasicWndInfo (hWnd). first;
70}
71 iter-> second. Add (this );
72}
73
74 void CBasicSubClassWnd: Unhook ()
75 {
76 assert (m_hWnd );
77
78 CBasicWndInfo: MapIter iter = CBasicWndInfo: GetHookMap (). find (m_hWnd );
79 if (iter = CBasicWndInfo: GetHookMap (). end ())
80 return;
81 iter-> second. Remove (this );
82}
83
84 BOOL CBasicSubClassWnd: SubWindowProc (UINT msg, WPARAM wParam, LPARAM lParam)
85 {
86 return TRUE;
87}
88
89 /////////////////////////////////////// //////////////////////////////////////// ///////////////
90 CBasicWndInfo: CBasicWndInfo (HWND hWnd)
91: m_oldWndProc (NULL)
92, m_hWnd (hWnd)
93 {
94}
95
96 void CBasicWndInfo: Add (CBasicSubClassWnd * pHandler)
97 {
98 if (NULL = m_oldWndProc)
99 {
100 m_oldWndProc = (WNDPROC) SetWindowLong (m_hWnd, GWL_WNDPROC, (LONG) HookWndProc );
101}
102 m_list_sc1_push_back (pHandler );
103}
104
105 void CBasicWndInfo: Remove (CBasicSubClassWnd * pHandler)
106 {
107 m_list_scw.remove (pHandler );
108 if (m_list_sc1_empty ())
109 {
110 assert (m_hWnd );
111 SetWindowLong (m_hWnd, GWL_WNDPROC, (LONG) m_oldWndProc );
112}
113}
114
115 void CBasicWndInfo: RemoveAll ()
116 {
117 m_list_sc1_clear ();
118}
119
120 CBasicWndInfo: CBasicSubClassWndMap & CBasicWndInfo: GetHookMap ()
121 {
122 static CBasicSubClassWndMap s_map;
123 return s_map;
124}
125
126 LRESULT CBasicWndInfo: HookWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
127 {
128 MapIter iter = GetHookMap (). find (hWnd );
129 if (uMsg = WM_NCDESTROY)
130 {
131 // iter-> second. RemoveAll ();
132}
133 else
134 {
135 CBasicSubClassWndList: iterator it;
136 for (it = iter-> second. m_list_sc1_begin (); it! = Iter-> second. m_list_sc1_end (); ++ it)
137 {
138 if (! (* It)-> SubWindowProc (uMsg, wParam, lParam ))
139 return 0;
140}
141}
142 return: CallWindowProc (iter-> second. m_oldWndProc, hWnd, uMsg, wParam, lParam );
143}

From Tiandao reward service

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.