/******************************************************************/
/*★ Direct Draw でメッセージループから Image を描画 前田 稔 ★*/
/******************************************************************/
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectDraw;
namespace DXDraw
{
public class DXDraw : Form
{
String ImgFile = "C:\\Data\\Test\\emiko_s.bmp";
private Device draw = null; // DrawDevice object.
private Surface primary = null; // primary destination surface.
private Surface offscreen = null; // offscreen surface(bitmap loaded).
private Rectangle dest;
public DXDraw()
{
ClientSize = new System.Drawing.Size(480, 480);
Text = "DXDraw";
CreateSurfaces(); // Surface was lost. Recreate them.
}
// 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.
dest = new Rectangle(PointToScreen(new Point(0,0)), ClientSize);
try
{
primary.Draw(dest, 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()
{
// DrawDevice を生成
draw = new Device();
draw.SetCooperativeLevel(this, CooperativeLevelFlags.Normal);
// primary Surface を生成
SurfaceDescription description = new SurfaceDescription();
description.SurfaceCaps.PrimarySurface = true;
primary = new Surface(description, draw);
// offscreen(Image) Surface を生成
description.Clear();
offscreen = new Surface(ImgFile, description, draw);
}
//☆ Main() メソッド
static void Main()
{
using (DXDraw frm = new DXDraw())
{
frm.CreateSurfaces(); // Create Draw Surfaces
frm.Show();
// メッセージループ
while(frm.Created)
{
frm.Draw();
Application.DoEvents();
}
}
}
}
}
|