VC 中Picture Control 添加圖片和單擊事件,比C#等語言稍微較麻煩
一、添加圖片
1、在窗體添加Picture Control控件
2、代碼添加圖片
HBITMAP hBmpWSUS = (HBITMAP)LoadImage (AfxGetResourceHandle(), MAKEINTRESOURCE(IDB_BITMAP_WSUS), IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS); // 關聯圖片ID HBITMAP hBmpVIRUS = (HBITMAP)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE (IDB_BITMAP_VIRUS), IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS); HBITMAP hBmpSOFT = (HBITMAP)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE (IDB_BITMAP_SOFT), IMAGE_BITMAP, 0, 0, LR_LOADMAP3DCOLORS); CStatic *pStaticWSUS = (CStatic*)GetDlgItem(IDC_PIC_WSUS); // 獲取指向靜態控件的指針 pStaticWSUS->ModifyStyle(0xF, SS_BITMAP|SS_CENTERIMAGE); // 設置靜態控件的樣式,使 其位圖居中顯示 pStaticWSUS->SetBitmap(hBmpWSUS); // 設置靜態控件的顯示 位圖 CStatic *pStaticVIRUS = (CStatic*)GetDlgItem(IDC_PIC_VIRUSLIST); pStaticVIRUS->ModifyStyle(0xF, SS_BITMAP|SS_CENTERIMAGE); pStaticVIRUS->SetBitmap(hBmpVIRUS); CStatic *pStaticSOFT = (CStatic*)GetDlgItem(IDC_PIC_SOFTLIST); pStaticSOFT->ModifyStyle(0xF, SS_BITMAP|SS_CENTERIMAGE); pStaticSOFT->SetBitmap(hBmpSOFT);
3、圖片顯示效果
二、單擊事件
1、設置Picture Control 的 notify 屬性為 true
2、 頭文件.h添加三個方法
public:
afx_msg void OnStnClickedPicWsus ();
afx_msg void OnStnClickedPicViruslist();
afx_msg void OnStnClickedPicSoftlist ();
3、源碼.cpp文件添加消息映射
BEGIN_MESSAGE_MAP(Main, CDialog)
ON_STN_CLICKED(IDC_PIC_WSUS, &Main::OnStnClickedPicWsus)
ON_STN_CLICKED (IDC_PIC_VIRUSLIST, &Main::OnStnClickedPicViruslist)
ON_STN_CLICKED (IDC_PIC_SOFTLIST, &Main::OnStnClickedPicSoftlist)
...
END_MESSAGE_MAP()
4、實現單擊事件方法
void Main::OnStnClickedPicWsus()
{
wsusDlg.ShowWindow(TRUE);
virusListDlg.ShowWindow(FALSE);
softListDlg.ShowWindow (FALSE);
}
void Main::OnStnClickedPicViruslist()
{
virusListDlg.ShowWindow (TRUE);
wsusDlg.ShowWindow(FALSE);
softListDlg.ShowWindow(FALSE);
}
void Main::OnStnClickedPicSoftlist()
{
softListDlg.ShowWindow(TRUE);
wsusDlg.ShowWindow (FALSE);
virusListDlg.ShowWindow(FALSE);
}