MFC 界面控件自動大小伸縮
C++ Builder 開發WINDOWS界面非常省時,不僅僅是提供的界面控件元素多,而且界面元素控件有一項自動伸縮和固定控件邊界在某個位置上的屬性。MFC提供的界面元素控件時沒有該屬性的,如果需要,必須得手動加上一堆代碼纔可以實現,非常繁瑣。對入門的開發人員來說這一點很頭疼,其實本人也很頭疼這一點,所以花了點小時間封裝了一些代碼來解決該問題,主要還是節省開發時間。
該類的使用起來比較簡單,最需要在生成一個對話框類的時候把繼承的類CDialog 修改爲 CAutoSizeWnd<CDialog>,當然還有其他一些相應的CDialog名稱的部分修改一下即可。之後再在OnCreate成員函數(響應WM_CREATE事件)內添加代碼: AddChildControl(控件指針, 固定的邊界枚舉),每一個控件加一條語句即可。這樣就已經可以實現上述的功能了。
該模板類最主要的函數是:
另外該類也只是一個初始品,所以現在只能支持CDialog,需要修改後才能支持CView等其他作爲窗體的界面元素。
接下是完整代碼:例子代碼
Code
1#ifndef CTEST_AUTO_DLG_H
2#define CTEST_AUTO_DLG_H
3
4#include "stdafx.h"
5#include <map>
6
7using namespace std;
8
9namespace EFixedType
10{
11 enum EFixedType
12 {
13 NONE = 0,
14 TOP = 1,
15 BOOTOM = 2,
16 LEFT = 4,
17 RIGHT = 8
18 };
19}
20
21template<class T>
22class CAutoSizeWnd : public T
23{
24public:
25
26 struct TFixed;
27
28 CAutoSizeWnd(UINT nIDTemplate, CWnd *pCWnd) : T(nIDTemplate, pCWnd)
29 {
30 m_fInitDlg = FALSE;
31 }
32
33 virtual ~CAutoSizeWnd()
34 {
35
36 }
37
38 //功能:該函數必須在 OnCreate 中調用
39 //參數:
40 // CWnd *pCWnd IN 窗口指針,不允許爲NULL
41 // DWORD dwType IN 固定的邊框,內容爲 EFixedType::NONE
42 // EFixedType::TOP
43 // EFixedType::BOOTOM
44 // EFixedType::LEFT
45 // EFixedType::RIGHT
46 // 中的一個或多個,多個用 '|' 號連接
47 //返回值:添加是否成功
48 BOOL AddChildControl(CWnd *pCWnd, DWORD dwType)
49 {
50 if (!pCWnd)
51 {
52 return FALSE;
53 }
54
55 TFixed *pTFixed = new TFixed();
56
57 if (!pTFixed)
58 {
59 return FALSE;
60 }
61
62 if (dwType != EFixedType::NONE)
63 {
64 if (dwType & EFixedType::TOP)
65 {
66 pTFixed->fTop = TRUE;
67 }
68
69 if (dwType & EFixedType::BOOTOM)
70 {
71 pTFixed->fBottom = TRUE;
72 }
73
74 if (dwType & EFixedType::LEFT)
75 {
76 pTFixed->fLeft = TRUE;
77 }
78
79 if (dwType & EFixedType::RIGHT)
80 {
81 pTFixed->fRight = TRUE;
82 }
83 }
84
85 m_CWndCollections.insert( CWndCollections::value_type(pCWnd, pTFixed));
86
87 return TRUE;
88 }
89
90protected:
91
92 BOOL OnInitDialog()
93 {
94 T::OnInitDialog();
95
96 CWndCollections::iterator entity = m_CWndCollections.begin();
97 CWndCollections::iterator endEntity = m_CWndCollections.end();
98
99 RECT rect;
100
101 TFixed *pTFixed = NULL;
102
103 CRect dlgRect;
104
105 this->GetClientRect(&dlgRect);
106
107 for (; entity != endEntity; ++entity)
108 {
109 pTFixed = entity->second;
110
111 entity->first->GetWindowRect(&rect);
112
113 this->ScreenToClient(&rect);
114
115 pTFixed->iTop = rect.top;
116
117 pTFixed->iBottom = dlgRect.Height() - rect.bottom;
118
119 pTFixed->iLeft = rect.left;
120
121 pTFixed->iRight = dlgRect.Width() - rect.right;
122 }
123
124 return TRUE;
125 }
126
127 virtual void OnSize(UINT nType, int cx, int cy)
128 {
129 T::OnSize(nType, cx, cy);
130
131 if (m_fInitDlg == FALSE)
132 {
133 m_fInitDlg = TRUE;
134
135 return;
136 }
137
138 //處理所有控件的顯示大小
139
140 CWndCollections::iterator entity = m_CWndCollections.begin();
141 CWndCollections::iterator endEntity = m_CWndCollections.end();
142
143 for (; entity != endEntity; ++entity)
144 {
145 AutoSize(entity->first, entity->second);
146 }
147 }
148
149 void AutoSize(CWnd *pCWnd, TFixed *pTFixed)
150 {
151 if (!pCWnd || !pTFixed)
152 {
153 return;
154 }
155
156 CRect dlgRect;
157
158 this->GetClientRect(&dlgRect);
159
160 RECT rect;
161
162 pCWnd->GetWindowRect(&rect);
163
164 this->ScreenToClient(&rect);
165
166 int width = rect.right - rect.left;
167
168 int heght = rect.bottom - rect.top;
169
170 if (pTFixed->fTop && pTFixed->fBottom)
171 {
172 rect.top = pTFixed->iTop;
173
174 rect.bottom = dlgRect.Height() - pTFixed->iBottom;
175 }
176 else if (pTFixed->fTop)
177 {
178
179 }
180 else if (pTFixed->fBottom)
181 {
182 rect.top = dlgRect.Height() - heght - pTFixed->iBottom;
183
184 rect.bottom = dlgRect.Height() - pTFixed->iBottom;
185 }
186
187 if (pTFixed->fLeft && pTFixed->fRight)
188 {
189 rect.left = pTFixed->iLeft;
190 rect.right = dlgRect.Width() - pTFixed->iRight;
191 }
192 else if (pTFixed->fLeft)
193 {
194
195 }
196 else if (pTFixed->fRight)
197 {
198 rect.left = dlgRect.Width() - width - pTFixed->iRight;
199
200 rect.right = dlgRect.Width() - pTFixed->iRight;
201 }
202
203 pCWnd->MoveWindow(&rect);
204 }
205
206
207private:
208
209 struct TFixed
210 {
211 TFixed()
212 {
213 fTop = FALSE;
214 fBottom = FALSE;
215 fLeft = FALSE;
216 fRight = FALSE;
217 iTop = 0;
218 iBottom = 0;
219 iLeft = 0;
220 iRight = 0;
221 }
222
223 BOOL fTop;//是否固定與父窗口上邊界的距離
224 BOOL fBottom;//是否固定與父窗口下邊界的距離
225 BOOL fLeft;//是否固定與父窗口左邊界的距離
226 BOOL fRight;//是否固定與父窗口右邊界的距離
227 int iTop;//與父窗口上邊界的距離
228 int iBottom;//與父窗口下邊界的距離
229 int iLeft;//與父窗口左邊界的距離
230 int iRight;//與父窗口右邊界的距離
231 };
232
233 typedef map<CWnd *, TFixed *> CWndCollections;
234
235 CWndCollections m_CWndCollections;
236
237 BOOL m_fInitDlg;
238};
239
240#endif
1#ifndef CTEST_AUTO_DLG_H
2#define CTEST_AUTO_DLG_H
3
4#include "stdafx.h"
5#include <map>
6
7using namespace std;
8
9namespace EFixedType
10{
11 enum EFixedType
12 {
13 NONE = 0,
14 TOP = 1,
15 BOOTOM = 2,
16 LEFT = 4,
17 RIGHT = 8
18 };
19}
20
21template<class T>
22class CAutoSizeWnd : public T
23{
24public:
25
26 struct TFixed;
27
28 CAutoSizeWnd(UINT nIDTemplate, CWnd *pCWnd) : T(nIDTemplate, pCWnd)
29 {
30 m_fInitDlg = FALSE;
31 }
32
33 virtual ~CAutoSizeWnd()
34 {
35
36 }
37
38 //功能:該函數必須在 OnCreate 中調用
39 //參數:
40 // CWnd *pCWnd IN 窗口指針,不允許爲NULL
41 // DWORD dwType IN 固定的邊框,內容爲 EFixedType::NONE
42 // EFixedType::TOP
43 // EFixedType::BOOTOM
44 // EFixedType::LEFT
45 // EFixedType::RIGHT
46 // 中的一個或多個,多個用 '|' 號連接
47 //返回值:添加是否成功
48 BOOL AddChildControl(CWnd *pCWnd, DWORD dwType)
49 {
50 if (!pCWnd)
51 {
52 return FALSE;
53 }
54
55 TFixed *pTFixed = new TFixed();
56
57 if (!pTFixed)
58 {
59 return FALSE;
60 }
61
62 if (dwType != EFixedType::NONE)
63 {
64 if (dwType & EFixedType::TOP)
65 {
66 pTFixed->fTop = TRUE;
67 }
68
69 if (dwType & EFixedType::BOOTOM)
70 {
71 pTFixed->fBottom = TRUE;
72 }
73
74 if (dwType & EFixedType::LEFT)
75 {
76 pTFixed->fLeft = TRUE;
77 }
78
79 if (dwType & EFixedType::RIGHT)
80 {
81 pTFixed->fRight = TRUE;
82 }
83 }
84
85 m_CWndCollections.insert( CWndCollections::value_type(pCWnd, pTFixed));
86
87 return TRUE;
88 }
89
90protected:
91
92 BOOL OnInitDialog()
93 {
94 T::OnInitDialog();
95
96 CWndCollections::iterator entity = m_CWndCollections.begin();
97 CWndCollections::iterator endEntity = m_CWndCollections.end();
98
99 RECT rect;
100
101 TFixed *pTFixed = NULL;
102
103 CRect dlgRect;
104
105 this->GetClientRect(&dlgRect);
106
107 for (; entity != endEntity; ++entity)
108 {
109 pTFixed = entity->second;
110
111 entity->first->GetWindowRect(&rect);
112
113 this->ScreenToClient(&rect);
114
115 pTFixed->iTop = rect.top;
116
117 pTFixed->iBottom = dlgRect.Height() - rect.bottom;
118
119 pTFixed->iLeft = rect.left;
120
121 pTFixed->iRight = dlgRect.Width() - rect.right;
122 }
123
124 return TRUE;
125 }
126
127 virtual void OnSize(UINT nType, int cx, int cy)
128 {
129 T::OnSize(nType, cx, cy);
130
131 if (m_fInitDlg == FALSE)
132 {
133 m_fInitDlg = TRUE;
134
135 return;
136 }
137
138 //處理所有控件的顯示大小
139
140 CWndCollections::iterator entity = m_CWndCollections.begin();
141 CWndCollections::iterator endEntity = m_CWndCollections.end();
142
143 for (; entity != endEntity; ++entity)
144 {
145 AutoSize(entity->first, entity->second);
146 }
147 }
148
149 void AutoSize(CWnd *pCWnd, TFixed *pTFixed)
150 {
151 if (!pCWnd || !pTFixed)
152 {
153 return;
154 }
155
156 CRect dlgRect;
157
158 this->GetClientRect(&dlgRect);
159
160 RECT rect;
161
162 pCWnd->GetWindowRect(&rect);
163
164 this->ScreenToClient(&rect);
165
166 int width = rect.right - rect.left;
167
168 int heght = rect.bottom - rect.top;
169
170 if (pTFixed->fTop && pTFixed->fBottom)
171 {
172 rect.top = pTFixed->iTop;
173
174 rect.bottom = dlgRect.Height() - pTFixed->iBottom;
175 }
176 else if (pTFixed->fTop)
177 {
178
179 }
180 else if (pTFixed->fBottom)
181 {
182 rect.top = dlgRect.Height() - heght - pTFixed->iBottom;
183
184 rect.bottom = dlgRect.Height() - pTFixed->iBottom;
185 }
186
187 if (pTFixed->fLeft && pTFixed->fRight)
188 {
189 rect.left = pTFixed->iLeft;
190 rect.right = dlgRect.Width() - pTFixed->iRight;
191 }
192 else if (pTFixed->fLeft)
193 {
194
195 }
196 else if (pTFixed->fRight)
197 {
198 rect.left = dlgRect.Width() - width - pTFixed->iRight;
199
200 rect.right = dlgRect.Width() - pTFixed->iRight;
201 }
202
203 pCWnd->MoveWindow(&rect);
204 }
205
206
207private:
208
209 struct TFixed
210 {
211 TFixed()
212 {
213 fTop = FALSE;
214 fBottom = FALSE;
215 fLeft = FALSE;
216 fRight = FALSE;
217 iTop = 0;
218 iBottom = 0;
219 iLeft = 0;
220 iRight = 0;
221 }
222
223 BOOL fTop;//是否固定與父窗口上邊界的距離
224 BOOL fBottom;//是否固定與父窗口下邊界的距離
225 BOOL fLeft;//是否固定與父窗口左邊界的距離
226 BOOL fRight;//是否固定與父窗口右邊界的距離
227 int iTop;//與父窗口上邊界的距離
228 int iBottom;//與父窗口下邊界的距離
229 int iLeft;//與父窗口左邊界的距離
230 int iRight;//與父窗口右邊界的距離
231 };
232
233 typedef map<CWnd *, TFixed *> CWndCollections;
234
235 CWndCollections m_CWndCollections;
236
237 BOOL m_fInitDlg;
238};
239
240#endif
該類的使用起來比較簡單,最需要在生成一個對話框類的時候把繼承的類CDialog 修改爲 CAutoSizeWnd<CDialog>,當然還有其他一些相應的CDialog名稱的部分修改一下即可。之後再在OnCreate成員函數(響應WM_CREATE事件)內添加代碼: AddChildControl(控件指針, 固定的邊界枚舉),每一個控件加一條語句即可。這樣就已經可以實現上述的功能了。
該模板類最主要的函數是:
Code
1 //功能:該函數必須在 OnCreate 中調用
2 //參數:
3 // CWnd *pCWnd IN 窗口指針,不允許爲NULL
4 // DWORD dwType IN 固定的邊框,內容爲 EFixedType::NONE
5 // EFixedType::TOP
6 // EFixedType::BOOTOM
7 // EFixedType::LEFT
8 // EFixedType::RIGHT
9 // 中的一個或多個,多個用 '|' 號連接
10 //返回值:添加是否成功
11 BOOL AddChildControl(CWnd *pCWnd, DWORD dwType);
1 //功能:該函數必須在 OnCreate 中調用
2 //參數:
3 // CWnd *pCWnd IN 窗口指針,不允許爲NULL
4 // DWORD dwType IN 固定的邊框,內容爲 EFixedType::NONE
5 // EFixedType::TOP
6 // EFixedType::BOOTOM
7 // EFixedType::LEFT
8 // EFixedType::RIGHT
9 // 中的一個或多個,多個用 '|' 號連接
10 //返回值:添加是否成功
11 BOOL AddChildControl(CWnd *pCWnd, DWORD dwType);
另外該類也只是一個初始品,所以現在只能支持CDialog,需要修改後才能支持CView等其他作爲窗體的界面元素。
接下是完整代碼:例子代碼