(原創) 如何使用C#與DrectDraw在Full-Screen模式下繪製Bitmap? (.NET) (DirectX)


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 DrawFullScreenBitmap : Form {
    
int _width = 1280;
    
int _height = 800;

    
string _bitmapFileName = "directx.bmp";
    
// Sprite width & height
    int _spriteWidth = 0;
    
int _spriteHeight = 0;

    
// DirectDraw Device
    Device _device = null;
    Clipper _clipper 
= null;

    
// Front Surface
    Surface _frontSurface = null;
    
// Back Surface
    Surface _backSurface = null;
    
// Bitmap Surface
    Surface _bitmapSurface = null;

    
public static void Main() {
      DrawFullScreenBitmap frm 
= new DrawFullScreenBitmap();
      Application.Exit();
    }

    
    
public DrawFullScreenBitmap() {
      InitializeComponent();

      
// Create DirectDraw Device
      this.createDevice();
      
// Create Clipper
      this.createClipper();
      
// Create Front Surface
      this.createFrontSurface();
      
// Create Back Surface
      this.createBackSurface();
      
// Create Bitmap Surface
      this.createBitmapSurface();
      
// Show the window
      this.Show();
      
// Start message loop
      this.startLoop();
    }


    
// Step 1. Device Creation
    void createDevice() {
      
this.SetBounds(00, _width, _height);
      
this.FormBorderStyle = FormBorderStyle.None;

      
// Create new DirectDraw device
      _device = new Device();
      
// Set FullScreen mode, owner is this form
      _device.SetCooperativeLevel(this, CooperativeLevelFlags.FullscreenExclusive);
      
// Set width & height & color bit
      _device.SetDisplayMode(_width, _height, 160false);
    }


    
// Step 2.Clipper Creation
    void createClipper() {
      _clipper 
= new Clipper(_device);
      _clipper.ClipList 
= new Rectangle[] new Rectangle(00, _width, _height) };
    }


    
// Step 3.Front Surface Creation
    void createFrontSurface() {
      
// Create new SurfaceDescription to create Surface
      SurfaceDescription desc = new SurfaceDescription();
      
// To create new Front Surface
      desc.SurfaceCaps.PrimarySurface = true;
      
// To create new Back Buffer Surface
      desc.SurfaceCaps.Complex = true;
      
// To enable Flip operation
      desc.SurfaceCaps.Flip = true;
      
// Set Back Buffer count
      desc.BackBufferCount = 1;

      
// Create new Front Surface      
      _frontSurface = new Surface(desc, _device);
      
// Fill Front Surface with color
      _frontSurface.ColorFill(Color.Pink);
    }


    
// Step 4.Back Surface Creation
    void createBackSurface() {
      
// Create new SurfaceCaps to describe Surface Capabilities
      SurfaceCaps caps = new SurfaceCaps();
      
// Enable BackBuffer
      caps.BackBuffer = true;
      
// Get the attached surface that matches the caps, which will be the backbuffer.
      _backSurface = _frontSurface.GetAttachedSurface(caps);
      
// Fill Back Surface with Color
      _backSurface.ColorFill(Color.Pink);
      
// Set Back Surface fore color
      _backSurface.ForeColor = Color.Black;
      
// Set clipper associated to Back Surface
      _backSurface.Clipper = _clipper;
    }


    
// 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 5.Draw Bitmap
    void renderGraphics() {
      
if (!this.Created)
        
return;

      
if (_frontSurface == null || _backSurface == null)
        
return;

      
// Fill back color  
      _backSurface.ColorFill(Color.Pink);
      
// Draw Bitmap by DirectDraw
      Rectangle dest = new Rectangle(00, _spriteWidth, _spriteHeight);
      Rectangle src 
= new Rectangle(00, _spriteWidth, _spriteHeight);
      _backSurface.Draw(dest, _bitmapSurface, DrawFlags.DoNotWait 
| DrawFlags.KeySource);
      

      
// Draw Front Surface by Back Surface
      Rectangle srcRect = new Rectangle(00, _width, _height);
      _frontSurface.Draw(
this.RectangleToScreen(this.ClientRectangle), _backSurface, srcRect, DrawFlags.DoNotWait);
    }


    
// Message Loop
    void startLoop() {
      
while (this.Created) {
        
this.renderGraphics();
        Application.DoEvents();
      }

    }

  }

}

posted on 2007-07-18 11:55  真 OO无双  阅读(2078)  评论(2编辑  收藏  举报

导航