Practical
Learning: Creating the Application |
|
- Start a new Windows Forms Application named PictureViewer3
- Change the properties of the form as follows:
Text: Picture Viewer
StartPosition: CenterScreen
- You can copy the following pictures and add them to a folder on your
computer
- In the Dialogs section of the Toolbox, click the OpenFileDialog
button
and click the form
- Change its characteristics as follows:
Title: Open a Picture
DefaultExt: bmp
Filter: Bitmap Files (*.bmp)|*.bmp|JPEG Files (*.jpg,*.jpeg)|*.jpg|GIF Files (*.gif)|*.gif|PNG Files (*.png)|*.png
(Name): ofdPicture
- Right-click the form and click View Code
- Declare a private Bitmap variable named bmpPicture
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
Bitmap ^ bmpPicture;
|
- In the Menus & Toolbars section of the Toolbox, click the MenuStrip button
and click the form
- While the menu strip is still selected, in the Properties window,
click (Name) and type mnuMain
- In the Common Controls section of the Toolbox, click PictureBox and
click the form
- While the picture box is still selected, in the Properties window,
change its characteristics as follows:
BorderStyle: Fixed3D
(Name): pbxViewer
Dock: Fill
- On top of the Properties window, click the arrow of the combo box
and select Form1
- Click the Events button
and double-click Paint
- Implement the event as follows:
System::Void Form1_Paint(System::Object^ sender,
System::Windows::Forms::PaintEventArgs^ e)
{
if( bmpPicture != nullptr )
{
pbxViewer->Image = bmpPicture;
}
}
|
- Display the form
- Under the form, click mnuMain
- In the Properties window, click Items and click its ellipsis button
- In the Items Collection Editor, make sure MenuItem is selected in
the Select Item And Add To List Below combo box and click Add
- While toolStripMenuItem1 is selected in the Members combo box, in
the right list, change the following characteristics:
Text: &File
(Name): mnuFile
- Still in the right list, click DropDownItems and click its ellipsis
button
- In the Items Collection Editor, make sure MenuItem is selected in
the Select Item And Add To List Below combo box and click Add
- While toolStripMenuItem1 is selected in the Members combo box, in
the right list, change the following characteristics:
Text: &Open
(Name): mnuFileOpen
- In the Items Collection Editor (mnuFile.DropDownItems), click OK
- In the Items Collection Editor, in the Select Item And Add To List
Below combo box, make sure MenuItem is selected in the and click
Add
- While toolStripMenuItem1 is selected in the Members combo box, in
the right list, change the following characteristics:
Text: &Tools
(Name): mnuTools
- Still in the right list, click DropDownItems and click its ellipsis
button
- In the Items Collection Editor, make sure MenuItem is selected in
the Select Item And Add To List Below combo box and click Add
- While toolStripMenuItem1 is selected in the Members combo box, in
the right list, change the following characteristics:
Text: &Flip
(Name): mnuToolsFlip
- In the Select Item And Add To List Below combo box, make sure
MenuItem is selected in the and click Add
- While toolStripMenuItem1 is selected in the Members combo box, in
the right list, change the following characteristics:
Text: &Mirror
(Name): mnuToolsMirror
- In the Items Collection Editor (mnuTools.DropDownItems), click OK
- In the Items Collection Editor, click OK
- Under the form, click mnuMain.
On the form, click File and click Open
- In the Properties window, click ShortcutKeys and click the
arrow of its combo box
- In the window that appears, click the Ctrl check box
- Click the arrow of the combo box next to Reset, scroll down and
select O
- On the form, click Tools
- In the Properties window, click DropDownItems and click its ellipsis
button
- In the Members list of the Items Collection Editor, click Tools
- On the right side, click DropDownItems and click its ellipsis button
- In the Members list, click mnuToolsFlip
- In the right list, click ShortcutKeys and click the arrow of
its combo box
- In the window that appears, click the Ctrl check box
- Click the arrow of the combo box next to Reset, scroll down and
select L
- In the Members list, click mnuToolsMirror
- In the right list, click ShortcutKeys, type Ctrl+M and
press Enter
- In the Items Collection Editor (mnuTools.DropDownItems), click OK
- In the Items Collection Editor, click OK
- On the form, click Tools and double-click Flip
- Implement the event as follows:
System::Void mnuToolsFlip_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( bmpPicture != nullptr )
{
bmpPicture->RotateFlip(RotateFlipType::RotateNoneFlipY);
Invalidate();
}
}
|
- Return to the form, click Tools and double-click Mirror
- Implement the event as follows:
System::Void mnuToolsMirror_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( bmpPicture != nullptr )
{
bmpPicture->RotateFlip(RotateFlipType::RotateNoneFlipX);
Invalidate();
}
}
|
- Save all
- Under the from, click mnuMain and, on the form, click File
- On the form, click Open and, in the Properties window, change its Text to &Open...
- On the form, double-click Open... and implement its event as follows:
System::Void mnuFileOpen_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( ofdPicture->ShowDialog() ==
System::Windows::Forms::DialogResult::OK )
{
bmpPicture = gcnew Bitmap(ofdPicture->FileName);
Invalidate();
}
}
|
- Execute the application
- Try opening a picture and mirror it
- Close the form and return to your programming environment
- Under the form, click mnuMain and, on the form, click File
- Under Open, click Type Here, type - and press Enter
- Under the new line, click Type Here and type Exit and press
Enter
- On the form, click Exit
- In the Properties window, change its characteristics as follows:
(Name): mnuFileExit
ShortcutKeys: Ctrl+F4
ShowShortcutKeys: false
- On the form, click File and double-click Exit
- Implement the event as follows:
System::Void mnuFileExit_Click(System::Object^ sender,
System::EventArgs^ e)
{
Close();
}
|
- Display the form
- Under the form, click mnuMain and, on the form, click Tools
- Under Mirror, click Type Here, type &Rotate and press Enter
- On the form, click Rotate
- On the right side of Rotate, click Type Here, type &90° and
press Enter
- Continue creating the sub-menus and menu items as follows:
- Click each menu item and change their names as follows:
Menu Item |
Sub-Menu Item |
Sub-Menu Item |
Name |
Rotate |
|
|
mnuToolsRotate |
|
90º |
|
mnuToolsRotate90 |
|
|
Left |
mnuToolsRotate90Left |
|
|
Right |
mnuToolsRotate90Right |
|
180º |
|
mnuToolsRotate180 |
Flip & Rotate |
|
|
mnuToolsFlipRotate |
|
90º |
|
mnuToolsFlipRotate90 |
|
|
Left |
mnuToolsFlipRotate90Left |
|
|
Right |
mnuToolsFlipRotate90Right |
|
180º |
|
mnuToolsFlipRotate180 |
- On the form and in the menu designer, click Tools, then click Rotate, then
click 90º and double-click Left
- Implement the event as follows:
System::Void mnuToolsRotate90Left_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( bmpPicture != nullptr )
{
bmpPicture->RotateFlip(RotateFlipType::Rotate270FlipNone);
Invalidate();
}
}
|
- Return to the form
- On the form and in the menu designer, click Tools, then click Rotate, then
click 90º and double-click Right
- Implement the event as follows:
System::Void mnuToolsRotate90Right_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( bmpPicture != nullptr )
{
bmpPicture->RotateFlip(RotateFlipType::Rotate90FlipNone);
Invalidate();
}
}
|
- Return to the form
- On the form and in the menu designer, click Tools, then click Rotate, and
double-click 180º
- Implement the event as follows:
System::Void mnuToolsRotate180_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( bmpPicture != nullptr )
{
bmpPicture->RotateFlip(RotateFlipType::Rotate180FlipNone);
Invalidate();
}
}
|
- Return to the form
- On the form and in the menu designer, click Tools, then click Flip &
Rotate, then click 90º and double-click Left
- Implement the event as follows:
System::Void mnuToolsFlipRotate90Left_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( bmpPicture != nullptr )
{
bmpPicture->RotateFlip(RotateFlipType::Rotate270FlipX);
Invalidate();
}
}
|
- Return to the form
- On the form and in the menu designer, click Tools, then click Flip &
Rotate, then click 90º and double-click Right
- Implement the event as follows:
System::Void mnuToolsFlipRotate90Right_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( bmpPicture != nullptr )
{
bmpPicture->RotateFlip(RotateFlipType::Rotate90FlipX);
Invalidate();
}
}
|
- Return to the form
- On the form and in the menu designer, click Tools, then click Flip &
Rotate, and double-click 180º
- Implement the event as follows:
System::Void mnuToolsFlipRotate180_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( bmpPicture != nullptr )
{
bmpPicture->RotateFlip(RotateFlipType::Rotate180FlipX);
Invalidate();
}
}
|
- Execute the application to test it
- Try opening a picture
- On the main menu, click View -> Other Windows -> Resource View
In the Resource View, right-click the name of the project -> Add ->
Resource...
- In the Add Resource dialog box, click Icon and click New
- Right-click the icon and click New Image Type
- In the New Icon Image Type dialog box, make sure 16x16, 16 colors is
selected and click OK
In the Resource View, click IDI_ICON1 and, in the Properties window,
change the following characteristics:
Filename: openpicture.ico
ID: IDI_OPENPICTURE
- Design the icon as follows:
- In the Resource View, right-click the name of the project -> Add ->
Resource...
- In the Add Resource dialog box, double-click Icon
- In the Resource View, click IDI_ICON1 and, in the Properties window,
change the following characteristics:
Filename: flip.ico
ID: IDI_FLIP
- Design the icon as follows:
- Save and close the icon the design
- In the Resource View, right-click the name of the project -> Add ->
Resource...
- In the Add Resource dialog box, double-click Icon
- In the Resource View, click IDI_ICON1 and, in the Properties window,
change the following characteristics:
Filename: mirror.ico
ID: IDI_MIRROR
- Design the icon as follows:
- Save and close the icon the design
- In the Resource View, right-click one of the icons and click Insert Icon
- Right-click the icon designer and click New Image Type
- In the New Icon Image Type, make sure 16x16, 16 colors is selected and
click OK
- In the Add Resource dialog box, double-click the new icon
- In the Resource View, click IDI_ICON1 and, in the Properties window,
change the following characteristics:
Filename: rotate90right.ico
ID: IDI_ROTATE90RIGHT
- Design the icon as follows:
- Save and close the icon the design
- In the Resource View, right-click one of the icons and click Insert Icon
- Right-click the icon designer and click New Image Type
- In the New Icon Image Type, make sure 16x16, 16 colors is selected and
click OK
- In the Add Resource dialog box, double-click the new icon
- In the Resource View, click IDI_ICON1 and, in the Properties window,
change the following characteristics:
Filename: rotate90left.ico
ID: IDI_ROTATE90LEFT
- Design the icon as follows:
- Save and close the icon the design
- In the Resource View, right-click one of the icons and click Insert Icon
- Right-click the icon designer and click New Image Type
- In the New Icon Image Type, make sure 16x16, 16 colors is selected and
click OK
- In the Add Resource dialog box, double-click the new icon
- In the Resource View, click IDI_ICON1 and, in the Properties window,
change the following characteristics:
Filename: rotate180.ico
ID: IDI_ROTATE180
- Design the icon as follows:
- Save and close the icon the design
- In the Resource View, right-click one of the icons and click Insert Icon
- Right-click the icon designer and click New Image Type
- In the New Icon Image Type, make sure 16x16, 16 colors is selected and
click OK
- In the Add Resource dialog box, double-click the new icon
- In the Resource View, click IDI_ICON1 and, in the Properties window,
change the following characteristics:
Filename: fliprotate90right.ico
ID: IDI_FLIPROTATE90RIGHT
- Design the icon as follows:
- Save and close the icon the design
- In the Resource View, right-click one of the icons and click Insert Icon
- Right-click the icon designer and click New Image Type
- In the New Icon Image Type, make sure 16x16, 16 colors is selected and
click OK
- In the Add Resource dialog box, double-click the new icon
- In the Resource View, click IDI_ICON1 and, in the Properties window,
change the following characteristics:
Filename: fliprotate90left.ico
ID: IDI_FLIPROTATE90LEFT
- Design the icon as follows:
- Save and close the icon the design
- In the Resource View, right-click one of the icons and click Insert Icon
- Right-click the icon designer and click New Image Type
- In the New Icon Image Type, make sure 16x16, 16 colors is selected and
click OK
- In the Add Resource dialog box, double-click the new icon
- In the Resource View, click IDI_ICON1 and, in the Properties window,
change the following characteristics:
Filename: fliprotate180.ico
ID: IDI_FLIPROTATE180
- Design the icon as follows:
- Save and close the icon the design
- Display the form
- Under the form, click mnuMain.
On the form, click File and click Open
- In the Properties window, click Image and click its ellipsis button
- Locate the openpicture.ico you designed and select it
- In the same way, associate the pictures to the menu items as follows:
Sub-Menu |
Icon Name |
File -> Open |
openpicture.ico |
Tools -> Flip |
flip.ico |
Tools -> Mirrow |
rotate.ico |
Tools -> Rotate -> 90º -> Left |
rotate90left.ico |
Tools -> Rotate -> 90º -> Right |
rotate90right.ico |
Tools -> Rotate -> 180º |
rotate180.ico |
Tools -> Flip & Rotate -> 90º -> Left |
fliprotate90left.ico |
Tools -> Flip & Rotate -> 90º -> Right |
fliprotate90right.ico |
Tools -> Flip & Rotate -> 180º |
fliprotate180.ico |
- Execute the application to see the result
- Close the form and return to your programming environment
- On the form, click Tools
- In the Properties window, double-click Enabled to change its value from True
to False
- Right-click the form and click View Code
- In the Scope in Form1.h combo box, select PictureViewer3::Form1
- In the Functions in PictureViewer3::Form1 combo box, select
mnuFileOpen_Click
- Change the event as follows:
System::Void mnuFileOpen_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( ofdPicture->ShowDialog() ==
System::Windows::Forms::DialogResult::OK )
{
bmpPicture = gcnew Bitmap(ofdPicture->FileName);
Invalidate();
mnuTools->Enabled = true;
}
}
|
- Execute the application to test it and notice that you cannot use the
Tools menu at this time
- Open a picture and notice that you can use the Tools menu now
- Close the form and return to your programming environment
- Under the form, click mnuMain and, on the form, click Tools
- Under Flip & Rotate, click Type Here, type - and press Enter
- Under the new menu separator, click Type Here, type &Original Size
and press Enter
- On the form, click Original Size
- In the Properties window, double-click Checked to change its value
to true
- Complete the menu with the following items
|
Text |
Name |
&Original Size |
mnuOriginalSize |
Ce&nter Image |
mnuCenterImage |
R&esize Form to Fit Picture |
mnuResizeForm |
O&ccupy Client Area |
mnuOccupyClientArea |
&Zoom |
mnuZoom |
|
- On the form, click Tools and double-click Original Size
- Implement its event as follows:
System::Void mnuOriginalSize_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( bmpPicture != nullptr )
{
pbxViewer->SizeMode = PictureBoxSizeMode::Normal;
mnuOriginalSize->Checked = true;
mnuCenterImage->Checked = false;
mnuResizeToPicture->Checked = false;
mnuUseClientArea->Checked = false;
mnuResizeZoom->Checked = false;
}
}
|
- Return to the form
- On the form, click Tools and double-click Center Image
- Implement its event as follows:
System::Void mnuCenterImage_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( bmpPicture != nullptr )
{
pbxViewer->SizeMode = PictureBoxSizeMode::CenterImage;
mnuCenterImage->Checked = true;
mnuResizeToPicture->Checked = false;
mnuOriginalSize->Checked = false;
mnuUseClientArea->Checked = false;
mnuResizeZoom->Checked = false;
}
}
|
- Return to the form
- On the form, click Tools and double-click Resize Form to Fit Picture
- Implement its event as follows:
System::Void mnuResizeForm_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( bmpPicture != nullptr )
{
pbxViewer->SizeMode = PictureBoxSizeMode::AutoSize;
mnuResizeToPicture->Checked = true;
mnuOriginalSize->Checked = false;
mnuCenterImage->Checked = false;
mnuUseClientArea->Checked = false;
mnuResizeZoom->Checked = false;
}
}
|
- Return to the form
- On the form, click Tools and double-click Occupy Client Area
- Implement its event as follows:
System::Void mnuOccupyClientArea_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( bmpPicture != nullptr )
{
pbxViewer->SizeMode = PictureBoxSizeMode::StretchImage;
mnuResizeToPicture->Checked = true;
mnuOriginalSize->Checked = false;
mnuCenterImage->Checked = false;
mnuUseClientArea->Checked = false;
mnuResizeZoom->Checked = false;
}
}
|
- Return to the form
- On the form, click Tools and double-click Zoom
- Implement its event as follows:
System::Void mnuZoom_Click(System::Object^ sender,
System::EventArgs^ e)
{
if( bmpPicture != nullptr )
{
pbxViewer->SizeMode = PictureBoxSizeMode::Zoom;
mnuResizeZoom->Checked = true;
mnuResizeToPicture->Checked = false;
mnuOriginalSize->Checked = false;
mnuCenterImage->Checked = false;
mnuUseClientArea->Checked = false;
}
}
|
- Execute the application to test
- Open a picture and use the options under the Tools menu
- Close the form and return to your programming environment
|
|