情報アイランド

「情報を制する者は世界を制す」をモットーに様々な情報を提供することを目指すブログです。現在はプログラミング関連情報が多めですが、投資関連情報も取り扱っていきたいです。

C#でWIN32API step11 文字描画(その1)

step8:ここまでのまとめ(その1)

step9:FPSを測定する

step10:図形描画

今回は文字列を描画してみます。

WM_PAINTメッセージプロシージャに文字を描画する処理を追加します。

文字の描画にはTextOut関数またはDrawText関数を使用します。

private static IntPtr WM_PAINT(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam)
{
    WIN32.Draw.PAINTSTRUCT paintstruct;

    IntPtr hdc = WIN32.Draw.BeginPaint(hWnd, out paintstruct);

    WIN32.Draw.TextOut(hdc, 150, 10, "Main Window", "Main Window".Length);

    WIN32.Draw.SetTextColor(hdc, 0x00FF0FF0);
    WIN32.Draw.TextOut(hdc, 150, 30, "文字2", "文字2".Length);

    WIN32.Window.RECT rect = new WIN32.Window.RECT();
    rect.left = 150;
    rect.top = 50;
    rect.right = 250;
    rect.bottom = 70;
    WIN32.Draw.Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);
    WIN32.Draw.DrawText(hdc, "文字3", "文字3".Length, ref rect, WIN32.Draw.DT.DT_CENTER | WIN32.Draw.DT.DT_VCENTER);

    IntPtr hfont = WIN32.Draw.CreateFont(32, 0, 0, 0, WIN32.Draw.FW_NORMAL, 0, 0, 0, WIN32.Draw.DEFAULT_CHARSET, WIN32.Draw.OUT_DEFAULT_PRECIS, WIN32.Draw.CLIP_DEFAULT_PRECIS, WIN32.Draw.DEFAULT_QUALITY, WIN32.Draw.DEFAULT_PITCH, "メイリオ");
    WIN32.Draw.SelectObject(hdc, hfont);
    WIN32.Draw.TextOut(hdc, 120, 150, "フォントを設定", "フォントを設定".Length);
    WIN32.Draw.DeleteObject(hfont);

    WIN32.Draw.SelectObject(hdc, WIN32.Draw.GetStockObject(WIN32.Draw.WHITE_BRUSH));
    WIN32.Draw.SetPixel(hdc, 100, 100, 0x000000FF);
    WIN32.Draw.SetPixelV(hdc, 100, 200, 0x000000FF);

    IntPtr hpen = WIN32.Draw.CreatePen(WIN32.Draw.PS.PS_DASH, 1, 0x00FF0000);
    WIN32.Draw.SelectObject(hdc, hpen);
    WIN32.Window.POINT point = new WIN32.Window.POINT();
    WIN32.Draw.MoveToEx(hdc, 10, 10, ref point);
    WIN32.Draw.LineTo(hdc, 222, 123);
    WIN32.Draw.DeleteObject(hpen);

    WIN32.Draw.SelectObject(hdc, WIN32.Draw.GetStockObject(WIN32.Draw.BLACK_PEN));
    IntPtr hsolidbrush = WIN32.Draw.CreateSolidBrush(0x0000FF00);
    WIN32.Draw.SelectObject(hdc, hsolidbrush);
    WIN32.Draw.Rectangle(hdc, 25, 25, 75, 75);
    WIN32.Draw.Ellipse(hdc, 25, 80, 150, 150);
    WIN32.Draw.DeleteObject(hsolidbrush);
    IntPtr hhatchbrush = WIN32.Draw.CreateHatchBrush(WIN32.Draw.HS.HS_CROSS, 0x000000FF);
    WIN32.Draw.SelectObject(hdc, hhatchbrush);
    WIN32.Draw.RoundRect(hdc, 25, 160, 75, 250, 5, 10);
    WIN32.Draw.Polygon(hdc, new WIN32.Window.POINT[] { new WIN32.Window.POINT() { x = 100, y = 100 }, new WIN32.Window.POINT() { x = 150, y = 200 }, new WIN32.Window.POINT() { x = 50, y = 200 }, }, 3);
    WIN32.Draw.DeleteObject(hhatchbrush);

    WIN32.Draw.EndPaint(hWnd, ref paintstruct);

    return IntPtr.Zero;
}

こちらが、P/I用のコードです(前回からの追加部分)。

using System;
using System.Runtime.InteropServices;

namespace Anttn.WIN32
{
    public static partial class Draw
    {
    (省略)

        [DllImport("gdi32.dll", CharSet = CharSet.Auto)]
        public static extern bool TextOut(IntPtr hdc, int nXStart, int nYStart, string lpString, int cbString);

        [DllImport("gdi32.dll")]
        public static extern uint SetTextColor(IntPtr hdc, uint crColor);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int DrawText(IntPtr hDC, string lpString, int nCount, [In, Out] ref WIN32.Window.RECT lpRect, DT uFormat);

        [DllImport("gdi32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr CreateFont(int nHeight, int nWidth, int nEscapement, int nOrientation, int fnWeight, uint fdwItalic, uint fdwUnderline, uint fdwStrikeOut, uint fdwCharSet, uint fdwOutputPrecision, uint fdwClipPrecision, uint fdwQuality, uint fdwPitchAndFamily, string lpszFace);

    (省略)

        [Flags]
        public enum DT : uint
        {
            DT_TOP = 0x00000000,
            DT_LEFT = 0x00000000,
            DT_CENTER = 0x00000001,
            DT_RIGHT = 0x00000002,
            DT_VCENTER = 0x00000004,
            DT_BOTTOM = 0x00000008,
            DT_WORDBREAK = 0x00000010,
            DT_SINGLELINE = 0x00000020,
            DT_EXPANDTABS = 0x00000040,
            DT_TABSTOP = 0x00000080,
            DT_NOCLIP = 0x00000100,
            DT_EXTERNALLEADING = 0x00000200,
            DT_CALCRECT = 0x00000400,
            DT_NOPREFIX = 0x00000800,
            DT_INTERNAL = 0x00001000,
            DT_EDITCONTROL = 0x00002000,
            DT_PATH_ELLIPSIS = 0x00004000,
            DT_END_ELLIPSIS = 0x00008000,
            DT_MODIFYSTRING = 0x00010000,
            DT_RTLREADING = 0x00020000,
            DT_WORD_ELLIPSIS = 0x00040000,
            DT_NOFULLWIDTHCHARBREAK = 0x00080000,
            DT_HIDEPREFIX = 0x00100000,
            DT_PREFIXONLY = 0x00200000,
        }

    (省略)

        public const int FW_DONTCARE = 0;
        public const int FW_THIN = 100;
        public const int FW_EXTRALIGHT = 200;
        public const int FW_ULTRALIGHT = FW_EXTRALIGHT;
        public const int FW_LIGHT = 300;
        public const int FW_NORMAL = 400;
        public const int FW_REGULAR = FW_NORMAL;
        public const int FW_MEDIUM = 500;
        public const int FW_SEMIBOLD = 600;
        public const int FW_DEMIBOLD = FW_SEMIBOLD;
        public const int FW_BOLD = 700;
        public const int FW_EXTRABOLD = 800;
        public const int FW_ULTRABOLD = FW_EXTRABOLD;
        public const int FW_HEAVY = 900;
        public const int FW_BLACK = FW_HEAVY;

        public const uint ANSI_CHARSET = 0;
        public const uint DEFAULT_CHARSET = 1;
        public const uint SYMBOL_CHARSET = 2;
        public const uint SHIFTJIS_CHARSET = 128;
        public const uint HANGEUL_CHARSET = 129;
        public const uint HANGUL_CHARSET = 129;
        public const uint GB2312_CHARSET = 134;
        public const uint CHINESEBIG5_CHARSET = 136;
        public const uint OEM_CHARSET = 255;
        public const uint JOHAB_CHARSET = 130;
        public const uint HEBREW_CHARSET = 177;
        public const uint ARABIC_CHARSET = 178;
        public const uint GREEK_CHARSET = 161;
        public const uint TURKISH_CHARSET = 162;
        public const uint VIETNAMESE_CHARSET = 163;
        public const uint THAI_CHARSET = 222;
        public const uint EASTEUROPE_CHARSET = 238;
        public const uint RUSSIAN_CHARSET = 204;
        public const uint MAC_CHARSET = 77;
        public const uint BALTIC_CHARSET = 186;

        public const uint OUT_DEFAULT_PRECIS = 0;
        public const uint OUT_STRING_PRECIS = 1;
        public const uint OUT_CHARACTER_PRECIS = 2;
        public const uint OUT_STROKE_PRECIS = 3;
        public const uint OUT_TT_PRECIS = 4;
        public const uint OUT_DEVICE_PRECIS = 5;
        public const uint OUT_RASTER_PRECIS = 6;
        public const uint OUT_TT_ONLY_PRECIS = 7;
        public const uint OUT_OUTLINE_PRECIS = 8;
        public const uint OUT_SCREEN_OUTLINE_PRECIS = 9;
        public const uint OUT_PS_ONLY_PRECIS = 10;

        public const uint CLIP_DEFAULT_PRECIS = 0;
        public const uint CLIP_CHARACTER_PRECIS = 1;
        public const uint CLIP_STROKE_PRECIS = 2;
        public const uint CLIP_MASK = 0xf;
        public const uint CLIP_LH_ANGLES = (1 << 4);
        public const uint CLIP_TT_ALWAYS = (2 << 4);
        public const uint CLIP_EMBEDDED = (8 << 4);

        public const uint DEFAULT_QUALITY = 0;
        public const uint DRAFT_QUALITY = 1;
        public const uint PROOF_QUALITY = 2;
        public const uint NONANTIALIASED_QUALITY = 3;
        public const uint ANTIALIASED_QUALITY = 4;

        public const uint DEFAULT_PITCH = 0;
        public const uint FIXED_PITCH = 1;
        public const uint VARIABLE_PITCH = 2;

        public const uint FF_DONTCARE = (0 << 4);
        public const uint FF_ROMAN = (1 << 4);
        public const uint FF_SWISS = (2 << 4);
        public const uint FF_MODERN = (3 << 4);
        public const uint FF_SCRIPT = (4 << 4);
        public const uint FF_DECORATIVE = (5 << 4);
    }
}

描画結果は以下のようになります。

pizyumi
プログラミング歴19年のベテランプログラマー。業務システム全般何でも作れます。現在はWeb系の技術を勉強中。
スポンサーリンク

-C#, WIN32API