Newebie: How do I change a Picture Box on a Visual C++ / CLR managed form into a picture box array?

C

Charles

Hello Everyone,
I have been gettting great feedback from microsoft.public.vc.language
group but after doing more searching I think my post should be directed
to this group.

I am trying to make a simple gif animation using VC++ and 13 different
gif files and a timer.
I am new to VC++ but played around with C++ for a few years. I am
using Microsoft Visual Studio 2005 (VC++).

Before I was using 13 different picturebox controls but have been told
that I should use a picturebox array instead.

I have found many exmaples on how to make a String array however I am
having a difficult time changing a picturebox control into a picturebox
array. I have started a new form and a single picturebox control. I
have been trying to figure out how to change the declaration to make it
into an array. However, I have had no luck this far. Below is a cut
and paste
of the new form with a single picturebox. If anyone could help me to
understand how to accomplish this I would be grateful.

Thank you,
Charles


--------------------------------------------------------------
#pragma once

namespace ex {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will
need to change the
/// 'Resource File Name' property for the managed
resource compiler tool
/// associated with all .resx files this class depends
on. Otherwise,
/// the designers will not be able to interact
properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::pictureBox^ pictureBox1;
protected:


private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;


#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not
modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->pictureBox1 = (gcnew
System::Windows::Forms::pictureBox());

(cli::safe_cast<System::ComponentModel::ISupportInitialize^

(this->pictureBox1))->BeginInit();

this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Location =
System::Drawing::point(70, 44);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size =
System::Drawing::Size(100, 50);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
this->pictureBox1->Click += gcnew
System::EventHandler(this,
&Form1::pictureBox1_Click);
//
// Form1
//
this->AutoScaleDimensions =
System::Drawing::SizeF(6, 13);
this->AutoScaleMode =
System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(295,
266);
this->Controls->Add(this->pictureBox1);
this->Name = L"Form1";
this->Text = L"Form1";

(cli::safe_cast<System::ComponentModel::ISupportInitialize^
(this->pictureBox1))->EndInit();


this->ResumeLayout(false);

}
#pragma endregion
private: System::Void pictureBox1_Click(System::Object^
sender,
System::EventArgs^ e) {
}
};
 
B

Bruno van Dooren

Hello Everyone,
I have been gettting great feedback from microsoft.public.vc.language
group but after doing more searching I think my post should be directed
to this group.

I am trying to make a simple gif animation using VC++ and 13 different
gif files and a timer.
I am new to VC++ but played around with C++ for a few years. I am
using Microsoft Visual Studio 2005 (VC++).

Before I was using 13 different picturebox controls but have been told
that I should use a picturebox array instead.

I have found many exmaples on how to make a String array however I am
having a difficult time changing a picturebox control into a picturebox
array. I have started a new form and a single picturebox control. I
have been trying to figure out how to change the declaration to make it
into an array. However, I have had no luck this far. Below is a cut
and paste
of the new form with a single picturebox. If anyone could help me to
understand how to accomplish this I would be grateful.


Hi,

There is an easy way to do this:
declare a member variable like this:
array<Image^> ^imageArray ;

int CurrentImage;

Then in your constructor, after the InitializeComponent method you have to
initialize your array of images:
CurrentImage = 0;

imageArray = gcnew array<Image^>(13);

for(int i=0; i< imageArray->Length ; i++)

{

//initialize image array

}

And finally, In your timer function you do this:
if(CurrentImage >= imageArray->Length -1)

CurrentImage = 0;

else

CurrentImage++;

pictureBox1->Image = imageArray[CurrentImage];

That way your picturebox cycles through your image collection without much
programming on your part.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
C

Charles

Thank you Bruno for your help.

In order to initialize the array would I still need to create 13
different picturebox objects in order to embed them into the Form1.resX
file?

Also in the area where I initialize the array using the IF (not end of
array) statement what would the best way to add the information for
each of the images (GIF files)? Below is an example of the pictureBox
with one file. I think I sould be able to reuse everything below with
the exception of the first line showing the image name.

------------------------------------
this->pictureBox1->Image = (cli::safe_cast<System::Drawing::Image^
(resources->GetObject(L"pictureBox1.Image")));
this->pictureBox1->Location = System::Drawing::point(93, 69);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(100, 100);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
this->pictureBox1->Click += gcnew System::EventHandler(this,
&Form1::pictureBox1_Click);
 
B

Bruno van Dooren

In order to initialize the array would I still need to create 13
different picturebox objects in order to embed them into the Form1.resX
file?
No. You can manually add the images to your resource with the resource
editor. You can use whatever format you like. it depends on the desired
image quality. Just make sure it is one of the supported types
Also in the area where I initialize the array using the IF (not end of
array) statement what would the best way to add the information for
each of the images (GIF files)? Below is an example of the pictureBox
with one file. I think I sould be able to reuse everything below with
the exception of the first line showing the image name.

Yopu don't need extra picturebox objects.
For initializing the array you can do something like this:

//get handle to resource manager
System::ComponentModel::ComponentResourceManager^ resources =
(gcnew
System::ComponentModel::ComponentResourceManager(Form1::typeid));

array<String^>^ names = {L"image_1.Image", .... ,"image_13.Image"};
//13 image resource names

//load each image into the array.
for(int i=0; i<13; i++)
{
imageArray =(cli::safe_cast<System::Drawing::Image^ >
(resources->GetObject(names)));
}

And then just change the image in your timer function like i demonstrated in
my previous mail.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
C

Charles

Hello Bruno,
I just wanted to take a minute to say thank you for all your help.
Your a true Pro!!

Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top