一、簡介
有時我們需要在非客戶區(例如:標題欄)添加按鈕,並對按鈕作出響應,如下圖所示:
為此我封裝了兩個類CNCButton和CNCButtonManager,CNCButton主要用於按鈕繪制,CNCButtonManager用於按鈕的管理並對非客戶區的消息進行處理。如下是幾個主要的非客戶區消息:
WM_NCHITTEST
WM_NCACTIVATE
WM_NCPAINT
二、使用
2.1 首先我們需要引用兩個類的頭文件
#include "NCButtonManager.h"
#include "NCButton.h"
2.2 為按鈕定義ID
#define ID_BUTTON1 100
#define ID_BUTTON2 101
#define ID_BUTTON3 102
2.3 添加按鈕和管理類成員變量
// Implementation
public:
CNCButton m_NcButton1;
CNCButton m_NcButton2;
CNCButton m_NcButton3;
CNCButtonManager m_NcButtonManager;
2.4 創建按鈕
// 按鈕的位置和尺寸
POINT ptBtn1Offset = {120, 10};
POINT ptBtn2Offset = {20, 32};
POINT ptBtn3Offset = {190, 10};
SIZE sizeBtn1 = {60, 14};
SIZE sizeBtn2 = {16, 14};
SIZE sizeBtn3 = {16, 14};
// 創建按鈕
m_NcButton1.Create("", this->m_hWnd, ptBtn1Offset, sizeBtn1, ID_BUTTON1);
m_NcButton2.Create("", this->m_hWnd, ptBtn2Offset, sizeBtn2, ID_BUTTON2);
m_NcButton3.Create("", this->m_hWnd, ptBtn3Offset, sizeBtn3, ID_BUTTON3);
// 設置文字、提示、按鈕位圖
m_NcButton1.SetText("VC知識庫", "宋體", 12);
m_NcButton1.SetTooltip("VC知識庫歡迎您!");
m_NcButton2.SetButtonBitmap(IDB_PEN1, BTNBMP_NORMAL); // 正常位圖
m_NcButton2.SetButtonBitmap(IDB_PEN2, BTNBMP_MOUSEOVER); // 鼠標移到上面時的位圖
m_NcButton2.SetTooltip("pen");
m_NcButton3.SetButtonBitmap(IDB_PEN1, BTNBMP_NORMAL);
m_NcButton3.SetButtonBitmap(IDB_PEN2, BTNBMP_MOUSEOVER);
m_NcButton3.SetTooltip("pen");
// 添加到管理類
m_NcButtonManager.AddButton(&m_NcButton1);
m_NcButtonManager.AddButton(&m_NcButton2);
m_NcButtonManager.AddButton(&m_NcButton3);
2.5 消息的處理
2.5.1 將消息轉發給管理類
LRESULT CMainFrame::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT lResult = CFrameWnd::DefWindowProc(message, wParam, lParam);
if(!IsWindow(this->m_hWnd))
return lResult;
LRESULT lMyResult = m_NcButtonManager.DefWindowProc(this->m_hWnd, message, wParam, lParam);
if(lMyResult != 0)
return lMyResult;
else
return lResult;
}
2.5.2 添加消息處理函數
// Operations
public:
void OnNcButton1Clicked();
void CMainFrame::OnNcButton1Clicked()
{
AfxMessageBox("VC知識庫歡迎您! www.vckbase.com");
}
2.5.3 消息映射
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_BN_CLICKED(ID_BUTTON1, OnNcButton1Clicked)
ON_BN_CLICKED(ID_BUTTON2, OnNcButton2Clicked)
ON_BN_CLICKED(ID_BUTTON3, OnNcButton3Clicked)
ON_WM_CREATE()
/
本文配套源碼