C#からWin32APIを呼び出してみた

ほぼ見よう見真似でやってみた。

using System;
using System.Drawing;
using System.Runtime.InteropServices;//DllImportに必要
public class WinApi
{
        //よくわからないけどたぶんメモリ上に連続してデータ並べるってこと
        [StructLayout(LayoutKind.Sequential)]
        //Win32APIで使うデータって.netでは一切定義されてないから定義しないとダメ
        public struct RECT
        {
            public Int32 left, top, right, bottom;
            //コンストラクタ?構造体なんであまり関係ないような気もする。
            //Setterみたいなもんです。たぶん。
            public RECT(Rectangle rect)
            {
                left = rect.Left;
                top = rect.Top;
                right = rect.Right;
                bottom = rect.Bottom;
            }
        }
        //ExtTextOutWはgdi32.dllにあるんですね。知らんけどね。
        //文字コードをUnicodeに指定ってのは、なんとなくわかった。
        [DllImport("gdi32.dll", CharSet = CharSet.Unicode)]
        private static unsafe extern bool ExtTextOutW(IntPtr hdc, Int32 X,
                Int32 Y, UInt32 fuOptions, RECT* lprc, string lpString,
                UInt32 cbCount, IntPtr lpDx);
        public static bool ExtTextOut(IntPtr hdc, Int32 X, Int32 Y,
                UInt32 fuOptions, RECT* lprc, string lpString,
                UInt32 cbCount, IntPtr lpDx)
        {
                unsafe
                {
                        ExtTextOutW(hdc, X, Y, fuOptions, lprc, lpString, cbCount, lpDx);
                }
        }
}

みたいな風に定義しといて、呼び出すときは、また今度にしよう。
これでフォームに文字列を描画できるようになりました。
.netの標準機能でもできるけどね。