Graphicsオブジェクト

Win32APIからでも使用できるようなインターフェースとなっている
参照(http://www.microsoft.com/japan/msdn/library/default.asp?url=/japan/msdn/library/ja/cpref/html/frlrfsystemdrawinggraphicsmemberstopic.asp)

  • 構築方法
    • FromHdc サンプル
[System.Runtime.InteropServices.DllImport("User32.dll")]
public extern static System.IntPtr GetDC(System.IntPtr hWnd);

private void button1_Click(object sender, System.EventArgs e)
{
  IntPtr deskHDC = GetDC(System.IntPtr.Zero);
  Graphics g = System.Drawing.Graphics.FromHdc(deskHDC);
  g.FillRectangle(new SolidBrush(Color.Red),50,50,200,200);
}
    • FromHwnd サンプル
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern System.IntPtr GetDesktopWindow();
private void button2_Click(object sender, System.EventArgs e)
{
    // Get handle to form.
    IntPtr hwnd = new IntPtr();
    hwnd = this.Handle;
    // Create new graphics object using handle to window.
    Graphics newGraphics = Graphics.FromHwnd(hwnd);
    // Draw rectangle to screen.
    newGraphics.DrawRectangle(new Pen(Color.Red, 3), 0, 0, 200, 100);
    // Dispose of new graphics.
    newGraphics.Dispose();
}
    • FromImage サンプル
private void button3_Click(object sender, System.EventArgs e)
{
    Graphics g = this.CreateGraphics();
    Image i = Image.FromFile(@"D:\aaa.gif");
    g.DrawImage(i,10,10);
}