using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Microsoft.DirectX;using Microsoft.DirectX.DirectDraw;namespace DrectDrawLab { public partial class DrawWindowsBitmap : Form { // window width & window height int _width = 640; int _height = 480; // Bitmap file name string _bitmapFileName = "directx.bmp"; // Sprite width & height int _spriteWidth = 0; int _spriteHeight = 0; // DirectDraw Device Device _device = null; // Clipper Clipper _clipper = null; // Primary Surface Surface _primarySurface = null; // Secondary Surface Surface _secondarySurface = null; // Bitmap Surface Surface _bitmapSurface = null; public static void Main() { DrawWindowsBitmap frm = new DrawWindowsBitmap(); Application.Exit(); } public DrawWindowsBitmap() { InitializeComponent(); // Create DirectDraw Device this.createDevice(); // Create Clipper this.createClipper(); // Create Primary Surface this.createPrimarySurface(); // Create Secondary Surface this.createSecondarySurface(); // Create Bitmap Surface this.createBitmapSurface(); // Show the window this.Show(); // Start message loop this.startLoop(); } // Step 1. Device Creation void createDevice() { // Create new DirectDraw device _device = new Device(); // set windowed mode, owner is this form _device.SetCooperativeLevel(this, CooperativeLevelFlags.Normal); } // Step 2.Clipper Creation void createClipper() { // Create new clipper _clipper = new Clipper(_device); // Set window size this.ClientSize = new Size(_width, _height); // Set window associated to clipper _clipper.Window = this; } // Step 3.Primary Surface Creation void createPrimarySurface() { // Create new SurfaceDescription to create Surface SurfaceDescription desc = new SurfaceDescription(); // Use System Memory desc.SurfaceCaps.SystemMemory = true; // To create Primary Surface desc.SurfaceCaps.PrimarySurface = true; // Create new Primary Surface _primarySurface = new Surface(desc, _device); // Set clipper associated to surface _primarySurface.Clipper = _clipper; } // Step 4.Secondary Surface Creation void createSecondarySurface() { // Create new SurfaceDescription to create Surface SurfaceDescription desc = new SurfaceDescription(); // Use System Memory desc.SurfaceCaps.SystemMemory = true; // To create Secondary Surface desc.SurfaceCaps.OffScreenPlain = true; // Set Secondary Surface width as Primary Surface width desc.Width = _primarySurface.SurfaceDescription.Width; // Set Secondary Surface height as Primary Surface height desc.Height = _primarySurface.SurfaceDescription.Height; // Crate Secondary Surface _secondarySurface = new Surface(desc, _device); // Create new Clipper to Secondary Surface _secondarySurface.Clipper = new Clipper(); // Create Clipper List _secondarySurface.Clipper.ClipList = new Rectangle[] {new Rectangle(0, 0, _width, _height)}; } // Step 5.Bitmap Surface Creation void createBitmapSurface() { SurfaceDescription desc = new SurfaceDescription(); _bitmapSurface = new Surface(_bitmapFileName, desc, _device); _spriteWidth = _bitmapSurface.SurfaceDescription.Width; _spriteHeight = _bitmapSurface.SurfaceDescription.Height; ColorKey key = new ColorKey(); key.ColorSpaceHighValue = key.ColorSpaceLowValue = 0; ColorKeyFlags flags = new ColorKeyFlags(); flags = ColorKeyFlags.SourceDraw; _bitmapSurface.SetColorKey(flags, key); } // Step 6.Draw Bitmap void renderGraphics() { if (!this.Created) return; if (_primarySurface == null || _secondarySurface == null) return; // Fill back color _secondarySurface.ColorFill(Color.Pink); // Draw Bitmap by DirectDraw Rectangle dest = new Rectangle(0, 0, _spriteWidth, _spriteHeight); Rectangle src = new Rectangle(0, 0, _spriteWidth, _spriteHeight); _secondarySurface.Draw(dest, _bitmapSurface, DrawFlags.DoNotWait | DrawFlags.KeySource); // Draw Primary Surface by Secondary Surface Rectangle srcRect = new Rectangle(0, 0, _width, _height); _primarySurface.Draw(this.RectangleToScreen(this.ClientRectangle), _secondarySurface, srcRect, DrawFlags.DoNotWait); } // Message Loop void startLoop() { while (this.Created) { this.renderGraphics(); Application.DoEvents(); } } private void DrawWindowsBitmap_Activated(object sender, EventArgs e) { this.renderGraphics(); } private void DrawWindowsBitmap_Resize(object sender, EventArgs e) { this.renderGraphics(); } }}