Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SSW_8_11.doc
Скачиваний:
2
Добавлен:
01.05.2025
Размер:
5 Мб
Скачать
☆

Int cTabCtrl::GetCurSel(); To Selects a tab in a tab control use SetCurSel() function:

Int cTabCtrl::SetCurSel( int nItem );

Here, nItem   The zero-based index of the item to be selected.

A tab control does not send a TCN_SELCHANGING or TCN_SELCHANGE notification message when a tab is selected using this function. These notifications are sent, using WM_NOTIFY, when the user clicks or uses the keyboard to change tabs.

To retrieve information about a tab in a tab control, use GetItem() function:

BOOL CTabCtrl::GetItem( int nItem, TC_ITEM* pTabCtrlItem ) const;

nItem   Zero-based index of the tab.

pTabCtrlItem   Pointer to a TC_ITEM structure, used to specify the information to retrieve. Also used to receive information about the tab. This structure is used with the InsertItem, GetItem, and SetItem member functions.

To calculate a tab control's display area given a window rectangle, or calculate the window rectangle that would correspond to a given display area use AdjustRect() function.

void CTabCtrl::AdjustRect( BOOL bLarger, LPRECT lpRect );

Parameters:

bLarger   Indicates which operation to perform. If this parameter is TRUE, lpRect specifies a display rectangle and receives the corresponding window rectangle. If this parameter is FALSE, lpRect specifies a window rectangle and receives the corresponding display rectangle.

lpRect   Pointer to a RECT structure that specifies the given rectangle and receives the calculated rectangle.

The Tab Control messages

On Tab Controls activation, to the WM_NOTIFY message there are linked three codes:

TCN_SELCHANGE - notifies a tab control’s parent window that the currently selected tab is about to change.

TCN_SELCHANGING - notifies a tab control’s parent window that the currently selected tab is about to change.

TCN_KEYDOWN - notifies a tab control’s parent window that a key has been pressed.

The messages are processed by macro command ON_NOTIFY().

    1. The property sheets and wizards

A property sheet is a special kind of dialog box that is generally used to modify the attributes of some external object, such as the current selection in a view. The property sheet has three main parts: the containing dialog box, one or more property pages shown one at a time, and a tab at the top of each page that the user clicks to select that page. Property sheets are useful for situations where you have a number of similar groups of settings or options to change. An example of a property sheet is the Project Settings dialog box in the integrated development environment. In this case, there are a number of different groups of options that need to be set. The property sheet allows a large amount of information to be grouped in an easily understood fashion.

Objects of class CPropertyPage represent individual pages of a property sheet, otherwise known as a tab dialog box. As with standard dialog boxes, you derive a class from CPropertyPage for each page in your property sheet. To use CPropertyPage-derived objects, first create a CPropertySheet object, and then create an object for each page that goes in the property sheet. Call CPropertySheet::AddPage for each page in the sheet, and then display the property sheet by calling CPropertySheet::DoModal for a modal property sheet, or CPropertySheet::Create for a modeless property sheet.

For the property sheets use #include <afxdlgs.h> preprocessor directive.

You can create a type of tab dialog box called a wizard, which consists of a property sheet with a sequence of property pages that guide the user through the steps of an operation, such as setting up a device or creating a newsletter. In a wizard-type tab dialog box, the property pages do not have tabs, and only one property page is visible at a time. Also, instead of having OK and Apply Now buttons, a wizard-type tab dialog box has a Back button, a Next or Finish button, and a Cancel button.

To use a property sheet in your application, complete the following steps:

  1. Create a dialog template resource for each property page. Keep in mind that the user may be switching from one page to another, so lay out each page as consistently as possible.

The dialog templates for all pages do not have to be the same size. The framework uses the size of the largest page to determine how much space to allocate in the property sheet for the property pages.

When you create the dialog template resource for a property page, you must specify the following styles in the Dialog Properties property sheet:

    1. Set the Caption edit box on the General page to the text you wish to appear in the tab for this page.

    2. Set the Style list box on the Styles page to Child.

    3. Set the Border list box on the Styles page to Thin.

    4. Ensure that the Titlebar check box on the Styles page is selected.

    5. Ensure that the Disabled check box on the More Styles page is selected.

  1. Use ClassWizard to create a CPropertyPage-derived class corresponding to each property page dialog template. To do this, choose ClassWizard from the View menu while the focus is on a particular property page dialog box. Choose CPropertyPage as the base class in ClassWizard.

  2. Using ClassWizard, create member variables to hold the values for this property page. The process for adding member variables to a property page is exactly the same as adding member variables to a dialog box, because a property page is a specialized dialog box.

  3. Construct a CPropertySheet object in your source code. Usually, you construct the CPropertySheet object in the handler for the command that displays the property sheet. This object represents the entire property sheet. If you create a modal property sheet with the DoModal function, the framework supplies three command buttons by default: OK, Cancel, and Apply. The framework creates no command buttons for modeless property sheets created with the Create function. You do not need to derive a class from CPropertySheet unless you want to either add other controls (such as a preview window) or display a modeless property sheet. This step is necessary for modeless property sheets because they do not contain any default controls that could be used to close the property sheet.

  4. For each page to be added to the property sheet, do the following:

    1. Construct one object for each CPropertyPage-derived class that you created using ClassWizard earlier in this process.

    2. Call CPropertySheet::AddPage for each page.

Typically, the object that creates the CPropertySheet also creates the CPropertyPage objects in this step. However, if you implement a CPropertySheet-derived class, you can embed the CPropertyPage objects in the CPropertySheet object and call AddPage for each page from the CPropertySheet-derived class constructor. AddPage adds the CPropertyPage object to the property sheet's list of pages but does not actually create the window for that page. Therefore, it is not necessary to wait until creation of the property sheet window to call AddPage; you can call AddPage from the property sheet's constructor.

  1. Call CPropertySheet::DoModal or Create to display the property sheet. Call DoModal to create a property sheet as a modal dialog box. Call Create to create the property sheet as a modeless dialog box.

  2. Exchange data between property pages and the owner of the property sheet.

To establish a property page as a wizard call CPropertySheet::SetWizardMode(). Call SetWizardMode before calling DoModal. After you call SetWizardMode, DoModal will return either ID_WIZFINISH (if the user closes with the Finish button) or IDCANCEL.

SetWizardMode sets the PSF_WIZARD flag.

On Pic. 6.2 There is the interface view for program with different control elements.

Pic 6.2. The view of control elements for listing 6.4.

Listing 6.4 the example program with common control elements

// t1.h: interface for the cmain class.

Class CMain : public CFrameWnd

{public:

void OnTab();

void OnTreeThange(NMHDR *hdr, lresult *notused);

void OnPaint();

void OnExpand();

void OneExpandAll();

void OnCollapse();

void InitTree();

void OnDialog();

void OnTtip(UINT idnotused, NMHDR *hdr,lresult *resultnotused);

void InitToolBar();

void OnExit();

CMain();

declare_message_map()

Private:

CTreeCtrl m_tree;

CToolBarCtrl m_tb;

};

Class CApp : public CWinApp

{public:

BOOL InitInstance();

};

Class CSDialog : public CDialog

{public:

void OnTabChange();

void OnNodeDelete();

void OnaAddNode();

void OnTreeChange(nmhdr *hdr,lresult *notused);

void OnPaint();

void InitTree();

void OnHScroll(uint sbcode,uint pos, CScrollBar *sb);

void OnVScroll(uint sbcode,uint pos, CScrollBar *sb);

bool OnInitDialog();

void OnTtip1(nmhdr *hdr,lresult *result);

csdialog(uint id,cwnd *owner):cdialog(id,owner){}

declare_message_map()

Private:

cstatusBarCtrl m_status;

cbutton *bt2;

cedit *edd2;

cbutton *bt1;

cedit *edd;

ctreeCtrl *tr;

cprogressctrl *pc;

csliderCtrl *slay;

cspinbuttonctrl m_spin;

};

Class ctabdialog1 : public cdialog

{public:

bool OnInitDialog();

ctabdialog1():cdialog(){}

declare_message_map()

Private:

cedit *edd;

};

Class ctabdialog2 : public cdialog

{public:

bool oninitdialog();

ctabdialog2():cdialog(){}

declare_message_map()

Private:

cedit *edd;

};

Class ctabdialog3 : public cdialog

{public:

bool oninitdialog();

ctabdialog3():cdialog(){}

declare_message_map()

Private:

cedit *edd;

};

Class csdialog2 : public cdialog

{public:

void ontabChange();

bool onInitDialog();

csdialog2(uint id, cwnd *owner):cdialog(id,owner){}

declare_message_map()

Private:

ctabctrl *tbc;

};

// t1.cpp: implementation of the classes.

#include <afxwin.h>

#include <afxcmn.h>

#include "t1.h"

#include "resource.h"

#define NUMBUTTONS 6

#define IDC_SPIN1 400001

#define ID_TREE 400002

#define IDD_STATUS 400003

#define NUM 5

#define NUMPARTS 3

CMain::CMain()

{Create(NULL,"Tool bar & MFC", WS_OVERLAPPEDWINDOW, rectDefault, NULL, MAKEINTRESOURCE(IDR_MENU1));

InitCommonControls();

InitToolBar();

RECT r;

r.left=r.top=30;

r.right=220;r.bottom=200;

m_Tree.Create(WS_VISIBLE | WS_CHILD | WS_BORDER |

TVS_EDITLABELS | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,

r,this,ID_TREE);

InitTree();

}

BEGIN_MESSAGE_MAP(CMain,CFrameWnd)

ON_COMMAND(ID_FIRST_EXIT,OnExit)

ON_COMMAND(ID_DIALOG,OnDialog)

ON_COMMAND(ID_SECOND_MESSAGE,OnMessage)

ON_COMMAND(ID_TAB,OnTab)

ON_WM_PAINT()

ON_NOTIFY(TVN_SELCHANGED,ID_TREE,OnTreeChange)

ON_NOTIFY_RANGE(TTN_NEEDTEXT,0,IDR_TOOLBAR1,OnTTip)

ON_COMMAND(ID_TREE_EXPAND,OnExpand)

ON_COMMAND(ID_TREE_EXPANDALL,OnExpandAll)

ON_COMMAND(ID_TREE_COLLAPSE,OnCollapse)

END_MESSAGE_MAP()

BOOL CApp::InitInstance()

{m_pMainWnd = new CMain;

m_pMainWnd -> ShowWindow(m_nCmdShow);

m_pMainWnd -> UpdateWindow();

return TRUE;

}

void CMain::OnExit()

{SendMessage(WM_CLOSE);

}

CApp App;

void CMain::InitToolBar()

{RECT r;

r.left=r.top=r.right=r.bottom=0;

TBBUTTON tbButton[4];

m_TB.Create(WS_VISIBLE | WS_CHILD |

TBSTYLE_TOOLTIPS,r,this,

IDR_TOOLBAR1);

tbButton[0].iBitmap=0;

tbButton[0].idCommand=ID_FIRST_EXIT;

tbButton[0].fsState=TBSTATE_ENABLED;

tbButton[0].fsStyle=TBSTYLE_BUTTON;

tbButton[0].dwData=0;

tbButton[0].iString=0;

tbButton[1].iBitmap=1;

tbButton[1].idCommand=ID_SECOND_MESSAGE;

tbButton[1].fsState=TBSTATE_ENABLED;

tbButton[1].fsStyle=TBSTYLE_CHECKGROUP;

tbButton[1].dwData=0;

tbButton[1].iString=0;

tbButton[2].iBitmap=2;

tbButton[2].idCommand=0;

tbButton[2].fsState=TBSTATE_INDETERMINATE;

tbButton[2].fsStyle=TBSTYLE_CHECKGROUP;

tbButton[2].dwData=0;

tbButton[2].iString=0;

tbButton[3].iBitmap=3;

tbButton[3].idCommand=ID_SECOND_MESSAGE;

tbButton[3].fsState=TBSTATE_ENABLED;

tbButton[3].fsStyle=TBSTYLE_CHECK;

tbButton[3].dwData=0;

tbButton[3].iString=0;

m_TB.AddButtons(4,tbButton);

m_TB.AddBitmap(4,IDR_TOOLBAR1);

}

void CMain::OnTTip(UINT idNotUsed,NMHDR *hdr,LRESULT *ResultNotUsed)

{switch(hdr->idFrom)

{case ID_FIRST_EXIT:

((TOOLTIPTEXT *)hdr)->lpszText="Exit"; break;

case ID_SECOND_MESSAGE:

((TOOLTIPTEXT *)hdr)->lpszText="SECOND MESSAGE: all carriers Of United Kingdom are avaliable"; break;

}

}

void CMain::OnDialog()

{CSDialog a(IDD_DIALOG1,this);

a.DoModal();

}

BEGIN_MESSAGE_MAP(CSDialog,CDialog)

ON_NOTIFY(NM_SETFOCUS,IDOK,OnTTip1)

ON_WM_HSCROLL()

ON_NOTIFY(TVN_SELCHANGED,IDC_TREE1,OnTreeChange)

ON_WM_VSCROLL()

ON_WM_PAINT()

ON_COMMAND(IDC_BUTTON1,OnAddNode)

ON_COMMAND(IDC_BUTTON2,OnNodeDelete)

END_MESSAGE_MAP()

void CSDialog::OnTTip1(NMHDR *hdr,LRESULT *result)

{char str[255];

wsprintf(str,"%ld",hdr->idFrom);

CClientDC dc(this);

dc.TextOut(1,1,str,15);

*result=0;

}

BOOL CSDialog::OnInitDialog()

{RECT r;

int i;

int parts[NUMPARTS];

CDialog::OnInitDialog();

GetClientRect(&r);

m_Spin.Create(WS_VISIBLE | WS_CHILD | WS_BORDER | UDS_SETBUDDYINT | UDS_ALIGNRIGHT,

r,this,IDC_SPIN1);

slay=(CSliderCtrl*)GetDlgItem(IDC_SLIDER1);

bt1=(CButton*)GetDlgItem(IDC_BUTTON1);

edd2=(CEdit*)GetDlgItem(IDC_EDIT1);

bt2=(CButton*)GetDlgItem(IDC_BUTTON2);

pc=(CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);

tr=(CTreeCtrl*)GetDlgItem(IDC_TREE1);

m_Spin.SetBuddy(GetDlgItem(IDC_EDIT2));

m_Spin.SetRange(0,100);

m_Spin.SetPos(5);

slay->SetRange(0,100);

slay->SetPos(15);

for(i=1;i<=NUMPARTS;i++)

parts[i-1]=r.right/NUMPARTS*i;

m_Status.Create(WS_VISIBLE | WS_CHILD,r,this,IDD_STATUS);

m_Status.SetParts(NUMPARTS,parts);

m_Status.SetText("Spin Ctrl: 10",0,0);

m_Status.SetText("Option 1 : ",1,0);

m_Status.SetText("Option 2 : 10",2,0);

InitTree();

return TRUE;

}

void CSDialog::OnHScroll(UINT SBCode, UINT Pos, CScrollBar * SB)

{if(SB==(CScrollBar *)slay)

{m_Spin.SetPos(slay->GetPos());

pc->SetPos(slay->GetPos());

char str3[40];

wsprintf(str3,"Slider position %d",slay->GetPos());

m_Status.SetText(str3,1,0);

}

}

void CSDialog::OnVScroll(UINT SBCode, UINT Pos, CScrollBar * SB)

{if(SB=(CScrollBar *)&m_Spin)

{slay->SetPos(m_Spin.GetPos());

pc->SetPos(m_Spin.GetPos());

char str3[40];

wsprintf(str3,"Spin position %d",m_Spin.GetPos());

m_Status.SetText(str3,2,SBT_POPOUT);

}

}

HTREEITEM hTreeCtrl[30];

HTREEITEM hTreeCurrent;

int i;

void CSDialog::InitTree()

{TV_INSERTSTRUCT tvs;

TV_ITEM tvi;

tvs.hInsertAfter=TVI_LAST;

tvi.mask=TVIF_TEXT;

tvi.pszText="One";

tvs.hParent=TVI_ROOT;

tvs.item=tvi;

hTreeCtrl[0]=tr->InsertItem(&tvs);

hTreeCurrent=hTreeCtrl[0];

tvi.pszText="Two";

tvs.hParent=hTreeCtrl[0];

tvs.item=tvi;

hTreeCtrl[1]=tr->InsertItem(&tvs);

tvi.pszText="Three";

tvs.hParent=hTreeCtrl[1];

tvs.item=tvi;

hTreeCtrl[2]=tr->InsertItem(&tvs);

tvi.pszText="Four";

tvs.hParent=hTreeCtrl[2];

tvs.item=tvi;

hTreeCtrl[3]=tr->InsertItem(&tvs);

tvi.pszText="Five";

tvs.hParent=hTreeCtrl[2];

tvs.item=tvi;

hTreeCtrl[4]=tr->InsertItem(&tvs);

}

void CSDialog::OnPaint()

{CPaintDC dc(this);

TV_ITEM tvi;

char str1[40];

char str2[40];

tvi.hItem = hTreeCurrent;

tvi.pszText = str1;

tvi.cchTextMax = sizeof(str1)-1;

tvi.mask=TVIF_TEXT | TVIF_HANDLE;

tr->GetItem(&tvi);

wsprintf(str2,"Current Selection %s",tvi.pszText);

dc.TextOut(2,120,str2,strlen(str2));

}

void CMain::InitTree()

{TV_INSERTSTRUCT tvs;

TV_ITEM tvi;

tvs.hInsertAfter=TVI_LAST;

tvi.mask=TVIF_TEXT;

tvi.pszText="One";

tvs.hParent=TVI_ROOT;

tvs.item=tvi;

hTreeCtrl[0]=m_Tree.InsertItem(&tvs);

hTreeCurrent=hTreeCtrl[0];

tvi.pszText="Two";

tvs.hParent=hTreeCtrl[0];

tvs.item=tvi;

hTreeCtrl[1]=m_Tree.InsertItem(&tvs);

tvi.pszText="Three";

tvs.hParent=hTreeCtrl[1];

tvs.item=tvi;

hTreeCtrl[2]=m_Tree.InsertItem(&tvs);

tvi.pszText="Four";

tvs.hParent=hTreeCtrl[2];

tvs.item=tvi;

hTreeCtrl[3]=m_Tree.InsertItem(&tvs);

tvi.pszText="Five";

tvs.hParent=hTreeCtrl[2];

tvs.item=tvi;

hTreeCtrl[4]=m_Tree.InsertItem(&tvs);

i=5;

}

void CSDialog::OnAddNode()

{if(!tr->GetSelectedItem())

MessageBox("Node isn't selected","");

else {i++;

char str[20];

edd2->GetWindowText(str,sizeof(str));

if(strlen(str))

{ hTreeCurrent=tr->GetSelectedItem();

TV_INSERTSTRUCT tvs;

TV_ITEM tvi;

tvs.hInsertAfter=TVI_LAST;

tvi.mask=TVIF_TEXT;

tvi.pszText=str;

tvs.hParent=hTreeCurrent;

tvs.item=tvi;

hTreeCtrl[i]=tr->InsertItem(&tvs);

hTreeCurrent=hTreeCtrl[i];

InvalidateRect(NULL);

}

else MessageBox("You should set unzero name ","");

}

}

void CMain::OnTreeChange(NMHDR * hdr, LRESULT * NotUsed)

{hTreeCurrent = m_Tree.GetSelectedItem();

InvalidateRect(NULL);

}

void CSDialog::OnTreeChange(NMHDR * hdr, LRESULT * NotUsed)

{hTreeCurrent = tr->GetSelectedItem();

InvalidateRect(NULL);

}

void CSDialog::OnNodeDelete()

{if(!tr->GetSelectedItem())

MessageBox("Node isn't selected","");

else {hTreeCurrent=tr->GetSelectedItem();

if(!tr->GetParentItem(hTreeCurrent))

MessageBox("You can't delete the root","");

else

{tr->DeleteItem(hTreeCurrent);

i--;

hTreeCurrent=hTreeCtrl[i--];

InvalidateRect(NULL);

}

}

}

void CMain::OnPaint()

{CPaintDC dc(this);

TV_ITEM tvi;

char str1[40];

char str2[40];

tvi.hItem = hTreeCurrent;

tvi.pszText = str1;

tvi.cchTextMax = sizeof(str1)-1;

tvi.mask=TVIF_TEXT | TVIF_HANDLE;

m_Tree.GetItem(&tvi);

wsprintf(str2,"Current Selection %s",tvi.pszText);

dc.TextOut(2,350,str2,strlen(str2));

}

void CMain::OnExpand()

{m_Tree.Expand(hTreeCurrent,TVE_EXPAND);

}

void CMain::OnExpandAll()

{int i;

for(i=0;i<NUM;i++)

{m_Tree.Expand(hTreeCtrl[i],TVE_EXPAND);}

}

void CMain::OnCollapse()

{m_Tree.Expand(hTreeCurrent,TVE_COLLAPSE);

}

CTabDialog1 DiagOb1;

CTabDialog2 DiagOb2;

CTabDialog3 DiagOb3;

CDialog *CurDialog;

BOOL CTabDialog1::OnInitDialog()

{CDialog::OnInitDialog();

edd=(CEdit*)GetDlgItem(IDC_EDIT1);

CClientDC dc(this);

dc.TextOut(0,0,"Invincible",20);

return TRUE;

}

BOOL CTabDialog2::OnInitDialog()

{CDialog::OnInitDialog();

edd=(CEdit*)GetDlgItem(IDC_EDIT1);

CClientDC dc(this);

dc.TextOut(10,40,"Illustrious",20);

return TRUE;

}

BOOL CTabDialog3::OnInitDialog()

{CDialog::OnInitDialog();

edd=(CEdit*)GetDlgItem(IDC_EDIT1);

CClientDC dc(this);

dc.TextOut(10,40,"Arc Royal",20);

return TRUE;

}

void CSDialog2::OnTabChange()

{ char str[30];

wsprintf(str,"Selected tab: %d",tbc->GetCurFocus());

CClientDC dc(this);

if(CurDialog)CurDialog->DestroyWindow();

dc.TextOut(0,0,str);

switch(tbc->GetCurSel())

{case 0: DiagOb1.Create(IDD_DIALOG3,this);

CurDialog=&DiagOb1;

break;

case 1: DiagOb2.Create(IDD_DIALOG4,this);

CurDialog=&DiagOb2;

break;

case 2: DiagOb3.Create(IDD_DIALOG5,this);

CurDialog=&DiagOb3;

break;

}

}

BEGIN_MESSAGE_MAP(CTabDialog1,CDialog)

END_MESSAGE_MAP()

BEGIN_MESSAGE_MAP(CTabDialog2,CDialog)

END_MESSAGE_MAP()

BEGIN_MESSAGE_MAP(CTabDialog3,CDialog)

END_MESSAGE_MAP()

BEGIN_MESSAGE_MAP(CSDialog2,CDialog)

ON_NOTIFY(TCN_SELCHANGING,IDC_TAB1,OnTabChange)

END_MESSAGE_MAP()

BOOL CSDialog2::OnInitDialog()

{CDialog::OnInitDialog();

tbc=(CTabCtrl*)GetDlgItem(IDC_TAB1);

TC_ITEM tci;

tci.mask=TCIF_TEXT;

tci.iImage=-1;

tci.pszText="Invincible";

tbc->InsertItem(0,&tci);

tci.pszText="Illustrious";

tbc->InsertItem(1,&tci);

tci.pszText="Arc Royal";

tbc->InsertItem(2,&tci);

CurDialog=&DiagOb1;

DiagOb1.Create(IDD_DIALOG3,this);

return TRUE;

}

void CMain::OnTab()

{CSDialog2 a(IDD_DIALOG2,this);

a.DoModal();

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]