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


None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
using  Microsoft.DirectX;
None.gif
using  Microsoft.DirectX.DirectDraw;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
namespace  DrectDrawLab  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif  
public partial class DrawWindowsBitmap : Form dot.gif{
InBlock.gif    
// window width & window height
InBlock.gif
    int _width = 640;
InBlock.gif    
int _height = 480;
InBlock.gif    
// Bitmap file name
InBlock.gif
    string _bitmapFileName = "directx.bmp";
InBlock.gif    
// Sprite width & height
InBlock.gif
    int _spriteWidth = 0;
InBlock.gif    
int _spriteHeight = 0;
InBlock.gif
InBlock.gif    
// DirectDraw Device
InBlock.gif
    Device _device = null;
InBlock.gif    
// Clipper
InBlock.gif
    Clipper _clipper = null;
InBlock.gif    
// Primary Surface
InBlock.gif
    Surface _primarySurface = null;
InBlock.gif    
// Secondary Surface
InBlock.gif
    Surface _secondarySurface = null;
InBlock.gif    
// Bitmap Surface
InBlock.gif
    Surface _bitmapSurface = null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void Main() dot.gif{
InBlock.gif      DrawWindowsBitmap frm 
= new DrawWindowsBitmap();
InBlock.gif      Application.Exit();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public DrawWindowsBitmap() dot.gif{
InBlock.gif      InitializeComponent();
InBlock.gif
InBlock.gif      
// Create DirectDraw Device
InBlock.gif
      this.createDevice();
InBlock.gif      
// Create Clipper
InBlock.gif
      this.createClipper();
InBlock.gif      
// Create Primary Surface
InBlock.gif
      this.createPrimarySurface();
InBlock.gif      
// Create Secondary Surface
InBlock.gif
      this.createSecondarySurface();
InBlock.gif      
// Create Bitmap Surface
InBlock.gif
      this.createBitmapSurface();
InBlock.gif      
// Show the window
InBlock.gif
      this.Show();
InBlock.gif      
// Start message loop
InBlock.gif
      this.startLoop();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Step 1. Device Creation
ExpandedSubBlockStart.gifContractedSubBlock.gif
    void createDevice() dot.gif{
InBlock.gif      
// Create new DirectDraw device
InBlock.gif
      _device = new Device();
InBlock.gif      
// set windowed mode, owner is this form
InBlock.gif
      _device.SetCooperativeLevel(this, CooperativeLevelFlags.Normal);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Step 2.Clipper Creation
ExpandedSubBlockStart.gifContractedSubBlock.gif
    void createClipper() dot.gif{
InBlock.gif      
// Create new clipper
InBlock.gif
      _clipper = new Clipper(_device);
InBlock.gif      
// Set window size
InBlock.gif
      this.ClientSize = new Size(_width, _height);
InBlock.gif      
// Set window associated to clipper
InBlock.gif
      _clipper.Window = this;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Step 3.Primary Surface Creation
ExpandedSubBlockStart.gifContractedSubBlock.gif
    void createPrimarySurface() dot.gif{
InBlock.gif      
// Create new SurfaceDescription to create Surface
InBlock.gif
      SurfaceDescription desc = new SurfaceDescription();
InBlock.gif      
// Use System Memory
InBlock.gif
      desc.SurfaceCaps.SystemMemory = true;
InBlock.gif      
// To create Primary Surface
InBlock.gif
      desc.SurfaceCaps.PrimarySurface = true;
InBlock.gif
InBlock.gif      
// Create new Primary Surface
InBlock.gif
      _primarySurface = new Surface(desc, _device);
InBlock.gif      
// Set clipper associated to surface
InBlock.gif
      _primarySurface.Clipper = _clipper;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Step 4.Secondary Surface Creation
ExpandedSubBlockStart.gifContractedSubBlock.gif
    void createSecondarySurface() dot.gif{
InBlock.gif      
// Create new SurfaceDescription to create Surface
InBlock.gif
      SurfaceDescription desc = new SurfaceDescription();
InBlock.gif      
// Use System Memory
InBlock.gif
      desc.SurfaceCaps.SystemMemory = true;
InBlock.gif      
// To create Secondary Surface
InBlock.gif
      desc.SurfaceCaps.OffScreenPlain = true;
InBlock.gif      
// Set Secondary Surface width as Primary Surface width
InBlock.gif
      desc.Width = _primarySurface.SurfaceDescription.Width;
InBlock.gif      
// Set Secondary Surface height as Primary Surface height
InBlock.gif
      desc.Height = _primarySurface.SurfaceDescription.Height;
InBlock.gif
InBlock.gif      
// Crate Secondary Surface
InBlock.gif
      _secondarySurface = new Surface(desc, _device);
InBlock.gif      
// Create new Clipper to Secondary Surface
InBlock.gif
      _secondarySurface.Clipper = new Clipper();
InBlock.gif      
// Create Clipper List
ExpandedSubBlockStart.gifContractedSubBlock.gif
      _secondarySurface.Clipper.ClipList = new Rectangle[] dot.gif{new Rectangle(00, _width, _height)};
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
// Step 5.Bitmap Surface Creation
ExpandedSubBlockStart.gifContractedSubBlock.gif
    void createBitmapSurface() dot.gif{
InBlock.gif      SurfaceDescription desc 
= new SurfaceDescription();
InBlock.gif      _bitmapSurface 
= new Surface(_bitmapFileName, desc, _device);
InBlock.gif      _spriteWidth 
= _bitmapSurface.SurfaceDescription.Width;
InBlock.gif      _spriteHeight 
= _bitmapSurface.SurfaceDescription.Height;
InBlock.gif      
InBlock.gif      ColorKey key 
= new ColorKey();
InBlock.gif      key.ColorSpaceHighValue 
= key.ColorSpaceLowValue = 0;
InBlock.gif      ColorKeyFlags flags 
= new ColorKeyFlags();
InBlock.gif      flags 
= ColorKeyFlags.SourceDraw;
InBlock.gif      _bitmapSurface.SetColorKey(flags, key);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Step 6.Draw Bitmap
ExpandedSubBlockStart.gifContractedSubBlock.gif
    void renderGraphics() dot.gif{
InBlock.gif      
if (!this.Created)
InBlock.gif        
return;
InBlock.gif
InBlock.gif      
if (_primarySurface == null || _secondarySurface == null)
InBlock.gif        
return;
InBlock.gif
InBlock.gif      
// Fill back color  
InBlock.gif
      _secondarySurface.ColorFill(Color.Pink);
InBlock.gif      
// Draw Bitmap by DirectDraw
InBlock.gif
      Rectangle dest = new Rectangle(00, _spriteWidth, _spriteHeight);
InBlock.gif      Rectangle src 
= new Rectangle(00, _spriteWidth, _spriteHeight);
InBlock.gif      _secondarySurface.Draw(dest, _bitmapSurface, DrawFlags.DoNotWait 
| DrawFlags.KeySource);
InBlock.gif      
InBlock.gif      
// Draw Primary Surface by Secondary Surface
InBlock.gif
      Rectangle srcRect = new Rectangle(00, _width, _height);
InBlock.gif      _primarySurface.Draw(
this.RectangleToScreen(this.ClientRectangle), _secondarySurface, srcRect, DrawFlags.DoNotWait);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Message Loop
ExpandedSubBlockStart.gifContractedSubBlock.gif
    void startLoop() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
while (this.Created) dot.gif{
InBlock.gif        
this.renderGraphics();
InBlock.gif        Application.DoEvents();
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private void DrawWindowsBitmap_Activated(object sender, EventArgs e) dot.gif{
InBlock.gif      
this.renderGraphics();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private void DrawWindowsBitmap_Resize(object sender, EventArgs e) dot.gif{
InBlock.gif      
this.renderGraphics();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值