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;
}

C++/CLIでのフォーム

OpenCVをDLL,表示部をC#と二つに分けていたが
単体で完結した方が開発しやすい気がした。
んなことでC++/CLIを試してみた。ほとんどC#みたいだけど細部が微妙に違うのがねー

コード

#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(MyHandler);				// 描画イベント関連付け
	}

	// イベントハンドラ
	static void MyHandler(Object^ sender, PaintEventArgs^ e){
		Graphics^ g = e->Graphics;
		g->DrawLine(gcnew Pen(Color::Red, 1), 10, 150, 260, 150);
	}

	// マウスクリックイベントをオーバライド
	virtual void OnMouseClick(MouseEventArgs^ e) override{
		Application::Exit();											// 終了
	}

};


// Main関数
[STAThreadAttribute]
int main()
{
	Application::Run(gcnew FormClass());
	
    return 0;
}

実行結果

OpenCV in C# その2

OpenCVの環境設定

1.OpenCVの入手

http://sourceforge.net/projects/opencvlibrary/
「View all file」→「OpenCV-2.1.0-win32-vs2008.exe」

2.インクルードファイルの設定

「プロジェクト」→「プロパティ」→「構成プロパティ」→「C++」→「全般」→「追加のインクルードディレクトリ」
に以下のものを追加

C:\OpenCV2.1\include\opencv

3.ライブラリファイルの設定

「プロジェクト」→「プロパティ」→「構成プロパティ」→「リンカ」→「全般」→「追加のライブラリディレクトリ」
に以下のものを追加

C:\OpenCV2.1\lib

4.追加の依存ファイルの設定

「プロジェクト」→「プロパティ」→「構成プロパティ」→「リンカ」→「入力」→「追加の依存ファイル」
に以下のものを追加

highgui210.lib
cxts210.lib
cv210.lib
cxcore210.lib
ml210.lib
cvaux210.lib

5.ヘッダの修正

#pragma once
#include <cv.h>
#include <highgui.h>
#include <ctype.h>
#include <stdio.h>
#include "stdafx.h"

using namespace System;
namespace CV {

	public ref class Class1
	{
	public:
		void test();
	};
}

6.コードの修正

#include "CV.h"

void CV::Class1::test(){
		IplImage *img = cvCreateImage (cvSize (400, 200), IPL_DEPTH_8U, 3);
	cvZero (img);

	cvNamedWindow ("Image", CV_WINDOW_AUTOSIZE);
	cvShowImage ("Image", img);
	cvWaitKey (0);

	cvDestroyWindow ("Image");
	cvReleaseImage (&img);
}

できたDLLを前回のと入れ替えるだけで問題なく動作するはず。

OpenCV in C# その1

まずC++/CLIを用いてC#で用いるDLLを試す。

1.プロジェクトの作成

「新規作成」→「プロジェクト」→「クラスライブラリ」
プロジェクト名はCVとでもしておく

2.CV.h

適当な関数を定義

#pragma once

using namespace System;
namespace CV {

	public ref class Class1
	{
	public:
		void test();
	};
}

3.CV.cpp

定義の中身

#include "stdafx.h"
#include "CV.h"
#include "stdio.h"

void CV::Class1::test(){
	printf("aaaa");
}

4.ビルド

F7を押してビルド,何も問題なくDLLが作成されるはず。

5.C#のプロジェクト作成

・・・といきたい所ですが,無料版のExpressだと64bit環境で動作する実行ファイルを作成できません。コンパイルオプションが「Any CPU」に固定されているのが原因らしいです。
というわけで今回は.NET Frameworkに付属するコンパイラを用います。

6.コンパイラの有無確認

おおむね

C:\Windows\Microsoft.NET\Framework\バージョン

のどこかに「csc.exe」が存在するはず,これがコンパイラとなります。2010/4/10時点ではv3.5の中にありました。ない場合は.NET Framework SDK を入手/インストールして下さい。

7.コンパイラのパス通し

「コンパネ」→「システム」→「システムの詳細設定」→「環境変数」→「システム環境変数」→「Path」

;C:\Windows\Microsoft.NET\Framework\v3.5

を追加、「;」は必須。

8.C#のコード

using System;
using System.Drawing;
using System.Windows.Forms;

using CV;

class F1: Form
{
    public static void Main()
    {
        F1 = new F1();
        Class1 cv = new Class1();
	cv.test();
        Application.Run(f1);
    }

    // コンストラクタ
    public F1()
    {
        Text = "タイトル";
        BackColor = Color.Yellow;
    }
}

9.コンパイル

コマンドプロンプトを起動し,コード/DLLがおいてある場所まで移動しコンパイルする。コマンド名は「csc」でExpressで問題だったプラットフォームなども手動で指定する。

cd 〜〜
csc /platform:x86 /r:CV.dll 〜.cs

問題なければ実行ファイルが作成され,cの命令であるprintfが使えているのが確認できるはず。

いちいちコマンド打つのは面倒なのでバッチファイルを作成するのおすすめ