C++/CLIの練習

文字書いたり,ダイアログを表示させてみた。
ほとんど記録用メモ

コード

#include <stdio.h>
#include <windows.h>

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

// コマンドプロンプトを表示させない
//#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"") 


//フォームを作成するクラス
ref class FormClass : System::Windows::Forms::Form
{
private:
public:
	// コンストラクタ
	FormClass(){														
		this->Text = "CLIテスト";										// タイトル
		//this->BackColor = Color::LightBlue;							// 背景色
		this->Paint += gcnew PaintEventHandler(MyPaint);				// 描画イベント関連付け 
		this->MouseDown += gcnew MouseEventHandler(MyClick);			// マウスイベント関連付け
	}

	// 描画イベントハンドラ
	static void MyPaint(Object^ sender, PaintEventArgs^ e){
		Graphics^ g = e->Graphics;										// 描画用グラフィックスを取得


		System::Drawing::Font^ f;										// フォントの作成
		f = gcnew System::Drawing::Font("MS ゴシック", 14);

		g->DrawLine(gcnew Pen(Color::Red, 1), 10, 150, 260, 150);		// 線を引く
		g->DrawString("orz", f,Brushes::Black, 10, 10);					// 文字を書く

		// メモリ解放
		delete f;

	}

	// マウスクリックイベントハンドラ
	static void MyClick(Object^ sender, MouseEventArgs^ e){
		System::Windows::Forms::DialogResult dr;
		dr= MessageBox::Show("test", "test2", MessageBoxButtons::OKCancel);

		if(dr == System::Windows::Forms::DialogResult::OK){
			printf("OK押した\n");
		} else if(dr == System::Windows::Forms::DialogResult::Cancel){
			printf("キャンセル押した\n");
		}
	}

};



// Main関数
[STAThreadAttribute]
int main()
{
	FormClass^ mf = gcnew FormClass();
	Application::Run(mf);

	// メモリ解放
	delete mf;
    return 0;
}