博客
关于我
强烈建议你试试无所不能的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/

你可能感兴趣的文章
elasticsearch安装步骤
查看>>
PHP获取Cookie模拟登录CURL(转)
查看>>
PHP-权限控制类(转)
查看>>
CSS3秘笈第三版涵盖HTML5学习笔记9~12章
查看>>
bzoj1044木棍分割
查看>>
leetcode-136-Single Number
查看>>
微信小程序笔记<五> 页面管理及生命周期(route)——getCurrentPages()
查看>>
http服务器小项目
查看>>
JS案例:Jq中的fadeOut和fadeIn实现简单轮播(没完善,简单实现)
查看>>
一些数学上的名词及操作
查看>>
C# DataGridVie利用model特性动态加载列
查看>>
IPv6 地址分类
查看>>
<%@ include %>指令和<jsp:include>区别
查看>>
因为文件组 'PRIMARY' 已满 解决办法
查看>>
Flume 读取实时更新的日志文件
查看>>
HDU 2049
查看>>
《Spring1之第十次站立会议》
查看>>
Unity Shader 噪声消融特效 - 剑灵死亡特效
查看>>
Eclipse 自动生成 Ant的Build.xml 配置文件
查看>>
添加一条信息到列表,如果重复就替换,
查看>>