Create your own controls - the art of subclassing
By Chris Maunder
An introduction to subclassing the Windows common controls using MFC
Introduction
程序員們可以用許多windows通用控件提供的功能方便的編程,這些控件從list到button甚至是進(jìn)度條都是可以直接拿來用的.即便如此,仍然有時(shí)候你所選擇的標(biāo)準(zhǔn)控件并不夠用.歡迎學(xué)習(xí)子類化控件這個(gè)經(jīng)典技法.
子類化一個(gè)窗體控件同子類化一個(gè)C++類并不一樣.子類化一個(gè)控件意味著你要替換這個(gè)窗口的某些默認(rèn)消息處理方法(message handlers),子類化能讓你可以高效的劫持這個(gè)控件讓它按照你要求的行為工作,而不是默認(rèn)Windows的默認(rèn)的行為.子類化幾乎允許你把控件做成你想的那么完美.有兩種子類化類型.實(shí)例對(duì)象實(shí)例化(instance subclassing)和全局實(shí)例化(global subclassing).實(shí)例對(duì)象子類化是當(dāng)你將一個(gè)單個(gè)的窗口實(shí)例作為子類.全局子類化是將某一種某一窗口類型的(CLASSWND吧,我吃不準(zhǔn))窗口(控件)做成自定義版本.這里只講下前者,實(shí)例對(duì)象實(shí)例化..
很重要的一點(diǎn)要了解繼承自CWnd
的對(duì)象同窗口本身(一個(gè)HWND
)的區(qū)別.你的CWnd
繼承類對(duì)象包含一個(gè)成員變量指向HWND
,并且包含HWND
處理消息(eg WM_PAINT
, WM_MOUSEMOVE
)用到的處理調(diào)用函數(shù)?(message pump calls吃不準(zhǔn)這個(gè))
子類化是簡(jiǎn)單的.首先你建立一個(gè)能夠處理你關(guān)注消息的窗口消息類,接著將你想要子類化的存在的窗體的行為用你指定的窗口消息類替換.之后這個(gè)窗口就很神奇了..下面就是一個(gè)對(duì)Button
的子類化演示.
A New Class
為了子類化控件,我們要新建一個(gè)消息處理類,分別處理我們有興趣的所有消息,我們都很懶,所以就盡量少處理幾個(gè)消息,就處理我們常用的幾個(gè)就好,最好的建立消息處理類的方法是直接繼承存在的類 CButton
,這個(gè)類是Button
的默認(rèn)消息處理類.
讓我們假定我們要做寫古怪的事,比如在每次鼠標(biāo)落在按鈕時(shí)后讓按鈕變成亮黃色.第一件事用ClassWizard建立CButton
繼承類CMyButton
.

在MFC里繼承CButton
有很多好處,最大好處就是不用再寫多一行來實(shí)現(xiàn)原有的默認(rèn)消息處理.如果我們?cè)敢馕覀兛梢灾苯舆M(jìn)行下一步,用這個(gè)CMyButton
子類化一個(gè)按鈕實(shí)例,這個(gè)按鈕實(shí)例已經(jīng)是一個(gè)功能完善的控件了,只是有點(diǎn)無趣(廢話),button control 而已.因?yàn)?/span>MFC實(shí)現(xiàn)了所有的默認(rèn)消息處理,所以我們可以僅僅重載幾個(gè)我們有興趣的消息處理,忽略其余的.
然而在這個(gè)例子中我們還是將其做成自己喜歡的怪異按鈕吧.
判斷鼠標(biāo)是否在按鈕上需要一個(gè)bool類型變量m_bOverControl. TRUE代表鼠標(biāo)在按鈕上,這個(gè)檢查是定期的(使用定時(shí)器timer).不幸的是對(duì)于我們來說沒有OnMouseEnter
和 OnMouseLeave
函數(shù)能夠跨越平臺(tái)使用(?不解?can be used across platforms),所以我們使用OnMouseMove
做這件事. 當(dāng)計(jì)時(shí)器激發(fā)時(shí),我們發(fā)現(xiàn)鼠標(biāo)不在按鈕上時(shí)我們讓計(jì)時(shí)器失效,重繪空間.
使用ClassWizard添加WM_MOUSEMOVE 和WM_TIMER消息處理,他們分別對(duì)應(yīng) OnMouseMove
和 OnTimer
.

ClassWizard會(huì)添加如下代碼:
BEGIN_MESSAGE_MAP(CMyButton, CButton)
//{{AFX_MSG_MAP(CMyButton)
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyButton message handlers
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CButton::OnMouseMove(nFlags, point);
}
void CMyButton::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CButton::OnTimer(nIDEvent);
}
消息表內(nèi)容(在 BEGIN_MESSAGE_MAP
段中) 將消息與函數(shù)一一映射. ON_WM_MOUSEMOVE
映射了 WM_MOUSEMOVE消息 => OnMouseMove(..)
, ON_WM_TIMER
映射 WM_TIMER消息 => OnTimer(..)
. 這兩個(gè)宏在MFC里很常用,有興趣可以看看.
假設(shè)我們定義了兩個(gè)變量BOOL m_bOverControl , UINT m_nTimerID, 在構(gòu)造函數(shù)初始化好,我們的消息處理如下:
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
if (!m_bOverControl) // Cursor has just moved over control
{
TRACE0("Entering control\n");
m_bOverControl = TRUE; // Set flag telling us the mouse is in
Invalidate(); // Force a redraw
SetTimer(m_nTimerID, 100, NULL); // Keep checking back every 1/10 sec
}
CButton::OnMouseMove(nFlags, point); // drop through to default handler
}
void CMyButton::OnTimer(UINT nIDEvent)
{
// Where is the mouse?
CPoint p(GetMessagePos());
ScreenToClient(&p);
// Get the bounds of the control (just the client area)
CRect rect;
GetClientRect(rect);
// Check the mouse is inside the control
if (!rect.PtInRect(p))
{
TRACE0("Leaving control\n");
// if not then stop looking...
m_bOverControl = FALSE;
KillTimer(m_nTimerID);
// ...and redraw the control
Invalidate();
}
// drop through to default handler
CButton::OnTimer(nIDEvent);
}
我們的工作最后一塊就是繪制,繪制不需處理消息,而是重載CWnd::DrawItem這個(gè)虛函數(shù).只有自繪控件才會(huì)調(diào)用這個(gè)函數(shù),它沒有默認(rèn)的實(shí)現(xiàn)(it ASSERT's if you try).這個(gè)函數(shù)只用來對(duì)于控件類的重載.

使用ClassWizard添加DrawItem重載,修改函數(shù)如下:
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rect = lpDrawItemStruct->rcItem;
UINT state = lpDrawItemStruct->itemState;
CString strText;
GetWindowText(strText);
// draw the control edges (DrawFrameControl is handy!)
if (state & ODS_SELECTED)
pDC->DrawFrameControl(rect, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_PUSHED);
else
pDC->DrawFrameControl(rect, DFC_BUTTON, DFCS_BUTTONPUSH);
// Deflate the drawing rect by the size of the button's edges
rect.DeflateRect( CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE)));
// Fill the interior color if necessary
if (m_bOverControl)
pDC->FillSolidRect(rect, RGB(255, 255, 0)); // yellow
// Draw the text
if (!strText.IsEmpty())
{
CSize Extent = pDC->GetTextExtent(strText);
CPoint pt( rect.CenterPoint().x - Extent.cx/2,
rect.CenterPoint().y - Extent.cy/2 );
if (state & ODS_SELECTED)
pt.Offset(1,1);
int nMode = pDC->SetBkMode(TRANSPARENT);
if (state & ODS_DISABLED)
pDC->DrawState(pt, Extent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
else
pDC->TextOut(pt.x, pt.y, strText);
pDC->SetBkMode(nMode);
}
}
這個(gè)類的各部分就差不多講完了,但是這里要注意一點(diǎn)點(diǎn). DrawItem
函數(shù)需要控件設(shè)置自繪屬性,在資源編輯界面為控件填上自繪屬性.不過還有一種更好的方法讓這個(gè)子類自動(dòng)設(shè)置它的窗體風(fēng)格,打開自繪屬性.要這樣做我們需要重載這最后的函數(shù)PreSubclassWindow
.
這個(gè)函數(shù)叫做SubclassWindow
, 當(dāng)調(diào)用CWnd::Create
和DDX_Control
后都會(huì)被調(diào)用,也就是說如果你建立一個(gè)類的實(shí)例,無論是動(dòng)態(tài)建立還是用對(duì)話框模板,PreSubclassWindow
都會(huì)被調(diào)用. PreSubclassWindow
將會(huì)在你要子類化的窗口建立后,在這個(gè)窗口顯示之前調(diào)用.也就是說它是需要對(duì)窗體表現(xiàn)進(jìn)行初始化的一個(gè)最適合的位置.
非常重要的一點(diǎn):如果你使用對(duì)話框資源新建了一個(gè)控件,那么你子類化的控件將不會(huì)得到WM_CREATE 消息, 因此我們不能使用 OnCreate
來初始化控件, 因?yàn)樗袝r(shí)候不會(huì)被調(diào)用(譯注:但是SubclassWindow
都可以).
使用ClassWizard重載PreSubclassWindow
添加如下代碼:
void CMyButton::PreSubclassWindow()
{
CButton::PreSubclassWindow();
ModifyStyle(0, BS_OWNERDRAW); // make the button owner drawn
}
恭喜你 – 你現(xiàn)在有了CMybutton
這個(gè)繼承類!
子類化
使用DDX 在創(chuàng)建過程中子類化一個(gè)窗體
在這個(gè)例子中,我在Demo對(duì)話框放上了一個(gè)按鈕控件

我讓默認(rèn)的對(duì)話框創(chuàng)建過程創(chuàng)建這個(gè)控件,然后使用DDX_...
將控件關(guān)聯(lián)于自定義的類. 這種方法可以用ClassWizard簡(jiǎn)單的實(shí)現(xiàn):添加一個(gè)成員變量,連接對(duì)應(yīng)的控件ID(這里是IDC_BUTTON
),設(shè)定為控件類型變量(Control type),class name為CMyButton
我們自定義的類.

ClassWizard添加了一個(gè)DDX_Control
調(diào)用在對(duì)話框的
DoDataExchange
函數(shù)里. DDX_Control
調(diào)用SubclassWindow
函數(shù)將
CMyButton
與控件關(guān)聯(lián)
,
于是有了自定義消息處理的效果
.這個(gè)按鈕算是已經(jīng)被劫持了,現(xiàn)在它的行為就跟我們想要的一樣了.
使用ClassWizard子類化窗體使用一個(gè)不識(shí)別的類
如果你在工程中添加了一個(gè)窗體類并且想要子類化一個(gè)窗體,但是ClassWizard添加變量時(shí)里面沒有顯示新的類型,你需要重新build Classwizard 文件. (這個(gè)是VC6BUG吧)
備份.clw,刪除原來的文件,回去原來的界面Ctrl+W.添加工程需要包括的文件,這樣應(yīng)該就行了 不行的話只有將控件關(guān)聯(lián)的基類名稱替換成自定義類名稱了..
子類化一個(gè)存在的窗體
使用DDX 是簡(jiǎn)單的,但是如果我們需要子類化一個(gè)已經(jīng)存在的空間就沒有用了. 比如,如果你想要子類化一個(gè)在combobox中的Edit控件.你需要在你能子類化Edit窗口之前先擁有combobox類(combobox包含一個(gè)Edit控件)
在這種情況下你需要用到 SubclassDlgItem
或 SubclassWindow
函數(shù).這兩個(gè)函數(shù)允許你動(dòng)態(tài)子類化窗體,換句話說,將已存在的某窗體關(guān)聯(lián)于你的自定義窗體類.
舉例來說,假設(shè)我們有一個(gè)對(duì)話框包括一個(gè)按鈕IDC_BUTTON1.
這個(gè)按鈕已經(jīng)創(chuàng)建了.我們想把它關(guān)聯(lián)于CMyButton
,讓它表現(xiàn)出我們想要的行為.
要這樣做,我們需要有一個(gè)新類型的對(duì)象,最好有一個(gè)對(duì)話框或視圖的成員函數(shù).
CMyButton m_btnMyButton;
接著在你的 OnInitDialog
(或其它適合的初始化函數(shù)) 中調(diào)用
m_btnMyButton.SubclassDlgItem(IDC_BUTTON1, this);
如果你已經(jīng)有了你想要子類化的窗體的指針,或者你在動(dòng)態(tài)創(chuàng)建控件的CView或其他CWnd子類下使用子類化,或者你不想用SubclassDlgItem
,你可以簡(jiǎn)單的調(diào)用:
CWnd* pWnd = GetDlgItem(IDC_BUTTON1); // or use some other method to get
// a pointer to the window you wish
// to subclass
ASSERT( pWnd && pWnd->GetSafeHwnd() );
m_btnMyButton.SubclassWindow(pWnd->GetSafeHwnd());
這個(gè)按鈕繪制很簡(jiǎn)單, 但是也很經(jīng)典了..是個(gè)基礎(chǔ)吧.程序編譯運(yùn)行后就是如圖:

注意我們只是重載了繪制函數(shù),使用獲取鼠標(biāo)狀態(tài)函數(shù).這意味著你的控件仍然是一個(gè)按鈕..為你的對(duì)話框類添加一個(gè)click的消息處理,你能看到它一樣會(huì)被調(diào)用.
gohan 2008.1.24
23:59
Conclusion
Subclassing is not hard - you just need to choose the class you wish to subclass carefully, and be aware of what messages you need to handle. Read up on the control you are subclassing - learn about the messages it handles and also the virtual member functions of its implementation class. Once you've hooked into a control and taken over it's inner workings the sky's the limit.
History
26 Oct 2001 - added info in SubclassWindow and SubclassDlgItem
License
This article is licensed under The Code Project Open License (CPOL)
About the Author
Chris Maunder

Sitebuilder, Editor, Staff, Admin
|
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs CodeProject. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.
His programming experience includes C/C++, C#, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.
He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.
Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.
Occupation:
|
Founder
|
Company:
|
The Code Project
|
Location:
|
Canada
|
|