A picture box is a rectangular object that can serve
different purposes. It can be used to host or hold other controls, it can
be used as a drawing board instead of drawing directly on a form, it can
be used to display a picture on an application, it can also be used for
aesthetic reasons.
To create a picture box, use the PictureBox class.
|
|
- Start Microsoft Visual Studio .NET
- On the Start Page, click New Project (alternatively, on the main
menu, you can click File -> New -> Project...)
- On the New Project dialog box, in the Project Types tree list, click
Visual C++ Projects
- In the Templates list, click Managed C++ Empty Project
- In the Name edit box, replace the <Enter name> content with Picture
Box
- In the Location combo box, accept the suggestion or type your own.
If you don't have any, type C:\Programs\MSVC.NET
- Click OK
- On the main menu, click Project -> Add New Item...
- In the Add New Item dialog box, in the Templates list, click C++
File
- In the Name box, replace <Enter name> with Main and click OK
- Replace the contents of the empty file with the following:
#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
__gc class SimpleForm : public Form
{
public:
SimpleForm();
private:
PictureBox *PictBox;
};
SimpleForm::SimpleForm()
{
// The caption of the form
this->Text = S"Picture Box Example";
PictBox = new PictureBox;
PictBox->BorderStyle = BorderStyle::FixedSingle;
PictBox->BackColor = Drawing::Color::White;
PictBox->Location = Point(16, 16);
PictBox->Size = Drawing::Size(240, 180);
// After creating the control, add it to the
// group of controls of the form
Controls->Add(PictBox);
}
int __stdcall WinMain()
{
SimpleForm *SF = new SimpleForm();
Application::Run(SF);
return 0;
}
|
- Test the application:
- To prepare a picture to show, from this
page, right-click the second picture and save it as serena.jpg.
Either save it to the folder that contains the current project or copy
and paste it to the folder of the current project.
- To display the image in the picture box, change the source file as
follows:
#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
__gc class SimpleForm : public Form
{
public:
SimpleForm();
private:
PictureBox *PictBox;
};
SimpleForm::SimpleForm()
{
// The caption of the form
this->Text = S"Picture Box Example";
// The size of the form
this->Size = Drawing::Size(300, 480);
PictBox = new PictureBox;
PictBox->BorderStyle = BorderStyle::FixedSingle;
PictBox->BackColor = Drawing::Color::White;
PictBox->Location = Point(16, 16);
PictBox->Size = Drawing::Size(262, 392);
// The following line assumes that the picture is in the same
// folder of the application
PictBox->Image = Image::FromFile("Serena.jpg");
// After creating the control, add it to the
// group of controls of the form
Controls->Add(PictBox);
}
int __stdcall WinMain()
{
SimpleForm *SF = new SimpleForm();
Application::Run(SF);
return 0;
}
|
|
- Test the application
|
|