OK, here it is:
Try if you can run it after changing line 108 with your pic path.
Code:
#pragma once
namespace speranta
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections::Generic;
using namespace System::Windows::Forms;
using namespace System::IO;
using namespace System::Drawing;
/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
String^ mPicPath;
List<String^> ^mPicNames;
Random^ mRandom;
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::PictureBox^ pictureBox1;
protected:
private: System::Windows::Forms::Button^ button1;
protected:
protected:
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());
this->button1 = (gcnew System::Windows::Forms::Button());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Location = System::Drawing::Point(9, 12);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(747, 419);
this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
this->pictureBox1->Click += gcnew System::EventHandler(this, &MyForm::pictureBox1_Click);
//
// button1
//
this->button1->Location = System::Drawing::Point(299, 456);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(153, 34);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(768, 497);
this->Controls->Add(this->button1);
this->Controls->Add(this->pictureBox1);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->EndInit();
this->ResumeLayout(false);
}
#pragma endregion
private:
System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e)
{
mPicPath = "E:\\Bilder\\";
mPicNames = gcnew List<String^>;
array<String^> ^tmp = Directory::GetFiles(mPicPath, "*.png");
mPicNames->AddRange(tmp);
tmp = Directory::GetFiles(mPicPath, "*.jpg");
mPicNames->AddRange(tmp);
if (mPicNames->Count == 0)
{
MessageBox::Show("No pics loaded");
return;
}
mRandom = gcnew Random();
pictureBox1->Image = Image::FromFile(mPicNames[0]);
}
private:
System::Void pictureBox1_Click(System::Object^ sender, System::EventArgs^ e)
{
// What shall happen here ??
}
private:
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
int idx = mRandom->Next(mPicNames->Count);
pictureBox1->Image = Image::FromFile(mPicNames[idx]);
}
};
}
BTW Hre are some tutorials you might want to look at.
Microsoft Visual C++/CLI Programming
YouTube