マイペースなプログラミング日記

DTMやプログラミングにお熱なd-kamiがマイペースに書くブログ

デスクトップに文字列を描画する

デスクトップの右上に文字列を描画するプログラムを作った。

using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace Desktop{
    class Desktop
        [DllImport("user32.dll")]
        private  static extern IntPtr GetDC(IntPtr hwnd);
        [DllImport("user32.dll")]
        private static extern IntPtr ReleaseDC(IntPtr hwnd, IntPtr hdc);

        public void DrawStringDesktop(){
            String hello = "Hello World";
            IntPtr disDC = GetDC(IntPtr.Zero);
            Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);

            Graphics g = Graphics.FromHdc(disDC);
            IntPtr hDC = g.GetHdc();
            g.ReleaseHdc(hDC);

            Font font = new Font("MSゴシック", 16);
            SizeF stringSize = g.MeasureString(hello, font, 1000);

            g.DrawString(hello, font, new SolidBrush(Color.FromArgb(128, 0, 0, 0)), Screen.PrimaryScreen.Bounds.Width - stringSize.Width, 0);

            g.Dispose();

            ReleaseDC(IntPtr.Zero, disDC);
        }
    }
}

参考
画面をキャプチャする
http://dobon.net/vb/dotnet/graphics/screencapture.html