(原創) 如何使用C#與DrectDraw在Windows模式下繪製直線? (.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 DrawWindowsLine : 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      DrawWindowsLine frm 
= new DrawWindowsLine();
InBlock.gif      Application.Exit();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public DrawWindowsLine() 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.gif{
InBlock.gif                                            
new Rectangle(00, _width, _height)
ExpandedSubBlockEnd.gif                                          }
;
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 line by DirectDraw
InBlock.gif
      _secondarySurface.DrawLine(00300300);
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 DrawLine_Activated(object sender, EventArgs e) dot.gif{
InBlock.gif      
this.renderGraphics();
ExpandedSubBlockEnd.gif    }

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

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值