gpt4 book ai didi

c++ - mfc tab控件切换tabs

转载 作者:行者123 更新时间:2023-11-30 02:03:32 24 4
gpt4 key购买 nike

我创建了一个简单的选项卡控件,它有 2 个选项卡(每个选项卡都是一个不同的对话框)。问题是我不知道如何在选项卡之间切换(当用户按下 Titlu Tab1 以显示我为第一个选项卡制作的对话框时,当它按下 Titlu Tab2 以显示我的其他对话框)。我添加了一个用于更改项目的处理程序,但我不知道我应该如何访问某种索引或选项卡的子项。

Tab1.hTab2.h 是仅显示带有每个选项卡名称的静态文本的对话框的 header 。

我的问题可能有明显的答案,但我是 c++ 和 MFC 的真正新手。

这是我的标题:

// CTabControlDlg.h : header file
//

#pragma once
#include "afxcmn.h"
#include "Tab1.h"
#include "Tab2.h"


// CCTabControlDlg dialog
class CCTabControlDlg : public CDialog
{
// Construction
public:
CCTabControlDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
enum { IDD = IDD_CTABCONTROL_DIALOG };

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support


// Implementation
protected:
HICON m_hIcon;

// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
CTabCtrl m_tabcontrol1;

CTab1 m_tab1;
CTab2 m_tab2;
afx_msg void OnTcnSelchangeTabcontrol(NMHDR *pNMHDR, LRESULT *pResult);
};

这是.cpp:

// CTabControlDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CTabControl.h"
#include "CTabControlDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

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

// Dialog Data
enum { IDD = IDD_ABOUTBOX };

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

// Implementation
protected:
DECLARE_MESSAGE_MAP()
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CCTabControlDlg dialog




CCTabControlDlg::CCTabControlDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCTabControlDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCTabControlDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TABCONTROL, m_tabcontrol1);
}

BEGIN_MESSAGE_MAP(CCTabControlDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_NOTIFY(TCN_SELCHANGE, IDC_TABCONTROL, &CCTabControlDlg::OnTcnSelchangeTabcontrol)
END_MESSAGE_MAP()


// CCTabControlDlg message handlers

BOOL CCTabControlDlg::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

CTabCtrl* pTabCtrl = (CTabCtrl*)GetDlgItem(IDC_TABCONTROL);

m_tab1.Create(IDD_TAB1, pTabCtrl);

TCITEM item1;
item1.mask = TCIF_TEXT | TCIF_PARAM;
item1.lParam = (LPARAM)& m_tab1;
item1.pszText = _T("Titlu Tab1");
pTabCtrl->InsertItem(0, &item1);

//Pozitionarea dialogului
CRect rcItem;
pTabCtrl->GetItemRect(0, &rcItem);
m_tab1.SetWindowPos(NULL, rcItem.left, rcItem.bottom + 1, 0, 0, SWP_NOSIZE | SWP_NOZORDER );

m_tab1.ShowWindow(SW_SHOW);

// al doilea tab
m_tab2.Create(IDD_TAB2, pTabCtrl);
TCITEM item2;
item2.mask = TCIF_TEXT | TCIF_PARAM;
item2.lParam = (LPARAM)& m_tab1;
item2.pszText = _T("Titlu Tab2");
pTabCtrl->InsertItem(0, &item2);

//Pozitionarea dialogului
//CRect rcItem;
pTabCtrl->GetItemRect(0, &rcItem);
m_tab2.SetWindowPos(NULL, rcItem.left, rcItem.bottom + 1, 0, 0, SWP_NOSIZE | SWP_NOZORDER );

m_tab2.ShowWindow(SW_SHOW);

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

void CCTabControlDlg::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 CCTabControlDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCTabControlDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}


void CCTabControlDlg::OnTcnSelchangeTabcontrol(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;

}

最佳答案

您可以在 MFC 中自动执行此操作,方法是将父对话框设为 CPropertySheet 并将包含的对话框设为 CPropertyPage

按照您目前的结构方式,您应该为每个对话框执行一个ShowWindow,其中一个设置为SW_SHOW,另一个设置为SW_HIDE 在您的 OnTcnSelchangeTabcontrol 函数中。

关于c++ - mfc tab控件切换tabs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11746715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com