介紹函數(shù):
void AFXAPI DDX_Control( CDataExchange* pDX, int nIDC, CWnd& rControl );
參數(shù)一:指向CDataExchange的指針,用于建立資料交換設(shè)備上下文;
參數(shù)二:控件IDC;
參數(shù)三:控件變量。
重寫SetIcon()函數(shù):
頭文件:
void SetIcon(int nIconInId, int nIconOutId = NULL); //帶參數(shù)的成員函數(shù)
源文件:
void CButtonST::SetIcon(int nIconInId, int nIconOutId)
{
HICON hIconIn; //定義HICON 變量,用于保存圖片
HICON hIconOut;
HINSTANCE hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconInId), RT_GROUP_ICON);
//鼠標(biāo)點(diǎn)擊時(shí)獲得應(yīng)用資源句柄
hIconIn = (HICON)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nIconInId), IMAGE_ICON, 0, 0, 0);
// 鼠標(biāo)移開時(shí)加載圖片
hIconOut = (nIconOutId == NULL) ? NULL : (HICON)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nIconOutId), IMAGE_ICON, 0, 0, 0);
SetIcon(hIconIn, hIconOut); //調(diào)用
}
void CButtonST::SetIcon(HICON hIconIn, HICON hIconOut)
{
// Note: the following two lines MUST be here! even if
// BoundChecker says they are unnecessary!
if (m_hIconIn != NULL) ::DestroyIcon(m_hIconIn); //判定是否與圖片相關(guān)聯(lián)
if (m_hIconOut != NULL) ::DestroyIcon(m_hIconOut);
// Set icon when the mouse is IN the button
m_hIconIn = hIconIn;
// Set icon when the mouse is OUT the button
m_hIconOut = (hIconOut == NULL) ? m_hIconIn : hIconOut;
ICONINFO ii; //ICONINFO 結(jié)構(gòu)體
// Get icon dimension
ZeroMemory(&ii, sizeof(ICONINFO)); //ii內(nèi)存設(shè)0
::GetIconInfo(m_hIconIn, &ii); //設(shè)置自定義圖標(biāo)
m_cxIcon = (BYTE)(ii.xHotspot * 2);
m_cyIcon = (BYTE)(ii.yHotspot * 2);
::DeleteObject(ii.hbmMask);
::DeleteObject(ii.hbmColor);
RedrawWindow();
} // End of SetIcon
程序流程:
a)添加頭文件
CButton m_btn;定義一對象
b)在新建框架類中初始化
BOOL CMy5Dlg::OnInitDialog()
{
m_btn.SetIcon(IDI_ICON1,IDI_ICON2); //設(shè)置兩幅圖標(biāo)
}
c)鼠標(biāo)點(diǎn)擊交換圖片
void CMy5Dlg::DoDataExchange(CDataExchange* pDX)
{
DDX_Control(pDX,IDC_BUTTON1,m_btn);
}