读书人

使用Tab控件如何出现了如下图所示有关

发布时间: 2011-12-31 23:50:30 作者: rapoo

使用Tab控件怎么出现了如下图所示问题。
该程序编译没有问题。

// textDlg.cpp : implementation file
//

#include "stdafx.h"
#include "text.h"
#include "textDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTextDlg dialog

CTextDlg::CTextDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTextDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTextDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTextDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTextDlg)
DDX_Control(pDX, IDC_TAB1, m_tab);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTextDlg, CDialog)
//{{AFX_MSG_MAP(CTextDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, OnSelchangeTab1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTextDlg message handlers

BOOL CTextDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);// Set big icon
SetIcon(m_hIcon, FALSE);// Set small icon

// TODO: Add extra initialization here
m_tab.InsertItem(0," 11111");
m_tab.InsertItem(1," 22222");


//建立属性页各页
pag0.Create(IDD_DIALOG0,GetDlgItem(IDC_TAB1));
pag1.Create(IDD_DIALOG1,GetDlgItem(IDC_TAB1));


//设置页面的位置在m_tab控件范围内
CRect rect;
m_tab.GetClientRect(&rect);
rect.top+=20;
rect.bottom-=4;
rect.left+=4;


rect.right-=4;
pag0.MoveWindow(&rect);
pag1.MoveWindow(&rect);


pag0.ShowWindow(TRUE);
m_tab.SetCurSel(0);

return TRUE; // return TRUE unless you set the focus to a control

}

void CTextDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CTextDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTextDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CTextDlg::OnSelchangeTab1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
int CurSel;
CurSel=m_tab.GetCurSel();

switch(CurSel)
{
case 0:
{
pag0.ShowWindow(TRUE);
pag1.ShowWindow(FALSE);
}break;

case 1:
{
pag0.ShowWindow(FALSE);
pag1.ShowWindow(TRUE);
}break;
default: ;
}

*pResult = 0;
}
运行结果如图所示:


[解决办法]
代码太长,没时间细看。
大致知道你是想把那个page放到tab上。下面是思路:
page创建的时候,要取得tab的坐标,然后创建到那个坐标上。

顺便提一下,那个OnSelchangeTab1函数这样写可以适用于两页的情况,但是如果有100页呢?100页还这么写要累死人的。这里提供另一种思路:
所以的page用数组实现,对所有的page先隐藏,然后把当前页显示。

读书人网 >VC/MFC

热点推荐