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

📄 graphics.cs

📁 功能:基于windows mobile 的地图查看器。使用vs2005开发
💻 CS
📖 第 1 页 / 共 3 页
字号:
//---------------------------------------------------------------------
//  This file is part of the Microsoft .NET Framework SDK Code Samples.
// 
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// 
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation.  See these other
//materials for detailed information regarding Microsoft code samples.
// 
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;

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

namespace GraphicsLibrary
{
    /// <summary>
    /// Draw options used for drawing bitmaps.
    /// </summary>
    [FlagsAttribute]
    public enum DrawOptions
    {
        None = 0,
        BlitKeyedTransparency = 0x0001,
        BlitMirrorLeftRight = 0x0010,
        BlitMirrorUpDown = 0x0020
    }

    public interface IGraphics : 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="animation">Animation to be drawn</param>
        void DrawAnimation(int x, int y, Animation animation);

        /// <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="sourceRegion">Source region of the draw</param>
        /// <param name="bmp">Bitmap to be drawn</param>
        void DrawBitmap(int x, int y, Rectangle sourceRegion, IBitmap 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="options">Font draw options</param>
        void DrawText(int x, int y, string text, Color color, IFont font,
            FontDrawOptions options);

        /// <summary>
        /// Set the current draw mode.
        /// </summary>
        /// <param name="options">options to be set</param>
        void SetDrawOptions(DrawOptions options);

        /// <summary>
        /// Clear the current draw mode of the specified options.
        /// </summary>
        /// <param name="options">options to be cleared</param>
        void ClearDrawOptions(DrawOptions options);

        /// <summary>
        /// Creates a bitmap compatible with this graphics device
        /// </summary>
        /// <param name="fileName">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>
        IBitmap CreateBitmap(string fileName, bool transparent);

        /// <summary>
        /// Creates a font object compatible with this graphics device
        /// </summary>
        /// <param name="fontName"></param>
        /// <returns>A font object compatible with this graphics device
        /// </returns>
        IFont CreateFont(string fontName);
    }

    /// <summary>
    /// A GDI implementation of the IGraphics
    /// </summary>
    public class GdiGraphics : IGraphics
    {

        /// <summary>
        /// Gets the width of the screen.
        /// </summary>
        public int ScreenWidth
        {
            get { return back.Width; }
        }

        /// <summary>
        /// Gets the height of the screen.
        /// </summary>
        public int ScreenHeight
        {
            get { return back.Height; }
        }

        /// <summary>
        /// Represents the back buffer.
        /// </summary>
        Bitmap back = null;

        /// <summary>
        /// Graphics object associated with the back buffer.
        /// </summary>
        Graphics gBack = null;

        /// <summary>
        /// Graphics object associated with the screen / owner control.
        /// </summary>
        Graphics screen = null;

        /// <summary>
        /// Initialize the graphics engine.
        /// </summary>
        /// <param name="owner">Owner control (Form)</param>
        public GdiGraphics(Control owner)
        {
            back = new Bitmap(240, 320);
            gBack = Graphics.FromImage(back);
            screen = owner.CreateGraphics();
        }

        /// <summary>
        /// Empty.  Provided for compatibility.
        /// </summary>
        public void Dispose()
        {
            screen.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="animation">Animation to be drawn</param>
        public void DrawAnimation(int x, int y, Animation animation)
        {
            DrawBitmap(x, y, animation.Region, animation.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="sourceRegion">Source region of the draw</param>
        /// <param name="bmp">Bitmap to be drawn</param>
        public void DrawBitmap(int x, int y, Rectangle sourceRegion,
            IBitmap bmp)
        {
            // make sure the function is passed an appropriate implementation
            GdiBitmap gdi_bmp = null;
            try
            {
                gdi_bmp = (GdiBitmap)bmp;
            }
            catch (InvalidCastException e)
            {
                throw new ApplicationException(
                    "The bitmap given was not created by" +
                    "this class' CreateBitmap() method.", e);
            }

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

            // Draw the bitmap
            if (gdi_bmp.Transparent)
            {
                if ((drawOptions & DrawOptions.BlitMirrorLeftRight) != 0)
                {
                    Rectangle dest = new Rectangle(x, y, sourceRegion.Width,
                        sourceRegion.Height);
                    ImageAttributes attr = new ImageAttributes();
                    attr.SetColorKey(gdi_bmp.SourceKey, gdi_bmp.SourceKey);
                    gBack.DrawImage(gdi_bmp.Image, dest, sourceRegion.X +
                        sourceRegion.Width,
                        sourceRegion.Y, -sourceRegion.Width, sourceRegion.Height,
                        GraphicsUnit.Pixel, attr);
                }
                else
                {
                    Rectangle dest = new Rectangle(x, y, sourceRegion.Width,
                        sourceRegion.Height);
                    ImageAttributes attr = new ImageAttributes();
                    attr.SetColorKey(gdi_bmp.SourceKey, gdi_bmp.SourceKey);
                    gBack.DrawImage(gdi_bmp.Image, dest, sourceRegion.X,
                        sourceRegion.Y,
                        sourceRegion.Width, sourceRegion.Height,
                        GraphicsUnit.Pixel, attr);
                }
            }
            else
            {
                if ((drawOptions & DrawOptions.BlitMirrorLeftRight) != 0)
                {
                    Rectangle dest = new Rectangle(x + sourceRegion.Width - 1,
                        y, 1, sourceRegion.Height);
                    Rectangle src = new Rectangle(sourceRegion.X, sourceRegion.Y,
                        1, sourceRegion.Height);
                    for (int i = 0; i < sourceRegion.Width; i++)
                    {
                        gBack.DrawImage(gdi_bmp.Image, dest.X, dest.Y, src,
                            GraphicsUnit.Pixel);
                        dest.X--;
                        src.X++;
                    }
                }
                else
                {
#if !DESKTOP
                    gBack.DrawImage(gdi_bmp.Image, x, y, sourceRegion,
                        GraphicsUnit.Pixel);
#else 
                    gBack.DrawImage(gdi_bmp.Image, x, y,
                        sourceRegion.Width, sourceRegion.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)
        {
            gBack.FillRectangle(new SolidBrush(c), r);
        }

        /// <summary>
        /// Flip the back buffer to the display.
        /// </summary>
        public void Flip()
        {
            screen.DrawImage(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="options">Font draw options</param>
        public void DrawText(int x, int y, string text,
            Color color, IFont font, FontDrawOptions options)
        {
            // make sure the function is passed an appropriate implementation

⌨️ 快捷键说明

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