110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace MDX4
{
public class MDX4 : Form
{
Device device = null;
PresentParameters presentParams = new PresentParameters();
float counter;
bool useTransformation = true;
Texture texture;
public MDX4()
{
this.ClientSize = new System.Drawing.Size(640,480);
this.Text = "移动背景 按“T”键重置";
}
public bool InitializeGraphics()
{
try
{
presentParams.Windowed=true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
device.DeviceReset += new System.EventHandler(this.OnResetDevice);
this.OnResetDevice(device, null);
texture = TextureLoader.FromFile(device,"cloud3.bmp");
return true;
}
catch (DirectXException)
{
return false;
}
}
public void OnResetDevice(object sender, EventArgs e)
{
Device dev = (Device)sender;
dev.RenderState.Lighting = false;
dev.RenderState.DitherEnable = true;
device.RenderState.CullMode = Cull.None;
}
private CustomVertex.PositionTextured[] CreateTransformedQuad()
{
CustomVertex.PositionTextured[] verts = new CustomVertex.PositionTextured[4];
verts[0].Position = new Vector3 ( -1.0f,-1.0f, 3.0f ); verts[0].Tu =0-counter; verts[0].Tv = 1.0f;
verts[1].Position = new Vector3 ( -1.0f, 1.0f, 3.0f ); verts[1].Tu =0-counter; verts[1].Tv = 0.0f;
verts[2].Position = new Vector3 ( 1.0f, -1.0f, 3.0f ); verts[2].Tu =1-counter; verts[2].Tv = 1.0f;
verts[3].Position = new Vector3 ( 1.0f, 1.0f, 3.0f ); verts[3].Tu =1-counter; verts[3].Tv = 0.0f;
return verts;
}
private CustomVertex.PositionTextured[] CreateQuad()
{
CustomVertex.PositionTextured[] verts = new CustomVertex.PositionTextured[4];
verts[0].Position = new Vector3 ( -1.0f,-1.0f, 3.0f ); verts[0].Tu =1.0f; verts[0].Tv = 1.0f;
verts[1].Position = new Vector3 ( -1.0f, 1.0f, 3.0f ); verts[1].Tu =1.0f; verts[1].Tv = 0.0f;
verts[2].Position = new Vector3 ( 1.0f, -1.0f, 3.0f ); verts[2].Tu =0.0f; verts[2].Tv = 1.0f;
verts[3].Position = new Vector3 ( 1.0f, 1.0f, 3.0f ); verts[3].Tu =0.0f; verts[3].Tv = 0.0f;
return verts;
}
private void Render()
{
if (device == null) return;
device.Clear(ClearFlags.Target , System.Drawing.Color.Black, 1.0f, 0);
device.BeginScene();
device.Transform.World = Matrix.Identity;
device.Transform.View = Matrix.LookAtLH ( new Vector3 ( 0, 0, 5f ), new Vector3 ( 0, 0, 0 ), new Vector3 ( 0, 1, 0 ) );
device.Transform.Projection = Matrix.PerspectiveFovLH ( (float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f );
device.VertexFormat = CustomVertex.PositionTextured.Format;
device.SetTexture(0,texture);
device.SamplerState[0].AddressU = TextureAddress.Wrap;
if(useTransformation)
{
device.TextureState[0].TextureTransform = TextureTransform.Count2;
Matrix m = Matrix.Identity;
m.M31 = counter;
device.Transform.Texture0 = m;
device.DrawUserPrimitives(PrimitiveType.TriangleStrip,2,CreateQuad());
device.TextureState[0].TextureTransform = TextureTransform.Disable;
}
else
device.DrawUserPrimitives(PrimitiveType.TriangleStrip,2,CreateTransformedQuad());
counter += 0.0015f;
device.EndScene();
device.Present();
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
this.Render();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if(keyData == Keys.Escape)
Application.Exit();
if(keyData == Keys.T)
useTransformation = !useTransformation;
return base.ProcessCmdKey( ref msg, keyData );
}
static void Main()
{
using (MDX4 frm = new MDX4())
{
if (!frm.InitializeGraphics())
{
MessageBox.Show("初始化DirectX失败,程序退出!");
return;
}
frm.Show();
//火车
PictureBox pb = new PictureBox();
pb.Image = Image.FromFile("R_5.bmp");
pb.Location = new Point(100, 100);
pb.Size = new Size(60, 60);
frm.Controls.Add(pb);
//移动的背景
while (frm.Created)
{
frm.Render();
Application.DoEvents();
}
}
}
private void InitializeComponent()
{
this.SuspendLayout();
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(400, 200);
this.Name = "MDX4";
this.ResumeLayout(false);
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/////// 沾贴如下代码
protected override void OnLoad(EventArgs e)
{
this.DoubleBuffered = true;
try
{
var url = "https://img-bbs.csdn.net/upload/201703/21/1490080881_639835.jpg";
var bytes = new WebClient().DownloadData(url);
farBackground = Image.FromStream(new System.IO.MemoryStream(bytes)) as Bitmap;
nearBackground = CreateNearBackGround(farBackground.Width, farBackground.Height);
}
catch { }
timer.Tick += delegate
{
if (farBackground == null) return;
offsetFar = (offsetFar + 2) % farBackground.Width;
offsetNear = (offsetNear + 5) % farBackground.Width;
this.Invalidate();
};
timer.Enabled = true;
}
protected override void OnPaint(PaintEventArgs e)
{
if (farBackground != null) e.Graphics.DrawImage(farBackground, -offsetFar, 0);
if (nearBackground != null) e.Graphics.DrawImage(nearBackground, -offsetNear, 0);
if (train != null) e.Graphics.DrawImage(train, 130, 180);
}
Bitmap CreateNearBackGround(int width, int height)
{
Bitmap bmp = new Bitmap(width, height);
using (var g = Graphics.FromImage(bmp))
{
Random r = new Random();
for (int i = 0; i < 30; i++)
{
g.DrawIcon(SystemIcons.Question, r.Next(width), r.Next(height));
}
}
return bmp;
}
Bitmap farBackground, nearBackground, train = SystemIcons.Error.ToBitmap();
int offsetFar, offsetNear;
Timer timer = new System.Windows.Forms.Timer() { Interval = 25 };
/////// 沾贴如上代码
}
以上是WinForm代码。但总的来说,动画上用WPF更好做一些。你要认真考虑是否用WPF来做。