Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Pro Visual C++-CLI And The .NET 2.0 Platform (2006) [eng]-1

.pdf
Скачиваний:
70
Добавлен:
16.08.2013
Размер:
24.18 Mб
Скачать

398 C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

#pragma endregion };

}

The best part about TabControls is that you don’t have to know anything about them because Visual Studio 2005’s design GUI tool can handle everything for you. The only real issue about TabControls is that there is no TabPage control in the Toolbox view to drag to the TabControl. Instead, to add a TabPage, you need to add it to the TabPages collection property within the TabControl’s Properties view.

I think the generated code is pretty self-explanatory. You add the TabPage to the TabControl, add the Label to a TabPage, and finally add the TabControl to the Form.

Figure 10-4 shows what TabControlEx.exe looks like when you execute it. Unfortunately, you can’t see it in action in this still image.

Figure 10-4. A simple TabControl

SplitContainer

The SplitContainer is a simple little control that takes two panels and allows you to resize them at runtime using a little area located between the panels known as the splitter. You can spot this area between the panels not only because the area is (normally) a different color than the panels, but also because the cursor changes automatically into what Windows calls a VSplit or HSplit cursor, depending on whether you use a vertical or horizontal SplitContainer.

The SplitContainer is a vast improvement over its predecessor, the Splitter control. The largest improvement in my mind is that with the Splitter you were required to do several elaborate steps to get it configured. Now, with the SplitContainer, you simply drag the control onto the Design view, and then using the Orientation property, you specify whether the container will be split vertically or horizontally. Most likely you will also use the dock property to fill the Window Form or the container control that you place it in, but you don’t have to, as Listing 10-4 points out.

The following are some SplitContainer properties that you might work with:

FixedPanel specifies that a panel stays a fixed size when a resize event occurs. The default is none, which causes the two panels that make up the control to stay proportionally the same.

Panel1 is the left or top Panel control depending on the type of split.

Panel1Collapsed is a Boolean value that allows you to make the Panel1 completely collapse when set to true. When collapsed, there is no way to resize the control; you must programmatically set the value back to false to allow the control to resize again.

Panel2 is the right or bottom Panel control depending on the type of split.

Panel2Collapsed is a Boolean value that allows you to make the Panel2 completely collapse when set to true. When collapsed, there is no way to resize the control; you must programmatically set the value back to false to allow the control to resize again.

C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

399

SplitterDistance is an Int32 value of the number of pixels from the left or top where the split occurs.

SplitterWidth is an Int32 value of the size between the two panels.

In the example, I fill the SplitContainer control’s Panel1 and Panel2 properties with TextBox controls, though this is not necessary. You can use the SplitContainer control’s Panel properties just like you would a standard Panel control.

Listing 10-4 shows the SplitContainer being used twice. The first time I split the entire Window Form vertically using a green background. The second time I split horizontally a small portion of the Right panel using a red background.

Listing 10-4. The SplitContainer Control

#pragma once

namespace

SplitContainerEx

{

 

using

namespace System;

using

namespace System::ComponentModel;

using

namespace System::Collections;

using

namespace System::Windows::Forms;

using

namespace System::Data;

using

namespace System::Drawing;

public ref class Form1 : public System::Windows::Forms::Form

{

public:

Form1(void)

{

InitializeComponent();

}

protected:

~Form1()

{

if (components)

{

delete components;

}

}

private:

System::Windows::Forms::SplitContainer^ splitContainer1; System::Windows::Forms::TextBox^ textBox1; System::Windows::Forms::SplitContainer^ splitContainer2; System::Windows::Forms::TextBox^ textBox2; System::Windows::Forms::TextBox^ textBox3;

System::ComponentModel::Container ^components;

400 C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

#pragma region Windows Form Designer generated code

void InitializeComponent(void)

{

this->splitContainer1 =

(gcnew System::Windows::Forms::SplitContainer()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->splitContainer2 =

(gcnew System::Windows::Forms::SplitContainer()); this->textBox2 = (gcnew System::Windows::Forms::TextBox()); this->textBox3 = (gcnew System::Windows::Forms::TextBox()); this->splitContainer1->Panel1->SuspendLayout(); this->splitContainer1->Panel2->SuspendLayout(); this->splitContainer1->SuspendLayout(); this->splitContainer2->Panel1->SuspendLayout(); this->splitContainer2->Panel2->SuspendLayout(); this->splitContainer2->SuspendLayout(); this->SuspendLayout();

//

//splitContainer1

this->splitContainer1->BackColor = System::Drawing::Color::Green; this->splitContainer1->Dock =

System::Windows::Forms::DockStyle::Fill; this->splitContainer1->Location = System::Drawing::Point(0, 0);

this->splitContainer1->Name = L"splitContainer1";

//splitContainer1.Panel1

//

this->splitContainer1->Panel1->Controls->Add(this->textBox1);

//

// splitContainer1.Panel2

//

this->splitContainer1->Panel2->Controls->Add(this->splitContainer2); this->splitContainer1->Size = System::Drawing::Size(292, 273); this->splitContainer1->SplitterDistance = 116; this->splitContainer1->TabIndex = 1;

this->splitContainer1->Text = L"splitContainer1";

//

// textBox1

//

this->textBox1->AutoSize = false; this->textBox1->BorderStyle =

System::Windows::Forms::BorderStyle::None; this->textBox1->Dock = System::Windows::Forms::DockStyle::Fill; this->textBox1->Location = System::Drawing::Point(0, 0); this->textBox1->Name = L"textBox1";

this->textBox1->Size = System::Drawing::Size(116, 273); this->textBox1->TabIndex = 0;

this->textBox1->Text = L"Left Textbox"; this->textBox1->TextAlign =

System::Windows::Forms::HorizontalAlignment::Center;

C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

401

//

//splitContainer2

this->splitContainer2->BackColor = System::Drawing::Color::Red; this->splitContainer2->Location = System::Drawing::Point(18, 82);

this->splitContainer2->Name = L"splitContainer2"; this->splitContainer2->Orientation =

System::Windows::Forms::Orientation::Horizontal;

//splitContainer2.Panel1

//

this->splitContainer2->Panel1->Controls->Add(this->textBox2);

//

//splitContainer2.Panel2

this->splitContainer2->Panel2->Controls->Add(this->textBox3); this->splitContainer2->Size = System::Drawing::Size(132, 102);

this->splitContainer2->SplitterDistance = 42; this->splitContainer2->TabIndex = 0;

this->splitContainer2->Text = L"splitContainer2";

//textBox2

//

this->textBox2->AutoSize = false; this->textBox2->BorderStyle =

System::Windows::Forms::BorderStyle::None; this->textBox2->Dock = System::Windows::Forms::DockStyle::Fill; this->textBox2->Location = System::Drawing::Point(0, 0); this->textBox2->Name = L"textBox2";

this->textBox2->Size = System::Drawing::Size(132, 42); this->textBox2->TabIndex = 0;

this->textBox2->Text = L"Top Right Textbox"; this->textBox2->TextAlign =

System::Windows::Forms::HorizontalAlignment::Center;

//

//textBox3

this->textBox3->AutoSize = false; this->textBox3->BorderStyle =

System::Windows::Forms::BorderStyle::None; this->textBox3->Dock = System::Windows::Forms::DockStyle::Fill;

this->textBox3->Location = System::Drawing::Point(0, 0); this->textBox3->Name = L"textBox3";

this->textBox3->Size = System::Drawing::Size(132, 56); this->textBox3->TabIndex = 0;

this->textBox3->Text = L"Bottom Right Textbox"; this->textBox3->TextAlign =

System::Windows::Forms::HorizontalAlignment::Center;

//Form1

//

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(292, 273);

402 C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

this->Controls->Add(this->splitContainer1); this->Name = L"Form1";

this->Text = L"Form1"; this->splitContainer1->Panel1->ResumeLayout(false); this->splitContainer1->Panel2->ResumeLayout(false); this->splitContainer1->ResumeLayout(false); this->splitContainer2->Panel1->ResumeLayout(false); this->splitContainer2->Panel2->ResumeLayout(false); this->splitContainer2->ResumeLayout(false); this->ResumeLayout(false);

}

#pragma endregion };

}

Figure 10-5 shows what SplitControlEx.exe looks like when you execute it.

Figure 10-5. A simple pair of SplitContainer controls

Strips

If you have worked with prior versions of Windows Forms, you may have noticed that your toolbars, status bar, and menus bore little resemblance to and lacked much of the functionality of those you found on many of Microsoft’s applications and tools. To fix this (oversight?), several third-party controls were developed. However, with .NET version 2.0 Web Forms, many of these third-party controls may have become obsolete due to the new strip controls—ToolStrip, StatusStrip, and

MenuStrip.

ToolStripContainer and ToolStripPanel

In previous versions of Windows Forms, there was little flexibility to how and where you displayed your main menu, status bar, and toolbars (I’ll just call them strip controls from here on). You had no options to drag your strip controls to different sides of your form or display multiple strip controls together. This has now been changed with the addition of the ToolStripContainer control.

C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

403

The ToolStripContainer control is where you place your strip controls if you want them aligned along the border. More accurately, any one of the following (aptly named) four ToolStripPanel properties is where you place your strip controls:

TopToolStripPanel

BottomToolStripPanel

LeftToolStripPanel

RightToolStripPanel

A fifth panel, the ContentPanel, fills the center area between all the ToolStripPanels. You use this area as the home for all the content of your form.

Before you add your strip control within Visual Studio 2005, you first add a ToolStripContainer just like you would any other control. Once added, you dock fill it to the form so that the four ToolStripPanels align with the sides of the form. Next, you expand the ToolStripPanel where you want to place your strip. Finally, you drag and drop your strip to the expanded ToolStripPanel.

One nice feature that the ToolStripPanel control provides is the ability to display multiple strip controls together. The ToolStripPanel is a container control that provides flow layout functionality. As you add strip controls to the ToolStripPanel control, they are arranged from left to right in the case of a ToolStripPanel control docked to the top or bottom, and from top to bottom in the case of a ToolStripPanel control docked to the left or right side. When you reach the edge of the ToolStripPanel, additional controls flow to the next row.

There’s not much to the coding of ToolStripContainer controls. You declare it as follows:

System::Windows::Forms::ToolStripContainer^ toolStripContainer1;

You create instances of it like so:

this->toolStripContainer1 = gcnew system::Windows::Forms::ToolStripContainer();

You use the following to dock it to a Windows Form border (and optionally set a few of its properties):

this->toolStripContainer1->Dock = System::Windows::Forms::DockStyle::Fill;

To add it to the Windows Form:

this->Controls->Add(this->toolStripContainer1);

And finally, to add strip controls to the appropriate ToolStripPanel using any combination of the following statements:

this->toolStripContainer1->TopToolStripPanel->Controls->Add(this->Strip1); this->toolStripContainer1->BottomToolStripPanel->Controls->Add(this->Strip1); this->toolStripContainer1->LeftToolStripPanel->Controls->Add(this->Strip1); this->toolStripContainer1->RightToolStripPanel->Controls->Add(this->Strip1);

By the way, you can add strips without using the ToolStripContainer, but then you lose the ability to move the strips around.

ToolStripManager

The ToolStripManager class is made up of several static properties and methods that you use to control the arrangement, rendering, and display style of strip controls. You can, in most cases, just use the defaults and ignore the ToolStripManager class completely, but if you find you have the need, the following three properties are available to specify a renderer and display styles:

404 C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

Renderer is a ToolStripRenderer object that specifies the default painting styles for the Windows Form.

RenderMode is a ToolStripManagerRenderMode enum specifying whether the System (flat style with system colors) or Professional (custom palette and a streamlined style) mode will be used.

VisualStylesEnabled is a Boolean that represents whether rendered is done using themes.

The main reason I use the ToolStripManager class is to merge two strip controls together and then later, when I no longer need the controls to be merged, undo the merge. The merging of two strip controls is done with the aptly named method Merge(), while you undo or revert the merge using the RevertMerge() method.

ToolStrip

Most Windows applications have a tool strip. Many like Microsoft Word have more than one, often all visible at the same time. In this section, you’ll learn how to implement ToolStrip controls using the .NET Framework class library.

The ToolStrip has improved considerably over its predecessor the ToolBar. It provides a lot more functionality with a much cleaner interface. With all this extra functionality, the ToolStrip control is a little more complex than the ToolBar, but the design tool is intuitive, which evens things out.

I guess I could have placed this ToolStrip discussion in with the “Container Controls” section as the ToolStrip is in fact a container. However, unlike the other containers, it can contain only controls derived from the ToolStripItem class. This isn’t really an issue, as you can place standard controls within the ToolStripControlHost control and then place them on the ToolStrip. The .NET Framework supports several ToolStripItems out of the box. The following are the most common ones you might use:

ToolStripButton is a selectable button that can contain text and images.

ToolStripComboBox is a combo box.

ToolStripSplitButton is a combination of a standard button on the left and a drop-down button on the right.

ToolStripLabel is a nonselectable item that displays text, images, and hyperlinks.

ToolStripSeparator is a separator.

ToolStripDropDownButton is a control that, when clicked, displays an associated list of buttons from which the user can select a single item.

ToolStripTextBox is a text box.

The ToolStrip control has a few overall tool strip configuration properties. These properties work in conjunction with the preceding ToolStripItems to get the final look and feel of the tool strip. Here are some of the more commonly used ToolStrip properties:

AllowItemReorder is a Boolean value indicating whether the ToolStrip will allow and handle by itself drag-and-drop and item reordering. The default value is false.

AllowMerge is a Boolean value indicating whether multiple MenuStrip, ToolStripDropDownMenu,

ToolStripMenuItem, and other types can be combined. The default is false.

CanOverflow is a Boolean value indicating whether items in the ToolStrip can be sent to an overflow menu. The default is true.

GripStyle is a ToolStripGripStyle enum value of either Visible or Hidden. The default is Visible.

C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

405

ImageList is a collection of bitmaps, icons, and metafiles that will be used to display the images on the ToolStrip. The default is nullptr or no image list.

OverflowButton is the ToolStripItem that is the overflow button for a ToolStrip with CanOverflow equal to true.

RenderMode is a ToolStripRenderMode enum that specifies the tool strip renderer to use. You will most likely use ManagerRenderMode, which uses the renderer specified by the ToolStripManager, but you can also specifically select System (flat style with system colors) or Professional (custom palette and a streamlined style).

ShowItemToolTips is a Boolean that represents whether tool tips are displayed for all ToolStripItems when the mouse passes over them. The default is false.

The ToolStripItem class provides a number of common properties used to configure the tool strip items themselves. Here are some of the more common properties:

AutoToolTip is a Boolean value indicating whether to use the Text property or the ToolTipText property for the ToolStripItem tool tip. The default is true, meaning the Text property is used.

DisplayStyle is a ToolStripGripStyle enum value indicating whether Image, ImageAndText,

None, or Text is displayed. The default is ImageAndText.

ImageIndex is a zero-based Int32 index to the ToolStrip::ImageList associated with the current ToolStripItem that represents the position of the image for the button. The default is –1, or no image will appear on the button.

ImageScaling is a Boolean value indicating whether the image automatically resizes to fit in a container.

Pressed is a Boolean that represents whether the item is pressed.

Selected is a Boolean that represents whether the item is selected.

Text is a String that represents the text displayed on the button.

TextImageRelation is a TextImageRelation enum value indicating the relationship between its text and image. Possible values are ImageAboveText, ImageBeforeText, Overlay, TextAboveImage, or TextBeforeImage. The default is ImageBeforeText.

ToolTipText is a String that appears in the ToolTip control associated with the item.

The code in Listing 10-5 builds a tool strip with two ToolStripButtons: a happy face and a sad face, ToolStripLabel and a ToolStripTextBox. When you click either of the buttons, the label in the body of the form is updated with a combination of the ToolStripTextBox’s Text property and

ToolTipText of the ToolStripButton inherited from the ToolStripItem.

Listing 10-5. An Emotional Tool Strip

namespace

ToolStripEx

{

 

using

namespace System;

using

namespace System::ComponentModel;

using

namespace System::Collections;

using

namespace System::Windows::Forms;

using

namespace System::Data;

using

namespace System::Drawing;

406 C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

public ref class Form1 : public System::Windows::Forms::Form

{

public:

Form1(void)

{

InitializeComponent();

}

protected:

~Form1()

{

if (components)

{

delete components;

}

}

private:

System::Windows::Forms::Label^ lbOutput; System::Windows::Forms::ToolStrip^ toolStrip; System::Windows::Forms::ToolStripButton^ tsbnHappy; System::Windows::Forms::ToolStripButton^ tsbnSad; System::Windows::Forms::ToolStripSeparator^ Sep1; System::Windows::Forms::ToolStripLabel^ Label; System::Windows::Forms::ToolStripTextBox^ tstbName; System::Windows::Forms::ToolStripContainer^ toolStripContainer1;

System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

void InitializeComponent(void)

{

System::ComponentModel::ComponentResourceManager^ Resources =

(gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid)); this->lbOutput = (gcnew System::Windows::Forms::Label()); this->toolStrip = (gcnew System::Windows::Forms::ToolStrip()); this->tsbnHappy = (gcnew System::Windows::Forms::ToolStripButton()); this->tsbnSad = (gcnew System::Windows::Forms::ToolStripButton()); this->Sep1 = (gcnew System::Windows::Forms::ToolStripSeparator()); this->Label = (gcnew System::Windows::Forms::ToolStripLabel()); this->tstbName = (gcnew System::Windows::Forms::ToolStripTextBox()); this->toolStripContainer1 =

(gcnew System::Windows::Forms::ToolStripContainer()); this->toolStrip->SuspendLayout(); this->toolStripContainer1->ContentPanel->SuspendLayout(); this->toolStripContainer1->TopToolStripPanel->SuspendLayout(); this->toolStripContainer1->SuspendLayout(); this->SuspendLayout();

//

// lbOutput

C H A P T E R 1 0 A D V A N C E D W I N D O W S F O R M S A P P L I C A T I O N S

407

//

this->lbOutput->AutoSize = true; this->lbOutput->Font =

(gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0)));

this->lbOutput->Location = System::Drawing::Point(47, 42); this->lbOutput->Name = L"lbOutput";

this->lbOutput->Size = System::Drawing::Size(208, 13); this->lbOutput->TabIndex = 7;

this->lbOutput->Text = L"Enter a name then click an emotion";

//

// toolStrip

//

this->toolStrip->Dock = System::Windows::Forms::DockStyle::None; this->toolStrip->Items->AddRange(

gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(5)

{this->tsbnHappy, this->tsbnSad, this->Sep1, this->Label, this->tstbName});

this->toolStrip->Location = System::Drawing::Point(0, 0); this->toolStrip->Name = L"toolStrip"; this->toolStrip->Size = System::Drawing::Size(300, 25); this->toolStrip->Stretch = true; this->toolStrip->TabIndex = 6;

this->toolStrip->Text = L"toolStrip1";

//

//tsbnHappy

this->tsbnHappy->Image = (cli::safe_cast<System::Drawing::Image^>

(resources->GetObject(L"tsbnHappy.Image"))); this->tsbnHappy->Name = L"tsbnHappy";

this->tsbnHappy->Size = System::Drawing::Size(58, 22); this->tsbnHappy->Text = L"Happy";

this->tsbnHappy->ToolTipText = L"a happy camper"; this->tsbnHappy->Click +=

gcnew System::EventHandler(this, &Form1::tsbn_Click);

//tsbnSad

//

this->tsbnSad->Image = (cli::safe_cast<System::Drawing::Image^>

(resources->GetObject(L"tsbnSad.Image"))); this->tsbnSad->Name = L"tsbnSad"; this->tsbnSad->Size = System::Drawing::Size(45, 22); this->tsbnSad->Text = L"Sad"; this->tsbnSad->ToolTipText = L"major gloomy"; this->tsbnSad->Click +=

gcnew System::EventHandler(this, &Form1::tsbn_Click);

//

// Sep1