DirectDraw实例:显示图片

  1using System;
  2using System.Drawing;
  3using System.Collections;
  4using System.ComponentModel;
  5using System.Windows.Forms;
  6using System.Data;
  7// 添加新的命名空间。
  8using DxVBLib;
  9
 10namespace 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
 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

posted on 2007-08-23 13:38  Gofficer  阅读(3110)  评论(1编辑  收藏  举报