はじめてのmono

超絶備忘録に付き一切の説明は省略。

0.環境

ディストリビューションVine Seed。ライブラリは以下のバージョンが入っている。

  • mono-core-1.1.13.6-0vl1
  • mono-basic-1.1.13.6-0vl1
  • mono-devel-1.1.13.6-0vl1
  • mono-extras-1.1.13.6-0vl1
  • mono-winforms-1.1.13.6-0vl1
  • mono-jscript-1.1.13.6-0vl1
  • mono-data-1.1.13.6-0vl1
  • mono-web-1.1.13.6-0vl1
  • mono-nunit-1.1.13.6-0vl1
  • libgdiplus-1.1.9.1-0vl1
  • gtk-sharp-1.0.8-1vl1
  • gtk-sharp-gapi-1.0.8-1vl1
  • gtk-sharp2-2.8.2-1vl1
  • gtk-sharp2-gapi-2.8.2-1vl1
  • libgda-sharp-1.9.100-0vl2
  • wine-0.9.1-0 (野良ビルドRPM)

1.コンソールでHelloWorld

public class HelloWorld {
  public static void Main(string [] args) {
    System.Console.WriteLine("Hello, World!");
  }
}
% mcs Hello.cs
% mono Hello.exe
Hello, World!

2.ウィンドウを出す

System.Windows.Formsを使う方法はエラーが出てうまくいかなかったのでGtk#を使うことにした。

namespace GtkHello
{
    using System;
    using Gtk;
    using Gdk;

    public class HelloWorld
    {
        public static int Main(string[] args)
        {
            Application.Init();

            Gtk.Window win = new Gtk.Window("Gtk# Hello World");
            win.DeleteEvent += new DeleteEventHandler(Window_Delete);
            win.ShowAll();

            Application.Run();

            return 0;
        }

        static void Window_Delete(object obj, DeleteEventArgs args)
        {
            Application.Quit();
            args.RetVal = true;
        }
    }
}
% mcs GtkHello.cs -pkg:gtk-sharp-2.0
% mono GtkHello.exe
(実行結果は省略)

付録: System.Windows.Formsを使ってウィンドウを出す(不成功)

using System;
using System.Windows.Forms;

public class MainForm : System.Windows.Forms.Form
{
    public MainForm()
    {
    }

    public static void Main(string[] args)
    {
        MainForm frm = new MainForm();
        Application.Run(frm);
    }
}
  • 設定: ~/.mono/config
<configuration>
        <dllmap dll="gdiplus.dll" target="/usr/lib/libgdiplus.so.0"/>
</configuration>
% mcs /t:winexe /r:System.Windows.Forms.dll MainForm.cs
% mono MainForm.exe
Mono System.Windows.Forms Assembly [$auto_build_revision$]
Keyboard: Japanese 106 keyboard layout
Gtk colorscheme read

Unhandled Exception: System.EntryPointNotFoundException: GdipGetFontHeightGivenDPI
in (wrapper managed-to-native) System.Drawing.GDIPlus:GdipGetFontHeightGivenDPI (intptr,single,single&)
in <0x00022> System.Drawing.Font:GetHeight (Single dpi)
in <0x00020> System.Drawing.Font:GetHeight ()
in <0x0000d> System.Drawing.Font:get_Height ()
in (wrapper remoting-invoke-with-check) System.Drawing.Font:get_Height ()
in <0x0007e> System.Windows.Forms.Form:GetAutoScaleSize (System.Drawing.Graphics g, System.Drawing.Font font)
in <0x0003e> System.Windows.Forms.Form:.ctor ()
in <0x0000a> MainForm:.ctor ()
in (wrapper remoting-invoke-with-check) MainForm:.ctor ()
in <0x0001b> MainForm:Main (System.String[] args)

追記

原因が分かった→id:nozom:20060525#1148579311