のっとぇの技術メモ

Windows,Linux,インターネット関連の技術メモや紹介

Friday, January 21, 2011

はじめてのVisual Studio Visual C++ 2005 Express Edition

いまさらながら、無料提供されているVisual Studio Visual C++ 2005 Express Editionを使ってみる

まず、Express Editionは無料版のためMFCライブラリが入っていません。
VC6などのMFCライブラリ使える環境とは全然違います。
たとえば、.NETライブラリを使って記述するため、変数宣言から使える関数まで違います。
だから殆ど別言語だと思った方がいいでしょう。

たとえば、文字変数宣言などは、以下の様に違います。

MFC(VC6)の場合
CString STR = "test";

.NET(Express Edition)の場合
String^ STR = "test";

昔のC言語の場合
Char STR[4] = "test";

この辺は扱う全角文字コードの違いや取り扱いの変化によって変わってきてます。
ユーザーがコードを書く場所も、MFCだとtest.cppだったものが、.NETだとtest.hになっています。

インストール方法


 以降、記述中

・デレクトリリストを取得してリストウインドーに表示してみる。
・ボタンを押すとデレクトリ選択のウインドーが出るようにする。
・リストウインドーから選択できるようにする。
・ファイル内容を表示してみる。
・正規表現によって文字の比較をしてみる。


参考にしたサイト

正規表現


■ソースファイル
以下、作成したソースファイル(test.h)の内容です。

#pragma once


namespace test {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Text;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Text::RegularExpressions;

///
/// Form1 の概要
///
/// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
/// マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
/// 変更する必要があります。この変更を行わないと、
/// デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、
/// 正しく相互に利用できなくなります。
///
public ref class Form1 : public System::Windows::Forms::Form
{
private: System::Windows::Forms::Button^ button4;
private: System::Windows::Forms::ComboBox^ comboBox1;
private: String^ windir;
private: String^ dirname;
private: int listmode;
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ここにコンストラクタ コードを追加します
//
windir = System::Environment::GetEnvironmentVariable("windir");
}

protected:
///
/// 使用中のリソースをすべてクリーンアップします。
///
~Form1()
{
if (components)
{
delete components;
}
}

protected:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::ListBox^ listBox2;

private:
///
/// 必要なデザイナ変数です。
///
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
///
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
///
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->listBox2 = (gcnew System::Windows::Forms::ListBox());
this->button4 = (gcnew System::Windows::Forms::Button());
this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
this->SuspendLayout();
//
// button1
//
this->button1->BackColor = System::Drawing::SystemColors::Window;
this->button1->Location = System::Drawing::Point(0, 1);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(29, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"directory";
this->button1->UseVisualStyleBackColor = false;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->BackColor = System::Drawing::Color::White;
this->button2->Location = System::Drawing::Point(0, 343);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 2;
this->button2->Text = L"file list";
this->button2->UseVisualStyleBackColor = false;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// button3
//
this->button3->BackColor = System::Drawing::SystemColors::Window;
this->button3->Location = System::Drawing::Point(356, 342);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(75, 23);
this->button3->TabIndex = 3;
this->button3->Text = L"file open";
this->button3->UseVisualStyleBackColor = false;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(31, 1);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(453, 19);
this->textBox1->TabIndex = 4;
this->textBox1->Text = L"c:\\";
this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
this->textBox1->MouseDoubleClick += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::textBox1_MouseDoubleClick);
//
// listBox2
//
this->listBox2->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
this->listBox2->FormattingEnabled = true;
this->listBox2->ItemHeight = 12;
this->listBox2->Location = System::Drawing::Point(3, 23);
this->listBox2->Name = L"listBox2";
this->listBox2->Size = System::Drawing::Size(481, 314);
this->listBox2->TabIndex = 5;
this->listBox2->UseTabStops = false;
this->listBox2->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::listBox2_SelectedIndexChanged);
//
// button4
//
this->button4->Location = System::Drawing::Point(83, 345);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(59, 20);
this->button4->TabIndex = 6;
this->button4->Text = L"propaty";
this->button4->UseVisualStyleBackColor = true;
this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
//
// comboBox1
//
this->comboBox1->FormattingEnabled = true;
this->comboBox1->Items->AddRange(gcnew cli::array<>(4) {L"function1", L"function2", L"function3", L"property"});
this->comboBox1->Location = System::Drawing::Point(437, 342);
this->comboBox1->Name = L"comboBox1";
this->comboBox1->Size = System::Drawing::Size(47, 20);
this->comboBox1->TabIndex = 7;
this->comboBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox1_SelectedIndexChanged);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->AutoSize = true;
this->ClientSize = System::Drawing::Size(486, 366);
this->Controls->Add(this->comboBox1);
this->Controls->Add(this->button4);
this->Controls->Add(this->listBox2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->listBox2->Items->Clear();
//Fileだった場合、textBoxを初期化
if (File::Exists(textBox1->Text)) {
textBox1->Text = dirname;
}
if (! Directory::Exists(textBox1->Text)) {
textBox1->Text = "c:\\";
}

listmode = 0;
//デレクトリリストを取得
dirname = textBox1->Text; //デレクトリパスを保存
cli::array^ dirs = Directory::GetDirectories(textBox1->Text);
int numDirs = dirs->Length;
for(int i=0; i
{
listBox2->Items->Add(dirs[i]);
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
listBox2->Items->Clear();
// textBoxがデレクトリでなかった場合、デレクトリパスに戻す
if (! Directory::Exists(textBox1->Text)) {
textBox1->Text = dirname;
}

listmode = 1;
//ファイルリストを取得
cli::array^ files = Directory::GetFiles(textBox1->Text);
int numFiles = files->Length;
for(int i=0; i
{
listBox2->Items->Add(files[i]);
}


}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {

//ファイル内容の表示
if (File::Exists(textBox1->Text)) {
listBox2->Items->Clear();
listmode = 3;
try
{
System::IO::StreamReader^ sr
=gcnew System::IO::StreamReader(
textBox1->Text,
System::Text::Encoding::Default);
String^ strLine1;
do {
strLine1= sr->ReadLine();
listBox2->Items->Add(strLine1);
}
while(sr->Peek() != -1);
}

catch(System::Exception^ ex)
{
MessageBox::Show(ex->ToString());
}
} //end-if//


}
private: System::Void listBox2_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {

System::String^ name;
int n;

//listBoxの選択内容を取得
n = listBox2->SelectedIndex;
name = Convert::ToString(listBox2->SelectedItem);
//listBoxへlist中のSelect内容を追加
// listBox2->Items->Add(n.ToString());
// listBox2->Items->Add(name);
textBox1->Text = name;

// DirectoryListを表示
if (listmode == 0) {
listBox2->Items->Clear();
dirname = textBox1->Text; //デレクトリパスを保存
cli::array^ dirs = Directory::GetDirectories(textBox1->Text);
int numDirs = dirs->Length;
for(int i=0; i
{
listBox2->Items->Add(dirs[i]);
}
}

}
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
if (File::Exists(textBox1->Text)) {
listBox2->Items->Clear();
//ファイル情報の表示
String^ testfile = textBox1->Text;
FileInfo^ pFileProps = gcnew FileInfo(testfile);

listBox2->Items->Add(String::Concat("File Name = ", pFileProps->FullName));
listBox2->Items->Add(String::Concat("Creation Time = ", pFileProps->CreationTime.ToString()));
listBox2->Items->Add(String::Concat("Last Access Time = ", pFileProps->LastAccessTime.ToString()));
listBox2->Items->Add(String::Concat("Last Write Time = ", pFileProps->LastWriteTime.ToString()));
listBox2->Items->Add(String::Concat("Size = ", pFileProps->Length.ToString()));
}

}
private: System::Void comboBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
//test
System::String^ select_menu;
select_menu = Convert::ToString(comboBox1->SelectedItem);
int n;
n = comboBox1->SelectedIndex;
//test
listBox2->Items->Add(n.ToString());
listBox2->Items->Add(select_menu);

switch (n) {
case 0:
if (File::Exists(textBox1->Text)) {
MessageBox::Show("fuction1");
}
break;
case 1:
MessageBox::Show("function not found!!");
break;
case 2:
MessageBox::Show("function not found!!");
break;
case 3:
if (File::Exists(textBox1->Text)) {
listBox2->Items->Clear();
//ファイル情報の表示
String^ testfile = textBox1->Text;
FileInfo^ pFileProps = gcnew FileInfo(testfile);

listBox2->Items->Add(String::Concat("File Name = ", pFileProps->FullName));
listBox2->Items->Add(String::Concat("Creation Time = ", pFileProps->CreationTime.ToString()));
listBox2->Items->Add(String::Concat("Last Access Time = ", pFileProps->LastAccessTime.ToString()));
listBox2->Items->Add(String::Concat("Last Write Time = ", pFileProps->LastWriteTime.ToString()));
listBox2->Items->Add(String::Concat("Size = ", pFileProps->Length.ToString()));
}
break;
} //switch-end//

}
private: System::Void textBox1_MouseDoubleClick(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
listmode = 0;
textBox1->Text = dirname;
}
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
System::String^ null;
null = "";
String^ str = textBox1->Text;
if ((str->Length)==0) {
MessageBox::Show("Null");
}
else {
String^ m = matchRegex("test",str);
if ((m->Length)>0) {
listBox2->Items->Add("Match!!");
}
m = matchRegex("^\\\\[a-zA-Z]*",str);
if ((m->Length)>0) {
listBox2->Items->Add("UnSupport Network Path!!");
}

}
if (! String::CompareOrdinal(textBox1->Text,null)) {
textBox1->Text = "c:\\";
}


}
String^ matchRegex (String^ regex, String^ str) {
String^ tmp = str;
System::Text::RegularExpressions::Regex^ r;
r = gcnew System::Text::RegularExpressions::Regex(regex);
System::Text::RegularExpressions::Match^ m = r->Match(tmp);
if (m->Success)
return m->Value;
else
return "";
}
//END//
};
}


uploaded.to

0 Comments:

Post a Comment

<< Home