一。按钮
将按钮升级为自定义按钮,MyButton
1.右键项目,添加MyButton类
2.在MyButton类中添加自定义功能,此时MyButton类相当于一个窗口,事件是标准消息,不是通告消息。
1 void MyButton::OnMouseMove(UINT nFlags, CPoint point) //当鼠标在MyButton(窗口)上移动时,改变按钮(窗口)位置 2 { 3 // TODO: 在此添加消息处理程序代码和/或调用默认值` 4 int x = rand() % 400; 5 int y = rand() % 400; 6 this->MoveWindow(x, y, 100, 50); 7 8 CButton::OnMouseMove(nFlags, point); 9 }
3.拖入Button控件,添加变量,类型选择MyButton
4.给按钮添加图片,在OnInitDialog中添加代码:
1 //加载位图 2 CBitmap bitmap; 3 bitmap.LoadBitmapW(IDB_BITMAP1); 4 //bmp获取位图信息 5 BITMAP bmp; 6 bitmap.GetBitmap(&bmp); 7 //给按钮设置位图 8 m_button.SetBitmap(bitmap); 9 m_button.MoveWindow(20, 20, bmp.bmWidth, bmp.bmHeight);
二。TabControl
1.在项目目录放入TabSheet.cpp和TabSheet.h文件,并在解决方案添加现有项,作为TabSheet类
- TabSheet.cpp
#include "pch.h" #include "TabSheet.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CTabSheet CTabSheet::CTabSheet() { m_nNumOfPages = 0; m_nCurrentPage = 0; } CTabSheet::~CTabSheet() { } BEGIN_MESSAGE_MAP(CTabSheet, CTabCtrl) //{{AFX_MSG_MAP(CTabSheet) ON_WM_LBUTTONDOWN() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CTabSheet message handlers BOOL CTabSheet::AddPage(LPCTSTR title, CDialog *pDialog,UINT ID) { if( MAXPAGE == m_nNumOfPages ) return FALSE; m_nNumOfPages++; m_pPages[m_nNumOfPages-1] = pDialog; m_IDD[m_nNumOfPages-1] = ID; m_Title[m_nNumOfPages-1] = title; return TRUE; } void CTabSheet::SetRect() { CRect tabRect, itemRect; int nX, nY, nXc, nYc; GetClientRect(&tabRect); GetItemRect(0, &itemRect); nX=itemRect.left; nY=itemRect.bottom+1; nXc=tabRect.right-itemRect.left-2; nYc=tabRect.bottom-nY-2; m_pPages[0]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_SHOWWINDOW); for( int nCount=1; nCount < m_nNumOfPages; nCount++ ) m_pPages[nCount]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_HIDEWINDOW); } void CTabSheet::Show() { for( int i=0; i < m_nNumOfPages; i++ ) { m_pPages[i]->Create( m_IDD[i], this ); InsertItem( i, m_Title[i] ); } m_pPages[0]->ShowWindow(SW_SHOW); for( int i=1; i < m_nNumOfPages; i++) m_pPages[i]->ShowWindow(SW_HIDE); SetRect(); } void CTabSheet::OnLButtonDown(UINT nFlags, CPoint point) { CTabCtrl::OnLButtonDown(nFlags, point); if(m_nCurrentPage != GetCurFocus()) { m_pPages[m_nCurrentPage]->ShowWindow(SW_HIDE); m_nCurrentPage=GetCurFocus(); m_pPages[m_nCurrentPage]->ShowWindow(SW_SHOW); // m_pPages[m_nCurrentPage]->SetFocus(); } } int CTabSheet::SetCurSel(int nItem) { if( nItem < 0 || nItem >= m_nNumOfPages) return -1; int ret = m_nCurrentPage; if(m_nCurrentPage != nItem ) { m_pPages[m_nCurrentPage]->ShowWindow(SW_HIDE); m_nCurrentPage = nItem; m_pPages[m_nCurrentPage]->ShowWindow(SW_SHOW); // m_pPages[m_nCurrentPage]->SetFocus(); CTabCtrl::SetCurSel(nItem); } return ret; } int CTabSheet::GetCurSel() { return CTabCtrl::GetCurSel(); } //释放page资源 void CTabSheet::Free() { CTabCtrl::DeleteAllItems(); for( int i=0; i < m_nNumOfPages; i++ ) { m_pPages[i]->DestroyWindow(); m_IDD[i] = NULL; m_Title[i] = NULL; } m_nCurrentPage = 0; m_nNumOfPages = 0; //CTabCtrl::DestroyWindow(); //注意不要将this指针释放了 }
- TabSheet.h
1 #if !defined(AFX_TABSHEET_H__42EE262D_D15F_46D5_8F26_28FD049E99F4__INCLUDED_) 2 #define AFX_TABSHEET_H__42EE262D_D15F_46D5_8F26_28FD049E99F4__INCLUDED_ 3 4 #if _MSC_VER > 1000 5 #pragma once 6 #endif // _MSC_VER > 1000 7 // TabSheet.h : header file 8 // 9 10 ///////////////////////////////////////////////////////////////////////////// 11 // CTabSheet window 12 #define MAXPAGE 16 13 14 class CTabSheet : public CTabCtrl 15 { 16 // Construction 17 public: 18 CTabSheet(); 19 20 // Attributes 21 public: 22 23 // Operations 24 public: 25 26 // Overrides 27 // ClassWizard generated virtual function overrides 28 //{{AFX_VIRTUAL(CTabSheet) 29 //}}AFX_VIRTUAL 30 31 // Implementation 32 public: 33 int GetCurSel(); 34 int SetCurSel(int nItem); 35 void Show(); 36 void Free(); 37 38 void SetRect(); 39 BOOL AddPage(LPCTSTR title, CDialog *pDialog, UINT ID); 40 virtual ~CTabSheet(); 41 42 // Generated message map functions 43 protected: 44 LPCTSTR m_Title[MAXPAGE]; 45 UINT m_IDD[MAXPAGE]; 46 CDialog* m_pPages[MAXPAGE]; 47 int m_nNumOfPages; 48 int m_nCurrentPage; 49 //{{AFX_MSG(CTabSheet) 50 afx_msg void OnLButtonDown(UINT nFlags, CPoint point); 51 //}}AFX_MSG 52 53 DECLARE_MESSAGE_MAP() 54 }; 55 56 ///////////////////////////////////////////////////////////////////////////// 57 58 //{{AFX_INSERT_LOCATION}} 59 // Microsoft Visual C++ will insert additional declarations immediately before the previous line. 60 61 #endif // !defined(AFX_TABSHEET_H__42EE262D_D15F_46D5_8F26_28FD049E99F4__INCLUDED_)
2.新建两个Dialog作为两个子窗口,修改属性和ID。并生成.cpp和.h文件,为CDlgChild1和CDlgChild2
style=child
Border=None
3.在主窗口.h中添加两个对话框.h文件并创建实例。
CDlgChild1 dlg1;
CDlgChild2 dlg2;
4.为Tab_Control控件添加变量m_tab,类型为CTabSheet。
4.在主对话框的OnInitDialog中为控件变量添加页。
1 m_tab.AddPage(TEXT("第一页"), &dlg1, IDD_DIALOG1); 2 m_tab.AddPage(TEXT("第二页"), &dlg2, IDD_DIALOG2); 3 //显示 4 m_tab.Show();