使用C#配合DirectX 9的DirectDraw在Full-Screen模式下繪製"矩形"的簡單範例.

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

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

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


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

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

// Step 1. Device Creation

void createDevice()
{
this.SetBounds(0, 0, _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, 16, 0, false);
}

// Step 2.Clipper Creation

void createClipper()
{
_clipper = new Clipper(_device);

_clipper.ClipList = new Rectangle[]
{ new Rectangle(0, 0, _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.Draw line

void renderGraphics()
{
if (!this.Created)
return;

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

// Fill back color
_backSurface.ColorFill(Color.Pink);
// Fill fore color
_backSurface.FillColor = Color.Blue;
// Draw Box by DirectDraw
_backSurface.DrawBox(0, 0, 50, 100);


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

// Message Loop

void startLoop()
{

while (this.Created)
{
this.renderGraphics();
Application.DoEvents();
}
}
}
}