|
When creating an application, if it is a
word processor, after opening the application, it may display an empty area
in which the user can start typing. If the user is working on a
spreadsheet, he or she may start typing numbers and performing calculations. In
the same way, a user who is facing a graphics application would start drawing in
it. The object that a user would be using is called a document. After working on
a document for a while, there are some other automatic ideas that come in the mind of a
user. One would lead to saving the document. Another would consist of printing
it. All these routine operations should be available to the user.
This aspect of the application is taken care of by the person who creates the
application. Not every application allows a user to save or to
print its documents. If you want these operations to be possible, you must
(explicitly) provide them.
|
Windows Common Dialog Boxes |
|
To support the various operations of saving a document, opening an existing
document, printing a document, setting up printing, etc, Microsoft Windows
provides a series of standard dialog boxes that are available almost regardless of
the programming environment you are using to develop your application. These
common dialog boxes are stored in libraries (DLLs) that ship with the operating system but
they may be provided in a raw format. For this reason, except if programming in
Win32, the programming environment you use provides a customized and friendlier
technique of adding these dialog boxes to your application. In the same way, the
.NET Framework provides its own implementation of these ready-made dialog boxes
in a manner that makes it easier to implement them.
To use a standard Windows dialog box, from the Toolbox, click the button that
corresponds to the dialog box you want to add and click the form anywhere. The position of the control on the form has no importance because it is only a
representative: it will not appear when the form is running. Once the desired dialog’s icon is on the form, place a button
on the form or create a menu item that will be used to call the dialog box.
The Save As dialog box allows users to store a file on the hard drive of the computer, on a portable
medium such as a floppy disk, or on a network drive.
The Open File dialog box is used to open existing documents.
|
Practical
Learning: Introduction the Open File Dialog Box
|
|
- Start a new Windows Forms Application named PictureViewer2
- Change the properties of the form as follows:
Text: Picture Viewer
StartPosition: CenterScreen
- On the main menu, click Project -> Add New Item...
- In the Add New Item dialog box, click Icon File
- Set the Name to openpicture and click Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:
- On the main menu, click File -> Save openpicture.ico As...
- Select the PictureViewer2\PictureViewer2\bin\Debug folder
- Click Save
- In the Solution Explorer, expand the bin\Debug folder. Right-click the
Debug folder -> Add -> New Item...
- Select Icon File. Set the Name to f the project -> Add ->
Resource...
- In the Add Resource dialog box, double-click Icon flip and
click Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- In the Solution Explorer, right-click the Debug folder -> Add
-> New Item...
- Select Icon File. Set the Name to mirror.ico and click Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- In the Solution Explorer, right-click the Debug folder -> Add
-> New Item...
- Select Icon File. Set the Name to rotate90right and click Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- In the Solution Explorer, right-click the Debug folder -> Add
-> New Item...
- Select Icon File. Set the Name to rotate90left and click Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- In the Solution Explorer, right-click the Debug folder -> Add
-> New Item...
- Select Icon File. Set the Name to rotate180 and click Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- In the Solution Explorer, right-click the Debug folder -> Add
-> New Item...
- Select Icon File. Set the Name to fliprotate90right and click
Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- In the Solution Explorer, right-click the Debug folder -> Add
-> New Item...
- Select Icon File. Set the Name to fliprotate90left and click
Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- In the Solution Explorer, right-click the Debug folder -> Add
-> New Item...
- Select Icon File. Set the Name to fliprotate180 and click Add
- Right-click the picture and click Delete Image Type
- Design the 16x16 icon as follows:

- Save and close the icon design
- From the Menus & Toolbars section of the Toolbox, click MenuStrip and
click the form
- While the menu strip is still selected under the form, in the
Properties window, click (Name) and type mnuMain
- Use the menu designer and/or the Items field of the Properties
window to create the menu items as follows:
| Menu Top |
Sub Items/DropDownItems |
| Text |
Name |
Text |
Name |
Image |
| &File |
mnuFile |
&Open Picture... |
mnuFileOpenPicture |
openpicture.ico |
| |
|
Separator |
|
|
| |
|
E&xit |
mnuFileExit |
|
 |
| Menu Top |
Sub Items/DropDownItems |
| Text |
Name |
Text |
Name |
Image |
| &Tools |
mnuTools |
&Flip |
mnuToolsFlip |
flip.ico |
| |
|
&Mirror |
mnuToolsMirror |
mirror.ico |
| |
|
Separator |
|
|
| |
|
&Rotate |
mnuToolsRotate |
|
| |
|
Flip && R&otate |
mnuToolsFlipRotate |
|
|

|
| Sub Item |
Sub Items/DropDownItems |
| Text |
Text |
Name |
Image |
| Rotate |
90º |
mnuToolsRotate90 |
|
| |
180º |
mnuToolsRotate180 |
rotate180.ico |
| Flip & Rotate |
90º |
mnuToolsFlipRotate90 |
|
| |
180º |
mnuToolsFlipRotate180 |
fliprotate180.ico |
|

 |
| Sub Item |
Sub Item |
Sub Items/DropDownItems |
| Text |
Text |
Text |
Name |
Image |
| Rotate |
90º |
&Left |
mnuToolsRotate90Left |
rotate90left.ico |
| |
|
&Right |
mnuToolsRotate90Right |
rotate90right.ico |
| Flip & Rotate |
90º |
&Left |
mnuToolsFlipRotate90Left |
fliprotate90left.ico |
| |
|
&Right |
mnuToolsFlipRotate90Right |
fliprotate90right.ico |
|

 |
| Menu Top |
Sub Item |
| Text |
Text |
Name |
| &Tools |
Flip & Rotate |
|
| |
Separator |
|
| |
&Original Size |
mnuOriginalSize |
| |
Ce&nter Image |
mnuCenterImage |
| |
R&esize Form to Fit Picture |
mnuResizeForm |
| |
O&ccupy Client Area |
mnuOccupyClientArea |
| |
&Zoom |
mnuZoom |
|
 |
- From the Common Controls section of the Toolbox, click PictureBox
and click the form
- In the Properties window, change its characteristics as follows:
BorderStyle: Fixed3D
(Name): pbxViewer
Dock: Fill
- You can copy the following pictures and add them to a folder on your
computer (for example the PictureViewer2\PictureViewer2\bin\Debug
folder of the current project)
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type PictureViewer.cs and press Enter twice
- Right-click the form and click View Code
- Declare a private Bitmap variable named bmpPicture
namespace PictureViewer2
{
public partial class Form1 : Form
{
Bitmap bmpPicture;
public Form1()
{
InitializeComponent();
}
}
}
|
- Return to the form
- On top of the Properties window, click the arrow of the combo box
and select PictureViewer
- Click the Events button
and double-click Paint
- Implement the event as follows:
private void PictureViewer_Paint(object sender, PaintEventArgs e)
{
if( bmpPicture != null )
{
pbxViewer.Image = bmpPicture;
}
}
|
- Return to the form
- Click Tools and double-click Flip
- Implement the event as follows:
private void mnuToolsFlip_Click(object sender, EventArgs e)
{
if( bmpPicture != null )
{
bmpPicture.RotateFlip(RotateFlipType.RotateNoneFlipY);
Invalidate();
}
}
|
- Return to the form, click Tools and double-click Mirror
- Implement the event as follows:
private void mnuToolsMirror_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
bmpPicture.RotateFlip(RotateFlipType.RotateNoneFlipX);
Invalidate();
}
}
|
- Return to the form, click Tools, then click Rotate, then
click 90º and double-click Left
- Implement the event as follows:
private void mnuToolsRotate90Left_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
bmpPicture.RotateFlip(RotateFlipType.Rotate270FlipNone);
Invalidate();
}
}
|
- Return to the form, click Tools, then click Rotate, then
click 90º and double-click Right
- Implement the event as follows:
private void mnuToolsRotate90Right_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
bmpPicture.RotateFlip(RotateFlipType.Rotate90FlipNone);
Invalidate();
}
}
|
- Return to the form, click Tools, then click Rotate, and
double-click 180º
- Implement the event as follows:
private void mnuToolsRotate180_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
bmpPicture.RotateFlip(RotateFlipType.Rotate180FlipNone);
Invalidate();
}
}
|
- Return to the form, click Tools, then click Flip &
Rotate, then click 90º and double-click Left
- Implement the event as follows:
private void mnuToolsFlipRotate90Left_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
bmpPicture.RotateFlip(RotateFlipType.Rotate270FlipX);
Invalidate();
}
}
|
- Return to the form, click Tools, then click Flip &
Rotate, then click 90º and double-click Right
- Implement the event as follows:
private void mnuToolsFlipRotate90Right_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
bmpPicture.RotateFlip(RotateFlipType.Rotate90FlipX);
Invalidate();
}
}
|
- Return to the form, click Tools, then click Flip &
Rotate, and double-click 180º
- Implement the event as follows:
private void mnuToolsFlipRotate180_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
bmpPicture.RotateFlip(RotateFlipType.Rotate180FlipX);
Invalidate();
}
}
|
- Return to the form, click Tools and double-click Original Size
- Implement its event as follows:
private void mnuToolsOriginalSize_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
pbxViewer.SizeMode = PictureBoxSizeMode.Normal;
mnuOriginalSize.Checked = true;
mnuCenterImage.Checked = false;
mnuOccupyClientArea.Checked = false;
mnuZoom.Checked = false;
}
}
|
- Return to the form, click Tools and double-click Center Image
- Implement its event as follows:
private void mnuToolsCenterImage_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
pbxViewer.SizeMode = PictureBoxSizeMode.CenterImage;
mnuOriginalSize.Checked = false;
mnuCenterImage.Checked = true;
mnuOccupyClientArea.Checked = false;
mnuZoom.Checked = false;
}
}
|
- Return to the form, click Tools and double-click Occupy Client Area
- Implement its event as follows:
private void mnuOccupyClientArea_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
pbxViewer.SizeMode = PictureBoxSizeMode.StretchImage;
mnuOriginalSize.Checked = false;
mnuCenterImage.Checked = false;
mnuOccupyClientArea.Checked = true;
mnuZoom.Checked = false;
}
}
|
- Return to the form, click Tools and double-click Zoom
- Implement its event as follows:
private void mnuZoom_Click(object sender, EventArgs e)
{
if (bmpPicture != null)
{
pbxViewer.SizeMode = PictureBoxSizeMode.Zoom;
mnuOriginalSize.Checked = false;
mnuCenterImage.Checked = false;
mnuOccupyClientArea.Checked = false;
mnuZoom.Checked = true;
}
}
|
- Return to the form
- From the Dialogs section of the Toolbox, click OpenFileDialog
and click the form
- While the open file control is still selected under the form, in the
Properties window, click (Name) and type ofdPicture
- Still in the Properties window for the open file control, click
Filter and type:
Bitmap Files (*.bmp)|*.bmp|JPEG Files (*.jpg,*.jpeg)|*.jpg|GIF Files
(*.gif)|*.gif|PNG Files (*.png)|*.png
- Save the form
- Still in the Properties window for the open file control, click
DefaultExt and type bmp
- Save the form
- On the form, click File and double-click Open Picture
- Implement the event as follows:
private void mnuFileOpenPicture_Click(object sender, EventArgs e)
{
if (ofdPicture.ShowDialog() == DialogResult.OK)
{
bmpPicture = new Bitmap(ofdPicture.FileName);
Invalidate();
mnuToolsOriginalSize_Click(sender, e);
}
}
|
- Return to the form
- Click Tools and double-click Exit
- Implement the event as follows:
private void mnuFileExit_Click(object sender, EventArgs e)
{
Close();
}
|
- Execute the application
- Open a picture and use the menu items
- Close the form and return to your programming environment
|
|