/***************************************/
/*★ 矢印キーで画像を移動 前田 稔 ★*/
/***************************************/
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";
Device draw = null; // DrawDevice object.
Surface primary = null; // primary destination surface.
Surface image = null; // Image surface.
Surface offscreen = null; // offscreen surface
Rectangle dest,sou; // 受取側と送り側の矩形領域
int xp=10, yp= 48; // 描画座標
int width, height; // Image Size
bool flg= true;
public DXDraw()
{
ClientSize = new System.Drawing.Size(640, 480);
Text = "DXDraw";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyEvent);
//Image File をロードしてサイズを取得
Bitmap bmp = new Bitmap(ImgFile);
width = bmp.Width;
height = bmp.Height;
bmp.Dispose();
CreateSurfaces();
}
// This function Draws the offscreen bitmap surface to the primary visible surface.
private void Draw()
{
if (null == primary) return;
if (FormWindowState.Minimized == WindowState) return;
if (flg == false) return;
flg = false;
Height = Height < 50 ? 50 : Height;
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()
{
// Create new DrawDevice. coop level normal windowed mode.
draw = new Device();
draw.SetCooperativeLevel(this, CooperativeLevelFlags.Normal);
// Create primary surface.
SurfaceDescription description = new SurfaceDescription();
description.SurfaceCaps.PrimarySurface = true;
primary = new Surface(description, draw);
// Create image surface.
description.Clear();
image = new Surface(ImgFile, description, draw);
// Create offscreen surface.
description.Clear();
description.SurfaceCaps.OffScreenPlain = true;
description.Width = this.Width;
description.Height = this.Height;
offscreen = new Surface(description, draw);
offscreen.ColorFill(Color.White);
dest = new Rectangle(xp, yp, width, height);
offscreen.Draw(dest, image, DrawFlags.Wait);
}
//protected override void OnKeyDown(KeyEventArgs e)
private void OnKeyEvent(object sender, System.Windows.Forms.KeyEventArgs e)
{
switch(e.KeyCode)
{ case Keys.Escape:
Application.Exit();
break;
case Keys.Left:
xp--;
break;
case Keys.Right:
xp++;
break;
case Keys.Up:
yp--;
break;
case Keys.Down:
yp++;
break;
default:
return;
}
offscreen.ColorFill(Color.White);
dest = new Rectangle(xp, yp, width, height);
sou = new Rectangle(0, 0, width, height);
if (xp < 0)
{
dest.Width += xp; dest.X = 0;
sou.Width += xp; sou.X = 0 - xp;
}
if (yp < 0)
{
dest.Height += yp; dest.Y = 0;
sou.Height += yp; sou.Y = 0 - yp;
}
offscreen.Draw(dest, image, sou, DrawFlags.Wait);
flg= true;
}
//☆ Main() メソッド
static void Main()
{
// using で資源の解放を確実に行う
using (DXDraw frm = new DXDraw())
{
frm.CreateSurfaces(); // Create Draw Surfaces
frm.Show();
// メッセージループ
while(frm.Created)
{
frm.Draw();
Application.DoEvents();
}
}
}
}
}
|