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

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

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

408 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->Sep1->Name = L"Sep1";

this->Sep1->Size = System::Drawing::Size(6, 25);

//

//Label

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

this->Label->Size = System::Drawing::Size(34, 22); this->Label->Text = L"Name";

//tstbName

//

this->tstbName->Name = L"tstbName"; this->tstbName->Size = System::Drawing::Size(92, 25); this->tstbName->Text = L"Computer";

//

//toolStripContainer1

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

this->toolStripContainer1->ContentPanel->Size = System::Drawing::Size(300, 105);

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

this->toolStripContainer1->Size = System::Drawing::Size(300, 130); this->toolStripContainer1->TabIndex = 8;

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

//toolStripContainer1.TopToolStripPanel

//

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

//

// Form1

//

this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Inherit;

this->ClientSize = System::Drawing::Size(300, 129); this->Controls->Add(this->toolStripContainer1); this->Name = L"Form1";

this->Text = L"Emotional Tool Strip"; this->toolStrip->ResumeLayout(false); this->toolStrip->PerformLayout(); this->toolStripContainer1->ContentPanel->ResumeLayout(false); this->toolStripContainer1->ContentPanel->PerformLayout(); this->toolStripContainer1->TopToolStripPanel->ResumeLayout(false); this->toolStripContainer1->TopToolStripPanel->PerformLayout(); this->toolStripContainer1->ResumeLayout(false); this->toolStripContainer1->PerformLayout(); this->ResumeLayout(false);

}

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

409

#pragma endregion

private:

System::Void tsbn_Click(System::Object^ sender, System::EventArgs^ e)

{

this->lbOutput->Text = String::Format("{0} is {1}!", tstbName->Text, ((ToolStripButton^)sender)->ToolTipText);

}

};

}

The process for creating a ToolStrip within Visual Studio 2005 is relatively straightforward, once you know how to do it. The steps are as follows:

1.Add a ToolStripContainer as outlined earlier.

2.Drag and drop the ToolStrip from the Toolbox to the ToolStripPanel of choice within the Design view.

3.Within the ToolStrip’s Properties dialog box, click the ellipses button next to the Items property. This will bring up a dialog box similar to the one shown in Figure 10-6.

Figure 10-6. The Items Collection Editor dialog box

4.Select the appropriate ToolStrip item type from the drop-down list.

5.Click the Add button and then update the ToolStrip item’s properties as appropriate.

6.Repeat step 4 for all the items.

7.Click the OK button.

Figure 10-7 shows what ToolStripEx.exe looks like when you execute it.

410 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

Figure 10-7. The emotional toolbar

StatusStrip

The StatusStrip is an easy-to-use control whose purpose is to display status information to the user. You will find the status strip at the bottom of many Windows applications. The truth is, the placement of the status strip is only a well-accepted convention, as the StatusStrip supports being placed anywhere on the Windows Form.

I have already covered almost everything you need to know about the StatusStrip, as the StatusStrip is a child of the ToolStrip. The only difference that you probably have to worry about is that the StatusStrip provides the Boolean property SizeGrip, which the ToolStrip doesn’t. If you set SizeGrip to false, the SizeGrip disappears. The default is true. The SizeGrip, by the way, is that dotted triangle in the bottom corner that you use to resize the window.

Since the StatusStrip is a slightly augmented ToolStrip, anything you can do with a ToolStrip you can do with a StatusStrip. This means you can use all the same ToolStripItems, plus the

ToolStripProgressBar control. (You can use the ToolStripProgressBar control on the ToolStrip as well, but you rarely, if ever, see it there as it normally represents a status.)

In most cases, you will probably use only the ToolStripLabel, which allows you to place text and images in the status strip.

One property that you will use on a StatusStrip’s ToolStripLabel that you don’t use as frequently on a ToolStrip is the Spring property. This property tells the ToolStripLabel to fill up all unused spaces on the StatusStrip, in effect causing all other controls to be left or right justified based on whether the ToolStripLabel is on the left or right of that control. In the example that follows, that is how I right justify the two mouse coordinate ToolStripLabels.

Listing 10-6 shows the creation of the StatusStrip with three ToolStripLabels . The status information displayed is the mouse x, y location and the last mouse button pressed while within the

ContentPanel area of the ToolStripContainer.

Listing 10-6. Status Bar Display of x, y Coordinates

namespace

StatusStripEx {

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();

}

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

411

protected:

~Form1()

{

if (components)

{

delete components;

}

}

private:

System::Windows::Forms::ToolStripContainer^ tsContainer; System::Windows::Forms::StatusStrip^ statusStrip1; System::Windows::Forms::ToolStripStatusLabel^ statusButtons; System::Windows::Forms::ToolStripStatusLabel^ statusXCoord; System::Windows::Forms::ToolStripStatusLabel^ statusYCoord;

System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

void InitializeComponent(void)

{

this->tsContainer =

(gcnew System::Windows::Forms::ToolStripContainer()); this->statusStrip1 =

(gcnew System::Windows::Forms::StatusStrip()); this->statusButtons =

(gcnew System::Windows::Forms::ToolStripStatusLabel()); this->statusXCoord =

(gcnew System::Windows::Forms::ToolStripStatusLabel()); this->statusYCoord =

(gcnew System::Windows::Forms::ToolStripStatusLabel()); this->tsContainer->BottomToolStripPanel->SuspendLayout(); this->tsContainer->SuspendLayout(); this->statusStrip1->SuspendLayout();

this->SuspendLayout();

//

//tsContainer

//tsContainer.BottomToolStripPanel

this->tsContainer->BottomToolStripPanel->Controls->Add( this->statusStrip1);

//tsContainer.ContentPanel

//

this->tsContainer->ContentPanel->Size = System::Drawing::Size(292, 251);

this->tsContainer->ContentPanel->MouseDown +=

gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::tsContainer_ContentPanel_MouseDown);

412 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->tsContainer->ContentPanel->MouseMove +=

gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::tsContainer1_ContentPanel_MouseMove);

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

this->tsContainer->Size = System::Drawing::Size(292, 273); this->tsContainer->TabIndex = 0;

this->tsContainer->Text = L"toolStripContainer1";

//

//statusStrip1

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

gcnew cli::array< System::Windows::Forms::ToolStripItem^>(3) {this->statusButtons, this->statusXCoord, this->statusYCoord});

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

this->statusStrip1->Size = System::Drawing::Size(292, 22); this->statusStrip1->TabIndex = 0;

//statusButtons

//

this->statusButtons->Name = L"statusButtons"; this->statusButtons->Size = System::Drawing::Size(177, 17); this->statusButtons->Spring = true; this->statusButtons->TextAlign =

System::Drawing::ContentAlignment::MiddleLeft;

//

//statusXCoord

this->statusXCoord->AutoSize = false; this->statusXCoord->BorderSides =

static_cast<System::Windows::Forms::ToolStripStatusLabelBorderSides>

((((System::Windows::Forms::ToolStripStatusLabelBorderSides::Left

| System::Windows::Forms::ToolStripStatusLabelBorderSides::Top)

| System::Windows::Forms::ToolStripStatusLabelBorderSides::Right)

| System::Windows::Forms::ToolStripStatusLabelBorderSides::Bottom)); this->statusXCoord->BorderStyle =

System::Windows::Forms::Border3DStyle::Sunken; this->statusXCoord->Name = L"statusXCoord";

this->statusXCoord->Size = System::Drawing::Size(50, 17);

//statusYCoord

//

this->statusYCoord->AutoSize = false; this->statusYCoord->BorderSides = static_cast<System::Windows::Forms::ToolStripStatusLabelBorderSides>

((((System::Windows::Forms::ToolStripStatusLabelBorderSides::Left

| System::Windows::Forms::ToolStripStatusLabelBorderSides::Top)

| System::Windows::Forms::ToolStripStatusLabelBorderSides::Right)

| System::Windows::Forms::ToolStripStatusLabelBorderSides::Bottom));

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

413

this->statusYCoord->BorderStyle = System::Windows::Forms::Border3DStyle::Sunken;

this->statusYCoord->Name = L"statusYCoord"; this->statusYCoord->Size = System::Drawing::Size(50, 17);

//

// Form1

//

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

this->Name = L"Form1";

this->Text = L"Status Strip Mouse Tracking"; this->tsContainer->BottomToolStripPanel->ResumeLayout(false); this->tsContainer->BottomToolStripPanel->PerformLayout(); this->tsContainer->ResumeLayout(false); this->tsContainer->PerformLayout(); this->statusStrip1->ResumeLayout(false); this->statusStrip1->PerformLayout(); this->ResumeLayout(false);

}

#pragma endregion

private:

System::Void tsContainer_ContentPanel_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)

{

// clicked mouse button in first status bar panel

if (e->Button == System::Windows::Forms::MouseButtons::Right) statusButtons->Text = "Right";

else if (e->Button == System::Windows::Forms::MouseButtons::Left) statusButtons->Text = "Left";

else

statusButtons->Text = "Middle";

}

System::Void tsContainer1_ContentPanel_MouseMove(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)

{

// x,y coords in second and third status bar panels statusXCoord->Text = String::Format("X={0}", e->X); statusYCoord->Text = String::Format("Y={0}", e->Y);

}

};

}

Figure 10-8 shows what StatusBar.exe looks like when you execute it.

414 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

Figure 10-8. A three-panel status bar

MenuStrip and ContextMenuStrip

There are two types of Windows Forms menus in the .NET Framework, the MenuStrip or the main menu that you find (almost always at the very top of the Windows Form) on most applications and the ContextMenuStrip or a menu that pops up within the context of some other control, for example, when you right-click an item in the Solution Explorer in Visual Studio 2005.

There is very little difference between either of these menus, especially while developing them. Simply drag either the MenuStrip or ContextMenuStrip to the Design view from the Toolbox window and then build the menu the exact same way. The only two differences are that they use different constructors, and you need to assign a ContextMenuStrip to a control’s ContextMenuStrip property, while a MenuStrip is added to a RaftingContainer control.

Believe it or not, you have almost already learned everything you need to know about a MenuStrip or a ContextMenuStrip, as they are, like the StatusStrip, slightly enhanced ToolStrips. So slightly enhanced that I found no methods or properties worth mentioning.

Since the MenuStrip and the ContextMenuStrip are slightly augmented ToolStrips, anything you can do with a ToolStrip you can do with either the MenuStrip or the ContextMenuStrip. This means you can use all the same ToolStripItems, plus the ToolStripMenuItem. (You can use the ToolStripMenuItem control on the ToolStrip as well, but you rarely, if ever, see it there as it normally represents a menu item.)

By convention and in most cases because it only makes visual or logical sense, you use the following ToolStripItems on a MenuStrip or ContextMenuStrip:

ToolStripMenuItem is a menu item.

ToolStripComboBox is a combo box.

ToolStripSeparator is a separator.

ToolStripTextBox is a textbox.

Building a menu is very straightforward. Add ToolStripMenuItems to the MenuStrip or ContextMenuStrip. If you want a submenu for the current ToolStripMenuItem, then add ToolStripMenuItems to its DropDownItems collection property. If you want a different ToolStripItem type, then add that ToolStripItem type instead of the ToolStripMenuItem.

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

415

The ToolStripMenuItem is well suited for menu development as it includes probably every property or method you will need to add a menu item. Here is a list of the properties that you will most likely use:

Checked is a Boolean that represents whether a check mark appears next to the menu item. The default is false, which means it won’t display the check mark.

CheckOnClick a Boolean that represents whether the ToolStripMenuItem should automatically appear checked/unchecked when clicked.

CheckState is a CheckState enum indicating whether a ToolStripMenuItem is in the Checked, Unchecked, or Indeterminate state. The default is Unchecked.

DropDownItems is a ToolStripItemCollection of submenu items for the current ToolStripMenuItem.

Enabled is a Boolean that represents whether the menu item is enabled. The default is true, which means it can be accessed.

Image is an Image object that represents the image to display for the menu item.

ShortcutKeys is a Keys enum that represents the shortcut keystroke associated with the menu item. The default is Keys::None, which associates no shortcut.

ShowShortcutKeys is a Boolean that represents whether the shortcut key is displayed. The default is true.

Text is a String that represents the text to display for the menu item.

The ToolStripItem that first surprised me when I first saw it as a standard item for a menu was the ToolStripComboBox, but then once I thought about it, I came to realize it made sense. Real estate on a menu is pretty scarce, and the use of multiple mutually exclusive radio button menu items to select a single item can be quite a waste of space. With a ToolStripComboBox, you can select an appropriate mutually exclusive item from a large list and at the same time only use up one line on the menu. My conclusion was reinforced when I found out that there is a radio button check in the

ToolStripMenuItem.

Listing 10-7 shows the creation of a MenuStrip with an assortment of ToolStripMenuItems with different properties set. It also includes a ToolStripComboBox to show how you can use it to retrieve a single value from a mutually exclusive list.

Listing 10-7. Simple Assorted Menu

namespace

SimpleMenu

{

 

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();

}

416 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

protected:

~Form1()

{

if (components)

{

delete components;

}

}

private:

System::Windows::Forms::ToolStripContainer^ toolStripContainer1; System::Windows::Forms::MenuStrip^ mainMenuStrip; System::Windows::Forms::ToolStripMenuItem^ miFile; System::Windows::Forms::ToolStripMenuItem^ miFileSub; System::Windows::Forms::ToolStripComboBox^ miFileSubThis; System::Windows::Forms::ToolStripMenuItem^ miFileExit; System::Windows::Forms::ToolStripMenuItem^ miFileSubCheck; System::Windows::Forms::ToolStripMenuItem^ miFileSubImage; System::Windows::Forms::ToolStripMenuItem^ miFileSubSayBoo; System::Windows::Forms::ToolStripMenuItem^ miHelp; System::Windows::Forms::ToolStripMenuItem^ miHelpAbout; System::Windows::Forms::ToolStripSeparator^ miFileSep1; System::ComponentModel::IContainer^ components;

#pragma region Windows Form Designer generated code void InitializeComponent(void)

{

System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));

this->toolStripContainer1 =

(gcnew System::Windows::Forms::ToolStripContainer()); this->mainMenuStrip = (gcnew System::Windows::Forms::MenuStrip()); this->miFile = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->miFileSub =

(gcnew System::Windows::Forms::ToolStripMenuItem()); this->miFileSubThis =

(gcnew System::Windows::Forms::ToolStripComboBox()); this->miFileSubCheck =

(gcnew System::Windows::Forms::ToolStripMenuItem()); this->miFileSubImage =

(gcnew System::Windows::Forms::ToolStripMenuItem()); this->miFileSubSayBoo =

(gcnew System::Windows::Forms::ToolStripMenuItem()); this->miFileSep1 =

(gcnew System::Windows::Forms::ToolStripSeparator()); this->miFileExit =

(gcnew System::Windows::Forms::ToolStripMenuItem()); this->miHelp =

(gcnew System::Windows::Forms::ToolStripMenuItem()); this->miHelpAbout =

(gcnew System::Windows::Forms::ToolStripMenuItem()); this->toolStripContainer1->TopToolStripPanel->SuspendLayout();

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

417

this->toolStripContainer1->SuspendLayout(); this->mainMenuStrip->SuspendLayout(); this->SuspendLayout();

//

//toolStripContainer1

//toolStripContainer1.ContentPanel

this->toolStripContainer1->ContentPanel->Size = System::Drawing::Size(292, 249);

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

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

this->toolStripContainer1->Size = System::Drawing::Size(292, 273); this->toolStripContainer1->TabIndex = 0;

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

//toolStripContainer1.TopToolStripPanel

//

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

//

//mainMenuStrip

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

gcnew cli::array< System::Windows::Forms::ToolStripItem^>(2) {this->miFile, this->miHelp});

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

this->mainMenuStrip->Size = System::Drawing::Size(292, 24); this->mainMenuStrip->TabIndex = 0;

this->mainMenuStrip->Text = L"menuStrip1";

//miFile

//

this->miFile->DropDownItems->AddRange(

gcnew cli::array< System::Windows::Forms::ToolStripItem^>(3) {this->miFileSub, this->miFileSep1, this->miFileExit});

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

this->miFile->Size = System::Drawing::Size(35, 20); this->miFile->Text = L"&File";

//

// miFileSub

//

this->miFileSub->DropDownItems->AddRange(

gcnew cli::array< System::Windows::Forms::ToolStripItem^>(4) {this->miFileSubThis, this->miFileSubCheck,

this->miFileSubImage, this->miFileSubSayBoo}); this->miFileSub->Name = L"miFileSub"; this->miFileSub->Size = System::Drawing::Size(152, 22); this->miFileSub->Text = L"&Sub";

//