DirectDraw实例:显示图片

  1 using  System;
  2 using  System.Drawing;
  3 using  System.Collections;
  4 using  System.ComponentModel;
  5 using  System.Windows.Forms;
  6 using  System.Data;
  7 //  添加新的命名空间。
  8 using  DxVBLib;
  9
 10 namespace  DirectDraw1
 11 {
 12    /**//// <summary>
 13    /// DirectDraw实例:显示图片。
 14    /// </summary>

 15    public class Form1 : System.Windows.Forms.Form
 16    {
 17        private System.Windows.Forms.Panel panel1;
 18        /**//// <summary>
 19        /// 必需的设计器变量。
 20        /// </summary>

 21        private System.ComponentModel.Container components = null;
 22
 23        public Form1()
 24        {
 25            //
 26            // Windows 窗体设计器支持所必需的
 27            //
 28            InitializeComponent();
 29            //
 30            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码。
 31            //
 32            DirectX = new DirectX7();
 33            InitializeDirectX();
 34        }

 35
 36        /**//// <summary>
 37        /// 清理所有正在使用的资源。
 38        /// </summary>

 39        protected override void Dispose( bool disposing )
 40        {
 41            if( disposing )
 42            {
 43                if (components != null)
 44                {
 45                    components.Dispose();
 46                }

 47            }

 48            base.Dispose( disposing );
 49        }

 50
 51        Windows Form Designer generated code#region Windows Form Designer generated code
 52        /**//// <summary>
 53        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 54        /// 此方法的内容。
 55        /// </summary>

 56        private void InitializeComponent()
 57        {
 58            this.panel1 = new System.Windows.Forms.Panel();
 59            this.SuspendLayout();
 60            //
 61            // panel1
 62            //
 63            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
 64            this.panel1.Name = "panel1";
 65            this.panel1.Size = new System.Drawing.Size(440320);
 66            this.panel1.TabIndex = 0;
 67            this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
 68            //
 69            // Form1
 70            //
 71            this.AutoScaleBaseSize = new System.Drawing.Size(818);
 72            this.ClientSize = new System.Drawing.Size(440320);
 73            this.Controls.AddRange(new System.Windows.Forms.Control[] {
 74                  this.panel1}
);
 75            this.Name = "Form1";
 76            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
 77            this.Text = "显示图片";
 78            this.Resize += new System.EventHandler(this.Form1_Resize);
 79            this.ResumeLayout(false);
 80
 81        }

 82        #endregion

 83
 84        /**//// <summary>
 85        /// 应用程序的主入口点。
 86        /// </summary>

 87        [STAThread]
 88        static void Main()
 89        {
 90            Application.Run(new Form1());
 91        }

 92        private DirectX7 DirectX = null;
 93        private DirectDraw7 DirectDraw = null;
 94        private DirectDrawSurface7 Surface = null;
 95        private DirectDrawSurface7 PrimarySurface = null;
 96        private DDSURFACEDESC2 Surface1;
 97        private DDSURFACEDESC2 Surface2;
 98        private DirectDrawClipper Clipper = null;
 99        private Boolean bInit;
100        private void Blt()
101        {
102            // 判断是否初始化成功。
103            if (bInit == false)
104                return;
105
106            DxVBLib.RECT r1 = new DxVBLib.RECT();
107            DxVBLib.RECT r2 = new DxVBLib.RECT();
108
109            // 得到窗口边界大小。
110            DirectX.GetWindowRect(panel1.Handle.ToInt32(), ref r1);
111            // 按照新的边界大小显示图片。
112            r2.Bottom = Surface2.lHeight;
113            r2.Right = Surface2.lWidth;
114            PrimarySurface.Blt(ref r1, Surface, ref r2, CONST_DDBLTFLAGS.DDBLT_WAIT);
115        }

116        private void InitializeDirectX()
117        {
118            // 初始化各个变量。
119            DirectDraw = DirectX.DirectDrawCreate("");
120            DirectDraw.SetCooperativeLevel(this.Handle.ToInt32(), CONST_DDSCLFLAGS.DDSCL_NORMAL);
121
122            Surface1.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS;
123            Surface1.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_PRIMARYSURFACE;
124            PrimarySurface = DirectDraw.CreateSurface(ref Surface1);
125
126            Surface2.lFlags = CONST_DDSURFACEDESCFLAGS.DDSD_CAPS;
127            Surface2.ddsCaps.lCaps = CONST_DDSURFACECAPSFLAGS.DDSCAPS_OFFSCREENPLAIN;
128
129            try
130            {
131                Surface = DirectDraw.CreateSurfaceFromFile("sample.bmp"ref Surface2); //background.bmp
132            }

133            catch(System.Runtime.InteropServices.COMException e)
134            {
135                // 没有找到文件。
136                if ( (uint)e.ErrorCode == 0x800A0035)
137                {
138                    MessageBox.Show("没有找到文件'sample.bmp'.\n该文件必须和程序放在一个目录下面。""图片没有找到");
139                }

140                else
141                {
142                    MessageBox.Show("异常: " + e.ToString(), "异常信息");
143                }

144                Application.Exit();
145                Application.DoEvents();
146            }

147            Clipper = DirectDraw.CreateClipper(0);
148            Clipper.SetHWnd(panel1.Handle.ToInt32());
149            PrimarySurface.SetClipper(Clipper);
150            // 初始化完成。
151            bInit = true;
152            Blt();
153        }

154
155        private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
156        {
157            DirectDraw.RestoreAllSurfaces();
158            Blt();
159        }

160
161        private void Form1_Resize(object sender, System.EventArgs e)
162        {
163            panel1.Width = this.ClientSize.Width;
164            panel1.Height = this.ClientSize.Height;
165            Blt();
166        }

167    }

168}

169

Interop.DxVBLib.rar

转载于:https://www.cnblogs.com/gofficer/archive/2007/08/23/866715.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值