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

本文介绍使用DirectX在全屏模式下创建设备、剪辑器、前后缓冲区,并实现从后缓冲区翻转到前缓冲区显示的线绘制过程。代码详细展示了设备设置、表面创建、颜色填充及DirectDraw绘图操作。


None.gifusing 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 DrawFullScreenLine : Form dot.gif{
InBlock.gif    
int _width = 1280;
InBlock.gif    
int _height = 800;
InBlock.gif    
InBlock.gif    
// DirectDraw Device
InBlock.gif
    Device _device = null;
InBlock.gif    Clipper _clipper 
= null;
InBlock.gif    
InBlock.gif    
// Front Surface
InBlock.gif
    Surface _frontSurface = null;
InBlock.gif    
// Back Surface
InBlock.gif
    Surface _backSurface = null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void Main() dot.gif{
InBlock.gif      DrawFullScreenLine frm 
= new DrawFullScreenLine();
InBlock.gif      Application.Exit();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public DrawFullScreenLine() 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 Front Surface
InBlock.gif
      this.createFrontSurface();
InBlock.gif      
// Create Back Surface
InBlock.gif
      this.createBackSurface();
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      
this.SetBounds(00, _width, _height);
InBlock.gif      
this.FormBorderStyle = FormBorderStyle.None;
InBlock.gif      
InBlock.gif      
// Create new DirectDraw device
InBlock.gif
      _device = new Device();
InBlock.gif      
// Set FullScreen mode, owner is this form
InBlock.gif
      _device.SetCooperativeLevel(this, CooperativeLevelFlags.FullscreenExclusive);
InBlock.gif      
// Set width & height & color bit
InBlock.gif
      _device.SetDisplayMode(_width, _height, 160false);
ExpandedSubBlockEnd.gif    }

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

InBlock.gif
InBlock.gif    
// Step 3.Front Surface Creation
ExpandedSubBlockStart.gifContractedSubBlock.gif
    void createFrontSurface() dot.gif{
InBlock.gif      
// Create new SurfaceDescription to create Surface
InBlock.gif
      SurfaceDescription desc = new SurfaceDescription();
InBlock.gif      
// To create new Front Surface
InBlock.gif
      desc.SurfaceCaps.PrimarySurface = true;
InBlock.gif      
// To create new Back Buffer Surface
InBlock.gif
      desc.SurfaceCaps.Complex = true;
InBlock.gif      
// To enable Flip operation
InBlock.gif
      desc.SurfaceCaps.Flip = true;
InBlock.gif      
// Set Back Buffer count
InBlock.gif
      desc.BackBufferCount = 1;
InBlock.gif
InBlock.gif      
// Create new Front Surface      
InBlock.gif
      _frontSurface = new Surface(desc, _device);
InBlock.gif      
// Fill Front Surface with color
InBlock.gif
      _frontSurface.ColorFill(Color.Pink);
ExpandedSubBlockEnd.gif    }

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

InBlock.gif
InBlock.gif    
// Step 5.Draw line
ExpandedSubBlockStart.gifContractedSubBlock.gif
    void renderGraphics() dot.gif{
InBlock.gif      
if (!this.Created)
InBlock.gif        
return;
InBlock.gif
InBlock.gif      
if (_frontSurface == null || _backSurface == null)
InBlock.gif        
return;
InBlock.gif
InBlock.gif      
// Fill back color  
InBlock.gif
      _backSurface.ColorFill(Color.Pink);
InBlock.gif      
// Fill fore color
InBlock.gif
      _backSurface.FillColor = Color.Blue;
InBlock.gif      
// Draw line by DirectDraw
InBlock.gif
      _backSurface.DrawLine(00300300);
InBlock.gif
InBlock.gif      
// Draw Front Surface by Back Surface
InBlock.gif
      Rectangle srcRect = new Rectangle(00, _width, _height);
InBlock.gif      _frontSurface.Draw(
this.RectangleToScreen(this.ClientRectangle), _backSurface, 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    }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值