【100分奉上】请问Tab page如何实现??

Paradin 2008-07-07 12:54:51
新手求教,大侠勿笑:
大家知不知道MFC怎么实现TabPage啊?CTabCtrl这个类我不知道放在什么地方好。拖控件的话只能放那个dialog里面啊,左边分了一个CTreeView,右边放一个CView,现在我想在右边实现TabPage,应该怎么做呢?
...全文
136 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
sanshao27 2008-07-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cnzdgs 的回复:]
右边用CFormView,在对话框资源上放CTabCtrl,再做若干个子对话框作为TabPage。也可以用代码来创建控件。
[/Quote]
asddg67 2008-07-07
  • 打赏
  • 举报
回复
www.vcbase.com
na_he 2008-07-07
  • 打赏
  • 举报
回复
使用:
CPMTableWnd *m_pTableWnd;
m_pTableWnd = new CPMTableWnd;
if(!m_pTableWnd->Create(NULL,"PMTabWnd",WS_CHILD|WS_VISIBLE/*|WS_BORDER*/,rc,this,1))
{
TRACE("Create TableWnd Error!\n");
delete m_pTableWnd;
return -1;
}
m_pTableWnd->InsertTab(mywnd1,"wnd1",LOCALANDFTP_VIEW,IDB_TREECATELOCAL_UP,IDB_TREECATELOCAL_DOWN);
m_pTableWnd->InsertTab(mywnd2,"wnd2",CATEGORY_VIEW,IDB_TREECATEGORY_UP,IDB_TREECATEGORY_DOWN);
GetActiveTab()获取激活的tab
SEtactiveTab()zhizhe
//上面是常用的几个interface了
na_he 2008-07-07
  • 打赏
  • 举报
回复

int CPMTableWnd::GetTabFromPoint (CPoint& pt) const
{
int nSize = m_arTabs.GetSize();
if (nSize > 0) // have window in table bar
{
for(int i = 0 ; i < nSize ; i++)
{
CTabInfo * pInfo = (CTabInfo *) m_arTabs.GetAt(i);
if(pInfo->m_rect.PtInRect(pt))
{
return i;
}
} // End For
}// End if size
return -1;
}
void CPMTableWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(SetActiveTab(GetTabFromPoint(point)))
{
TCHAR szIniFile[MAX_PATH];
BOOL bRet = GetPMINIFileNameEx(PMSET_INI, szIniFile);
ASSERT(bRet);
BOOL bShowDlg = GetPrivateProfileInt("Setting", "ShowSwitchDialog", 0, szIniFile);
if(bShowDlg)
{
CSwtichTableDlg dlg;
if(dlg.DoModal() == IDOK)
{
BOOL bSwitch = GetPrivateProfileInt("Setting", "SwitchTable", 1, szIniFile);
if(bSwitch)
{
CWnd * pWnd = GetActiveWnd();
if(pWnd) pWnd->SendMessage(SWITCH_LISTVIEW);
}
}
}
else
{
BOOL bSwitch = GetPrivateProfileInt("Setting", "SwitchTable", 1, szIniFile);
if(bSwitch)
{
CWnd * pWnd = GetActiveWnd();
if(pWnd) pWnd->SendMessage(SWITCH_LISTVIEW);
}
}
}
CWnd::OnLButtonDown(nFlags, point);
}

void CPMTableWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting

int nSize = m_arTabs.GetSize();
CFont * pFont = GetFont();
if (nSize > 0) // have window in table bar
for(int i = 0 ; i < nSize ; i++)
{
CTabInfo * pInfo = (CTabInfo *) m_arTabs.GetAt(i);
if (i == m_iActiveTab)
pInfo->Draw(&dc,TRUE);
else
pInfo->Draw(&dc);
}
}



CTabInfo::CTabInfo( const CString& strText,
const UINT uiHiliBmp,
const UINT uiDisableBmp,
CWnd* pWnd,
const int iTabID)
{
m_strText = strText;
m_rect.SetRectEmpty ();
m_bVisible = TRUE;
m_nFullWidth = 0;
if (uiHiliBmp != -1 && uiDisableBmp != -1)
{
m_pHiliIcon = new CBitmap;
m_pHiliIcon->LoadBitmap(uiHiliBmp);

m_pDisableIcon = new CBitmap;
m_pDisableIcon->LoadBitmap(uiDisableBmp);

}
else
{
m_pHiliIcon = NULL;
m_pDisableIcon = NULL;
}
m_pLeftBmp = new CBitmap;
m_pLeftBmp->LoadBitmap(IDB_TREEHILI_LEFT);
m_pRightBmp = new CBitmap;
m_pRightBmp->LoadBitmap(IDB_TREEHILI_RIGHT);
m_pBottomBmp = new CBitmap;
m_pBottomBmp->LoadBitmap(IDB_TREEHILI_BOTTOM);

m_pDLeftBmp = new CBitmap;
m_pDLeftBmp->LoadBitmap(IDB_TREEDISABLE_LEFT);
m_pDRightBmp = new CBitmap;
m_pDRightBmp->LoadBitmap(IDB_TREEDISABLE_RIGHT);
m_pDBottomBmp = new CBitmap;
m_pDBottomBmp->LoadBitmap(IDB_TREEDISABLE_BOTTOM);

m_pWnd = pWnd;
m_clrText = (COLORREF) -1;
m_clrBack = (COLORREF) -1;
}

CTabInfo::~CTabInfo()
{
if (m_pLeftBmp != NULL)
{
delete m_pLeftBmp;
m_pLeftBmp = NULL;
}

if (m_pBottomBmp != NULL)
{
delete m_pBottomBmp;
m_pBottomBmp = NULL;
}

if (m_pRightBmp != NULL)
{
delete m_pRightBmp;
m_pRightBmp = NULL;
}

if (m_pDLeftBmp != NULL)
{
delete m_pDLeftBmp;
m_pDLeftBmp = NULL;
}

if (m_pDBottomBmp != NULL)
{
delete m_pDBottomBmp;
m_pDBottomBmp = NULL;
}

if (m_pDRightBmp != NULL)
{
delete m_pDRightBmp;
m_pDRightBmp = NULL;
}


if (m_pHiliIcon != NULL)
{
delete m_pHiliIcon;
m_pHiliIcon = NULL;
}

if (m_pDisableIcon != NULL)
{
delete m_pDisableIcon;
m_pDisableIcon = NULL;
}
}


LPCTSTR MakeShortString(CDC *pDC, LPCTSTR lpszLong, int nColumnLen, int nOffset)
{
static const _TCHAR szThreeDots[] = _T("...");

int nStringLen = lstrlen(lpszLong);
if(nStringLen == 0 ||
(pDC->GetTextExtent(lpszLong, nStringLen).cx + nOffset) <= nColumnLen)
return(lpszLong);

static _TCHAR szShort[MAX_PATH];
lstrcpy(szShort,lpszLong);
int nAddLen = pDC->GetTextExtent(szThreeDots,sizeof(szThreeDots)).cx;

for(int i = nStringLen-1; i > 0; i--)
{
szShort[i] = 0;
if((pDC->GetTextExtent(szShort, i).cx + nOffset + nAddLen)
<= nColumnLen)
{
break;
}
}
lstrcat(szShort, szThreeDots);
return(szShort);
}


void CTabInfo::Draw(CDC *pDC,BOOL bSelected)
{
ASSERT(pDC);

CDC * pMemeDC = new CDC;
pMemeDC->CreateCompatibleDC(pDC);
CBitmap * pOld = NULL;
CRect rect = m_rect;

if (bSelected )
{
//Draw left image
BITMAP leftbmp,rightbmp,topbmp;
memset(&leftbmp,0,sizeof(BITMAP));
memset(&rightbmp,0,sizeof(BITMAP));
memset(&topbmp,0,sizeof(BITMAP));

if(m_pLeftBmp)
{
m_pLeftBmp->GetBitmap(&leftbmp);
pOld = (CBitmap*)pMemeDC->SelectObject(m_pLeftBmp);
pDC->BitBlt(m_rect.left,0,leftbmp.bmWidth,leftbmp.bmHeight,pMemeDC,0,0,SRCCOPY);
}

//Draw right image
if(m_pRightBmp)
{
m_pRightBmp->GetBitmap(&rightbmp);
pOld = (CBitmap*)pMemeDC->SelectObject(m_pRightBmp);
pDC->BitBlt(m_rect.right - rightbmp.bmWidth ,0,rightbmp.bmWidth,rightbmp.bmHeight,pMemeDC,0,0,SRCCOPY);
}

//Draw bottom image
if(m_pBottomBmp)
{
m_pBottomBmp->GetBitmap(&topbmp);
pOld = (CBitmap*)pMemeDC->SelectObject(m_pBottomBmp);
int nposx = m_rect.left + leftbmp.bmWidth ;
int nposy = m_rect.bottom - topbmp.bmHeight - 4;
int nWidth = m_rect.Width() - leftbmp.bmWidth - rightbmp.bmWidth ;
pDC->StretchBlt (nposx, 0 , nWidth ,
topbmp.bmHeight ,pMemeDC , 0 , 0 , topbmp.bmWidth ,
topbmp.bmHeight , SRCCOPY);
}

//Draw Icon
BITMAP HiliIconbmp;
memset(&HiliIconbmp,0,sizeof(BITMAP));
if(m_pHiliIcon)
{
m_pHiliIcon->GetBitmap(&HiliIconbmp);
pOld = (CBitmap*)pMemeDC->SelectObject(m_pHiliIcon);
pDC->BitBlt(m_rect.left + leftbmp.bmWidth ,5 ,HiliIconbmp.bmWidth,HiliIconbmp.bmHeight,pMemeDC,0,0,SRCCOPY);

}

rect.left = m_rect.left + leftbmp.bmWidth + HiliIconbmp.bmWidth;
rect.right = m_rect.right - rightbmp.bmWidth;


}
else
{
//Draw left image
BITMAP leftbmp,rightbmp,topbmp;
memset(&leftbmp,0,sizeof(BITMAP));
memset(&rightbmp,0,sizeof(BITMAP));
memset(&topbmp,0,sizeof(BITMAP));
if(m_pDLeftBmp)
{
m_pDLeftBmp->GetBitmap(&leftbmp);
pOld = (CBitmap*)pMemeDC->SelectObject(m_pDLeftBmp);
pDC->BitBlt(m_rect.left,0,leftbmp.bmWidth,leftbmp.bmHeight,pMemeDC,0,0,SRCCOPY);
}

//Draw right image
if(m_pDRightBmp)
{
m_pDRightBmp->GetBitmap(&rightbmp);
pOld = (CBitmap*)pMemeDC->SelectObject(m_pDRightBmp);
pDC->BitBlt(m_rect.right - rightbmp.bmWidth ,0,rightbmp.bmWidth,rightbmp.bmHeight,pMemeDC,0,0,SRCCOPY);
}

//Draw bottom image
if(m_pDBottomBmp)
{
m_pDBottomBmp->GetBitmap(&topbmp);
pOld = (CBitmap*)pMemeDC->SelectObject(m_pDBottomBmp);
int nposx = m_rect.left + leftbmp.bmWidth ;
int nWidth = m_rect.Width() - leftbmp.bmWidth - rightbmp.bmWidth ;
pDC->StretchBlt (nposx, 0 , nWidth ,
topbmp.bmHeight ,pMemeDC , 0 , 0 , topbmp.bmWidth ,
topbmp.bmHeight , SRCCOPY);
}

//Draw Icon
BITMAP DisableIconbmp;
memset(&DisableIconbmp,0,sizeof(BITMAP));
if(m_pDisableIcon)
{
m_pDisableIcon->GetBitmap(&DisableIconbmp);
pOld = (CBitmap*)pMemeDC->SelectObject(m_pDisableIcon);
pDC->BitBlt(m_rect.left + leftbmp.bmWidth ,8 ,DisableIconbmp.bmWidth,DisableIconbmp.bmHeight,pMemeDC,0,0,SRCCOPY);

}
// = NULL;
rect.left = m_rect.left + leftbmp.bmWidth + DisableIconbmp.bmWidth;
rect.right = m_rect.right - rightbmp.bmWidth;

}

rect.top += 4;
int nMode = pDC->SetBkMode(TRANSPARENT);
char szTxt[MAX_PATH];
strcpy(szTxt,m_strText);
CString str = MakeShortString(pDC,szTxt,rect.Width(),0);
if(rect.Width() > 0)
pDC->DrawText(str,rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
pDC->SetBkMode(nMode);

//Delete temp dc
pMemeDC->SelectObject(pOld);
delete pMemeDC;
pMemeDC = NULL;

}
na_he 2008-07-07
  • 打赏
  • 举报
回复
.cpp code

#include "stdafx.h"
#include "PMTableWnd.h"
#include "resource.h"
#include "SwtichTableDlg.h"
#include "Pglobal.h"
#include "CommFun.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// CPMTableWnd

CPMTableWnd::CPMTableWnd()
{
m_nTabsHeight = 30;
m_iActiveTab = 0;
m_iTabsNum = 0;
m_rectWndArea.SetRectEmpty();
}

CPMTableWnd::~CPMTableWnd()
{
int nSize = m_arTabs.GetSize();
if (nSize > 0) // have window in table bar
for(int i = 0 ; i < nSize ; i++)
{
CTabInfo * pInfo = (CTabInfo *) m_arTabs.GetAt(i);
delete pInfo;
pInfo = NULL;
}

}


BEGIN_MESSAGE_MAP(CPMTableWnd, CWnd)
//{{AFX_MSG_MAP(CPMTableWnd)
ON_WM_SIZE()
ON_WM_LBUTTONDOWN()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CPMTableWnd message handlers

void CPMTableWnd::AdjustTabs ()
{
CWnd * pParentWnd = GetParent();
CRect rect;
pParentWnd->GetWindowRect(rect);
//Set Table Bar Size
MoveWindow(0,rect.Height() - m_nTabsHeight,rect.Width(),m_nTabsHeight);

//Set View Size
m_rectWndArea.SetRect(0,0,rect.Width(),rect.Height() - m_nTabsHeight) ;

int nSize = m_arTabs.GetSize();
if (nSize > 0) // have window in table bar
{
int nWidth = rect.Width()/nSize;
int nTableWidth = 0;
for(int i = 0 ; i < nSize ; i++)
{
CTabInfo * pInfo = (CTabInfo *) m_arTabs.GetAt(i);
pInfo->m_rect.SetRect(nTableWidth,0,nTableWidth+nWidth,m_nTabsHeight);
nTableWidth += nWidth;
pInfo->m_pWnd->ShowWindow(SW_HIDE);
}

// Set Active View
CTabInfo * pActiveInfo = (CTabInfo *) m_arTabs.GetAt(GetActiveTab());
pActiveInfo->m_pWnd->ShowWindow(SW_SHOW);
pActiveInfo->m_pWnd->SetWindowPos (NULL,
m_rectWndArea.left, m_rectWndArea.top,
m_rectWndArea.Width (), m_rectWndArea.Height (),
SWP_NOACTIVATE | SWP_NOZORDER);
}
Invalidate();
}

void CPMTableWnd::OnSize(UINT nType, int cx, int cy)
{

CWnd::OnSize(nType, cx, cy);
AdjustTabs();
// TODO: Add your message handler code here
}

void CPMTableWnd::InsertTab (CWnd* pNewWnd, LPCTSTR lpszTabLabel, int nInsertAt, UINT uiImageHiliId /*= (UINT)-1*/, UINT uiImageDisableId /*= (UINT)-1*/)
{
ASSERT(pNewWnd);
ASSERT(nInsertAt >= 0 && nInsertAt <= m_iTabsNum);
CTabInfo * pTableInfo = new CTabInfo(lpszTabLabel,uiImageHiliId,uiImageDisableId,pNewWnd,nInsertAt);
m_arTabs.InsertAt(nInsertAt,pTableInfo);
m_iTabsNum++;
}

CWnd* CPMTableWnd::GetActiveWnd () const
{
// ASSERT(m_iActiveTab < m_arTabs.GetSize() -1);
CTabInfo * pTableInfo =(CTabInfo *) m_arTabs.GetAt(m_iActiveTab);
ASSERT(pTableInfo);
if(pTableInfo)
{
return pTableInfo->m_pWnd;
}
return NULL;

}

BOOL CPMTableWnd::SetActiveTab (int iTab)
{
ASSERT(iTab >= 0);
BOOL bReturn = false;
if(m_iActiveTab == iTab)
bReturn = false;
else if (iTab >= 0 && m_iTabsNum > iTab)
{
bReturn = true;
m_iActiveTab = iTab;
AdjustTabs();
}

return bReturn;
}
na_he 2008-07-07
  • 打赏
  • 举报
回复
我有做过了,我贴出主要code,你看看思路了,
.h文件
#if !defined(AFX_PMTABLEWND_H__BA31AD37_F79B_46B8_A460_2E1864B3D7D3__INCLUDED_)
#define AFX_PMTABLEWND_H__BA31AD37_F79B_46B8_A460_2E1864B3D7D3__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// PMTableWnd.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CPMTableWnd window

class CTabInfo : public CObject
{
public:
void Draw(CDC * pDC,int bSelected = false);
CTabInfo( const CString& strText,
const UINT uiHiliBmp,
const UINT uiDisableBmp,
CWnd* pWnd,
const int iTabID);
virtual ~CTabInfo();


CString m_strText;
UINT m_uiIcon;
CRect m_rect;
BOOL m_bVisible;
CWnd* m_pWnd;
int m_nFullWidth;
int m_iTabID;

COLORREF m_clrText;
COLORREF m_clrBack;
protected:

CBitmap* m_pHiliIcon;
CBitmap* m_pDisableIcon;
CBitmap* m_pLeftBmp;
CBitmap* m_pRightBmp;
CBitmap* m_pBottomBmp;
CBitmap* m_pDLeftBmp;
CBitmap* m_pDRightBmp;
CBitmap* m_pDBottomBmp;

};


class CPMTableWnd : public CWnd
{
// Construction
public:

// Attributes
public:

BOOL SetImageList (HIMAGELIST hImageList);
void SetTabsHeight ();
int GetTabsHeight () const
{
return m_nTabsHeight;
}
void InsertTab (CWnd* pNewWnd, LPCTSTR lpszTabLabel, int nInsertAt, UINT uiImageHiliId = (UINT)-1, UINT uiImageDisableId = (UINT)-1);

BOOL RemoveTab (int iTab, BOOL bRecalcLayout = TRUE);
void RemoveAllTabs ();
int GetVisibleTabsNum () const;
BOOL ShowTab (int iTab, BOOL bShow = TRUE, BOOL bRecalcLayout = TRUE, BOOL bActivate = FALSE);
CWnd* GetTabWnd (int iTab) const;

BOOL GetTabRect (int iTab, CRect& rect) const;

BOOL GetTabLabel (int iTab, CString& strLabel) const;
BOOL SetTabLabel (int iTab, const CString& strLabel);
UINT GetTabIcon (int iTab) const;
BOOL SetTabIcon (int iTab, HICON hIcon);

BOOL SetActiveTab (CPoint point);
COLORREF GetTabBkColor (int iTab) const;
BOOL SetTabBkColor (int iTab, COLORREF color = (COLORREF)-1);

COLORREF GetTabTextColor (int iTab) const;
BOOL SetTabTextColor (int iTab, COLORREF color = (COLORREF)-1);
// Tab activation:
int GetActiveTab () const
{
return m_iActiveTab;
}

CWnd* GetActiveWnd () const;
BOOL SetActiveTab (int iTab) ;
int GetTabFromPoint (CPoint& pt) const;

void AdjustTabs (); //adjust table info rect

protected:
CPtrArray m_arTabs; // Array of CTabInfo objects
int m_iTabsNum; // m_arTabs size
int m_iActiveTab; // Active tab number
int m_nTabsHeight; // Height of table bar
CRect m_rectWndArea; // View area
// Operations
// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPMTableWnd)
//}}AFX_VIRTUAL

// Implementation
public:
virtual ~CPMTableWnd();
CPMTableWnd();

// Generated message map functions
protected:
//{{AFX_MSG(CPMTableWnd)
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnPaint();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_PMTABLEWND_H__BA31AD37_F79B_46B8_A460_2E1864B3D7D3__INCLUDED_)
yc_8301 2008-07-07
  • 打赏
  • 举报
回复
帮顶,,
我也想知道,,高手都来回答了呀!!
healer_kx 2008-07-07
  • 打赏
  • 举报
回复
可以用CPropertyPage和CPropertySheet,也有CTabCtrl用。
jimzhangwhu 2008-07-07
  • 打赏
  • 举报
回复
可是如果希望左边点一个就出一个TabPage怎么办呢?如果自己创建的TabPage是有限的啊?
cnzdgs 2008-07-07
  • 打赏
  • 举报
回复
右边用CFormView,在对话框资源上放CTabCtrl,再做若干个子对话框作为TabPage。也可以用代码来创建控件。
Yofoo 2008-07-07
  • 打赏
  • 举报
回复
在对话框中加入属性页
http://www.vckbase.com/document/viewdoc/?id=398
jimzhangwhu 2008-07-07
  • 打赏
  • 举报
回复
我也想知道啊,高手教我们啊!!!!

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧