⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gxgraphics.cs

📁 功能:基于windows mobile 的地图查看器。使用vs2005开发
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Windows;
using System.Drawing.Imaging;

#if !DESKTOP
using Microsoft.WindowsMobile.DirectX;
using Microsoft.WindowsMobile.DirectX.Direct3D;
#else
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
#endif

namespace GXGraphicsLibrary
{
	/// <summary>
	/// Draw flags used for drawing bitmaps.  These flags
	/// map to GapiDraw flags so they can be used directly.
	/// </summary>
	public enum DrawFlags
	{
		kNone = 0,
		GDBLTFAST_KEYSRC = 0x0001,
		GDBLT_MIRRORLEFTRIGHT = 0x0010,
		GDBLT_MIRRORUPDOWN = 0x0020
	}

	public interface IGXGraphics : IDisposable
	{
		/// <summary>
		/// Gets the height of the screen.
		/// </summary>
		int ScreenWidth { get; }
	   
		/// <summary>
		/// Gets the width of the screen.
		/// </summary>
		int ScreenHeight { get; }

		/// <summary>
		/// Draw the current cell of the animation to the back buffer.
		/// </summary>
		/// <param name="x">X destination of the draw</param>
		/// <param name="y">Y destination of the draw</param>
		/// <param name="anim">Animation to be drawn</param>
		void DrawAnimation(int x, int y, GXAnimation anim);
		
		/// <summary>
		/// Draw the bitmap to the back buffer.
		/// </summary>
		/// <param name="x">X destination of the draw</param>
		/// <param name="y">Y destination of the draw</param>
		/// <param name="srcRegion">Source region of the draw</param>
		/// <param name="bmp">Bitmap to be drawn</param>
		void DrawBitmap(int x, int y, Rectangle srcRegion, IGXBitmap bmp);
		
		/// <summary>
		/// Draw a filled rectangle to the back buffer.
		/// </summary>
		/// <param name="r">Rectangle to be filled</param>
		/// <param name="c">Color of rectangle</param>
		void DrawFilledRect(Rectangle r, Color c);
		
		/// <summary>
		/// Flip the back buffer to the display.
		/// </summary>
		void Flip();
		
		/// <summary>
		/// Draw the string to the back buffer.
		/// </summary>
		/// <param name="x">X destination of text</param>
		/// <param name="y">Y destination of text</param>
		/// <param name="text">Text to be displayed</param>
		/// <param name="color">Color of text</param>
		/// <param name="font">Font to be used</param>
		/// <param name="flags">Font draw flags</param>
		void DrawText(int x, int y, string text, Color color, IGXFont font, FontDrawFlags flags);
		
		/// <summary>
		/// Set the current draw mode.
		/// </summary>
		/// <param name="flags">Flags to be set</param>
		void SetDrawFlags(DrawFlags flags);
		
		/// <summary>
		/// Clear the current draw mode of the specified flags.
		/// </summary>
		/// <param name="flags">Flags to be cleared</param>
		void ClearDrawFlags(DrawFlags flags);
		
		/// <summary>
		/// Creates a bitmap compatible with this graphics device
		/// </summary>
		/// <param name="file_name">The file to load the image from</param>
		/// <param name="transparent">True if the image should be drawn with alpha transparency</param>
		/// <returns>A bitmap compatible with this graphics device</returns>
		IGXBitmap CreateBitmap(string file_name, bool transparent);
        
		/// <summary>
        /// Creates a font object compatible with this graphics device
        /// </summary>
        /// <param name="font_name"></param>
        /// <returns>A font object compatible with this graphics device</returns>
		IGXFont CreateFont(string font_name);
	}

	/// <summary>
	/// A GDI implementation of the IGXGraphics
	/// </summary>
	public class GDIGXGraphics : IGXGraphics
	{
		
		/// <summary>
		/// Gets the width of the screen.
		/// </summary>
		public int ScreenWidth 
		{
			//get { return Screen.PrimaryScreen.Bounds.Width; } }
			get { return m_back.Height; }
		}

		/// <summary>
		/// Gets the height of the screen.
		/// </summary>
		public int ScreenHeight 
		{
			//get { return Screen.PrimaryScreen.Bounds.Height; }
			get { return m_back.Height; }
		}

		/// <summary>
		/// Represents the back buffer.
		/// </summary>
		protected Bitmap m_back = null;
		/// <summary>
		/// Graphics object associated with the back buffer.
		/// </summary>
		protected Graphics m_gBack = null;
		/// <summary>
		/// Graphics object associated with the screen / owner control.
		/// </summary>
		protected Graphics m_screen = null;
		/// <summary>
		/// Initialize the graphics engine.
		/// </summary>
		/// <param name="cntrl">Owner control (Form)</param>
		public GDIGXGraphics(Control cntrl)
		{
			m_back = new Bitmap(240,320);
			m_gBack = Graphics.FromImage(m_back);
			m_screen = cntrl.CreateGraphics();
		}

		/// <summary>
		/// Empty.  Provided for compatibility.
		/// </summary>
		public void Dispose()
		{
		}

		/// <summary>
		/// Draw the current cell of the animation to the back buffer.
		/// </summary>
		/// <param name="x">X destination of the draw</param>
		/// <param name="y">Y destination of the draw</param>
		/// <param name="anim">Animation to be drawn</param>
		public void DrawAnimation(int x, int y, GXAnimation anim)
		{
			DrawBitmap(x, y, anim.Region, anim.Image);
		}

		/// <summary>
		/// Draw the bitmap to the back buffer.
		/// </summary>
		/// <param name="x">X destination of the draw</param>
		/// <param name="y">Y destination of the draw</param>
		/// <param name="srcRegion">Source region of the draw</param>
		/// <param name="bmp">Bitmap to be drawn</param>
		public void DrawBitmap(int x, int y, Rectangle srcRegion, IGXBitmap bmp)
		{
			GDIGXBitmap gdi_bmp = (GDIGXBitmap)bmp;

			// Clip the regions to the screen
			if (!ValidateRegions(ref x, ref y, ref srcRegion))
				return;

			// Draw the bitmap
			if (gdi_bmp.Transparent)
			{
				if ((m_drawFlags & DrawFlags.GDBLT_MIRRORLEFTRIGHT) != 0)
				{
					Rectangle dest = new Rectangle(x, y, srcRegion.Width, srcRegion.Height);
					ImageAttributes attr = new ImageAttributes();
					attr.SetColorKey(gdi_bmp.SourceKey, gdi_bmp.SourceKey);
					m_gBack.DrawImage(gdi_bmp.Image, dest, srcRegion.X + srcRegion.Width,
						srcRegion.Y, -srcRegion.Width, srcRegion.Height, GraphicsUnit.Pixel, attr);
				}
				else
				{
					Rectangle dest = new Rectangle(x, y, srcRegion.Width, srcRegion.Height);
					ImageAttributes attr = new ImageAttributes();
					attr.SetColorKey(gdi_bmp.SourceKey, gdi_bmp.SourceKey);
					m_gBack.DrawImage(gdi_bmp.Image, dest, srcRegion.X, srcRegion.Y,
						srcRegion.Width, srcRegion.Height, GraphicsUnit.Pixel, attr);
				}
			}
			else
			{
				if ((m_drawFlags & DrawFlags.GDBLT_MIRRORLEFTRIGHT) != 0)
				{
					Rectangle dest = new Rectangle(x + srcRegion.Width - 1, y, 1, srcRegion.Height);
					Rectangle src = new Rectangle(srcRegion.X, srcRegion.Y, 1, srcRegion.Height);
					for (int i = 0; i < srcRegion.Width; i++)
					{
						m_gBack.DrawImage(gdi_bmp.Image, dest.X, dest.Y, src,
							GraphicsUnit.Pixel);
						dest.X--;
						src.X++;
					}
				}
				else
				{
#if !DESKTOP
					m_gBack.DrawImage(gdi_bmp.Image, x, y, srcRegion, GraphicsUnit.Pixel);
#else
					m_gBack.DrawImage(gdi_bmp.Image, x, y, srcRegion.Width, srcRegion.Height);
#endif
				}
			}
		}

		/// <summary>
		/// Draw a filled rectangle to the back buffer.
		/// </summary>
		/// <param name="r">Rectangle to be filled</param>
		/// <param name="c">Color of rectangle</param>
		public void DrawFilledRect(Rectangle r, Color c)
		{
			m_gBack.FillRectangle(new SolidBrush(c), r);
		}

		/// <summary>
		/// Flip the back buffer to the display.
		/// </summary>
		public void Flip()
		{
			m_screen.DrawImage(m_back, 0, 0);
		}

		/// <summary>
		/// Draw the string to the back buffer.
		/// </summary>
		/// <param name="x">X destination of text</param>
		/// <param name="y">Y destination of text</param>
		/// <param name="text">Text to be displayed</param>
		/// <param name="color">Color of text</param>
		/// <param name="font">Font to be used</param>
		/// <param name="flags">Font draw flags</param>
		public void DrawText(int x, int y, string text, Color color, IGXFont f, FontDrawFlags flags)
		{
			GDIGXFont font = (GDIGXFont)f;

			float drawX = (float)x;
			float drawY = (float)y;

			if ((flags & FontDrawFlags.GDDRAWTEXT_CENTER) != 0)
			{
				SizeF sSize = m_gBack.MeasureString(text, font.MyFont);
				drawX -= sSize.Width / 2.0F;
			}
			else if ((flags & FontDrawFlags.GDDRAWTEXT_RIGHT) != 0)
			{
				SizeF sSize = m_gBack.MeasureString(text, font.MyFont);
				drawX -= sSize.Width;
			}

			Brush brush = new SolidBrush(color);
			m_gBack.DrawString(text, font.MyFont, brush, drawX, drawY);

		}
	
		/// <summary>
		/// Draw flags used for drawing bitmaps.  These flags
		/// map to GapiDraw flags so they can be used directly.
		/// </summary>
		protected DrawFlags m_drawFlags = 0;
		/// <summary>
		/// Set the current draw mode.
		/// </summary>
		/// <param name="flags">Flags to be set</param>
		public void SetDrawFlags(DrawFlags flags)
		{
			m_drawFlags |= flags;
		}

		/// <summary>
		/// Clear the current draw mode of the specified flags.
		/// </summary>
		/// <param name="flags">Flags to be cleared</param>
		public void ClearDrawFlags(DrawFlags flags)
		{
			m_drawFlags &= ~flags;
		}

		/// <summary>
		/// Validate draw regions be clipping them to the screen.
		/// </summary>
		/// <param name="x">X destination</param>
		/// <param name="y">Y destination</param>
		/// <param name="rSrc">Source region</param>
		/// <returns>true if any part of the destination is drawable (on-screen),
		/// false otherwise</returns>
		public bool ValidateRegions(ref int x, ref int y, ref Rectangle rSrc)
		{
			if (x < 0)
			{
				rSrc.Width += x;
				if ((m_drawFlags & DrawFlags.GDBLT_MIRRORLEFTRIGHT) == 0)
				{
					rSrc.X -= x;
				}

				x = 0;
			}
			else if (x >= ScreenWidth)
			{
				return false;
			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -