開発環境 Microsoft Visual C++ 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 空の CLR プロジェクト
プロジェクト名 ClrEdit

ClrEdit.cpp
#pragma comment(linker, "/subsystem:windows")
#pragma comment(linker, "/entry:main")
 
// .NET参照
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
 
// 名前空間
using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Windows::Forms;
 
public ref class Form1 : public Form {
public:
	Form1(void) {
		InitializeComponent();
	}
private:
	Panel^ panel;
	Button^ edit;
	TextBox^ text;
 
	void InitializeComponent(void) {
		panel = gcnew Panel;
		edit = gcnew Button;
		text = gcnew TextBox;
		panel->SuspendLayout();
		SuspendLayout();
 
		// panel
		panel->Controls->Add(edit);
		panel->Height = 28;
 
		// edit
		edit->Top = (panel->Height - edit->Height) / 2;
		edit->Text = L"編集";
		edit->Click += gcnew EventHandler(this, &Form1::EditClick);
 
		// text
		text->Multiline = true;
		text->ScrollBars = ScrollBars::Vertical;
 
		// Form1
		ClientSize = Drawing::Size(480, 480);
		Controls->Add(panel);
		Controls->Add(text);
		Text = L"Form1";
		ActiveControl = text;
 
		panel->ResumeLayout(false);
		panel->PerformLayout();
		ResumeLayout(false);
		PerformLayout();
	}
protected:
	virtual void OnResize(EventArgs^ e) override {
		int width = ClientSize.Width;
		panel->Width = width;
		text->Width = width;
		text->Top = panel->Top + panel->Height;
		text->Height = ClientSize.Height - panel->Height;
	}
private:
	void EditClick(Object^ sender, EventArgs^ e) {
 
		StringReader^ sr = gcnew StringReader(text->Text);
		StringWriter^ sw = gcnew StringWriter;
		while (sr->Peek() > -1) {
			String^ line = L" " + sr->ReadLine();
			sw->WriteLine(line);
		}
		sw->Close();
		sr->Close();
		text->Text = sw->ToString();
 
		text->Select();
		text->SelectAll();
/*/
		String^ file1 = Path::GetTempFileName();
		String^ file2 = Path::GetTempFileName();
		Encoding^ enc = Encoding::GetEncoding(L"shift_jis");
 
		// 書き込み
		File::WriteAllText(file1, text->Text, enc);
 
		// 編集
		StreamReader^ sr = gcnew StreamReader(file1, enc);
		StreamWriter^ sw = gcnew StreamWriter(file2, false, enc);
		while (sr->Peek() > -1) {
			String^ line = L" " + sr->ReadLine();
			sw->WriteLine(line);
		}
		sw->Close();
		sr->Close();
 
		// 読み込み
		text->Text = File::ReadAllText(file2, enc);
 
		File::Delete(file2);
		File::Delete(file1);
*/
	}
};
 
[STAThread]
int main() {
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false);
	Application::Run(gcnew Form1());
	return 0;
}
 
最終更新:2012年10月11日 10:16