/********************************************************/
/*★ Direct Draw で offscreen に図形を描画 前田 稔 ★*/
/********************************************************/
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectDraw;
namespace DXDraw
{
public class DXDraw : System.Windows.Forms.Form
{
private Device draw = null; // DrawDevice object.
private Surface primary = null; // primary destination surface.
private Surface offscreen = null; // offscreen surface(bitmap loaded).
private Rectangle destination = new Rectangle();
public DXDraw()
{
this.ClientSize = new System.Drawing.Size(400, 320);
this.Text = "DXDraw";
this.Resize += new System.EventHandler(this.DXDraw_SizeChanged);
this.SizeChanged += new System.EventHandler(this.DXDraw_SizeChanged);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.DXDraw_Paint);
CreateSurfaces(); // Call creates the surface objects.
}
//☆ Main() メソッド
static void Main()
{
// using で資源の解放を確実に行う
using(DXDraw mf = new DXDraw())
{ Application.Run(mf);
}
}
private void DXDraw_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Draw();
}
private void DXDraw_SizeChanged(object sender, System.EventArgs e)
{
Draw();
}
// This function Draws the offscreen bitmap surface to the primary visible surface.
private void Draw()
{
if (null == primary) return;
if (FormWindowState.Minimized == WindowState) return;
Height = Height < 50 ? 50 : Height;
// Get the new client size to Draw to.
destination = new Rectangle(PointToScreen(new Point(0,0)), ClientSize);
try
{
primary.Draw(destination, offscreen, DrawFlags.Wait);
}
catch(SurfaceLostException)
{
CreateSurfaces(); // Surface was lost. Recreate them.
}
}
// This function is where the surfaces and clipper object are created.
private void CreateSurfaces()
{
// Create new DrawDevice.
draw = new Device();
// coop level normal windowed mode.
draw.SetCooperativeLevel(this, CooperativeLevelFlags.Normal);
// Create new primary surface.
SurfaceDescription description = new SurfaceDescription();
description.SurfaceCaps.PrimarySurface = true;
primary = new Surface(description, draw);
// Clear offscreen Surface
description.Clear();
description.SurfaceCaps.OffScreenPlain = true;
description.Width = 400;
description.Height = 320;
offscreen = new Surface(description, draw);
offscreen.ColorFill(Color.White);
offscreen.FillColor = Color.Blue;
offscreen.DrawEllipse(50, 20, 350, 200);
offscreen.FillColor = Color.Red;
offscreen.DrawBox(50, 220, 350, 300);
}
}
}
|