博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(原創) 如何使用C#與DrectDraw在Windows模式下繪製矩形? (.NET) (DirectX)
阅读量:6503 次
发布时间:2019-06-24

本文共 3513 字,大约阅读时间需要 11 分钟。

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 DrawWindowsBox : Form dot.gif{
InBlock.gif    
// window width & window height
InBlock.gif
    int _width = 640;
InBlock.gif    
int _height = 480;
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
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void Main() dot.gif{
InBlock.gif      DrawWindowsBox frm 
= new DrawWindowsBox();
InBlock.gif      Application.Exit();
ExpandedSubBlockEnd.gif    }
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public DrawWindowsBox() dot.gif{
InBlock.gif      InitializeComponent();
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      
// 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.gifnew Rectangle(00, _width, _height) };
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 (_primarySurface == null || _secondarySurface == null)
InBlock.gif        
return;
InBlock.gif
InBlock.gif      
// Fill back color  
InBlock.gif
      _secondarySurface.ColorFill(Color.Pink);
InBlock.gif      
// Fill fore color
InBlock.gif
      _secondarySurface.FillColor = Color.Blue;
InBlock.gif      
// Draw Box by DirectDraw
InBlock.gif
      _secondarySurface.DrawBox(0050100);
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 DrawWindowsBox_Activated(object sender, EventArgs e) dot.gif{
InBlock.gif      
this.renderGraphics();
ExpandedSubBlockEnd.gif    }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private void DrawWindowsBox_Resize(object sender, EventArgs e) dot.gif{
InBlock.gif      
this.renderGraphics();
ExpandedSubBlockEnd.gif    }
ExpandedSubBlockEnd.gif  }
ExpandedBlockEnd.gif}

转载地址:http://famyo.baihongyu.com/

你可能感兴趣的文章
oracle
查看>>
java SpringUtil获取bean
查看>>
Centos6.4最小化安装系统初始化脚本
查看>>
赛门铁克开启“容灾即服务”时代
查看>>
复杂度归纳--小结
查看>>
PHP学习笔记 第八讲 Mysql.简介和创建新的数据库
查看>>
js获取鼠标位置
查看>>
Mysql
查看>>
跨越企业的“中等收入陷阱”
查看>>
Android 开发者必知的开发资源
查看>>
软件工程技术基础-(软件复用技术)
查看>>
给django视图类添加装饰器
查看>>
luogu P1280 尼克的任务 序列DP
查看>>
sys.check_constraints
查看>>
vue问题
查看>>
运行PHP出现No input file specified错误解决办法
查看>>
selenium之frame操作
查看>>
php 引入其他文件中的变量
查看>>
MYSQL体系结构-来自期刊
查看>>
mysql的基本知识
查看>>