用C#一步一步构建一个完整的D3D场景(四) 控件

我与影子孤独终老i 提交于 2020-01-03 21:23:29

由于要从底层重新写自己的控件,花了一定的时间,目前已经完成基类的工作,并用基类派生了三个简单的控件(Button,Label和CheckBox),均已调试完毕。

下面直接给出完整的D3D_Control类。

    public abstract class D3D_Control
    {
        private string m_ControlName;

        private D3D_Scene m_Scene;
        private D3D_Control m_Parent;
        private bool m_Loaded;
        private bool m_Show;

        private Point m_Location;
        private Size m_Size;
        private bool m_TopMost;

        private bool m_Visible;
        private bool m_Enable;

        private bool m_CanFocus;
        private bool m_Focus;
        private bool m_MouseEntered;

        private Sprite m_Sprite;

        private Color m_TextureBackColor;
        private float m_TextureTransparency;                
        private Texture m_Texture;
        private int m_CurState;
        private List<Rectangle> m_RectanglesInTexture;

        private Color m_BGIColor; 
        private float m_BGITransparency;        
        private bool m_BGIAutoScaleEnable;
        private bool m_BGIAutoScrollEnable;
        private Rectangle m_RectangleInBGI;
        private Size m_BGISize;
        private Texture m_BGITexture;
        private int m_BGIBorderWidth;
        private int m_BGIScrollDefaultStep;
        private int m_BGIScrollXStep;
        private int m_BGIScrollYStep;
        
        private string m_Text;
        private Color m_TextColor;
        private System.Drawing.Font m_TextFont;

        public string Name { get { return m_ControlName; } set { m_ControlName = value; } }

        public D3D_Scene Scene
        {
            get { return m_Scene; }
            private set
            {
                m_Scene = value;
            }
        }
        public D3DDevice Device 
        {
            get
            {
                if(Scene!=null) return Scene.Device;
                return null;
            }
        }
        public D3D_Control Parent { get { return m_Parent; } set { m_Parent = value; } }
        public bool Loaded
        {
            get { return m_Loaded; }
            set
            {
                if (Loaded == value) return;
                m_Loaded = value;
                if (Loaded && Load != null) Load(this, null);
            }
        }
        public bool Show
        {
            get { return m_Show; }
            set
            {
                if (Show == value) return;
                m_Show = value;
                if (Show && !Loaded) Loaded = true;
            }
        }

        public Point Location
        {
            get
            {
                return m_Location;
            }
            set
            {
                m_Location = value;
                if (LocationChanged != null) LocationChanged(this, null);
            }
        }
        public Size Size
        {
            get
            {
                return m_Size;
            }
            set
            {
                m_Size = value;
                if (SizeChanged != null) SizeChanged(this, null);
            }
        }
        public int Left { get { return Location.X; } }
        public int Top { get { return Location.Y; } }
        public int Right { get { return Location.X + Size.Width - 1; } }
        public int Bottom { get { return Location.Y + Size.Height - 1; } }
        public int Width { get { return Size.Width; } }
        public int Height { get { return Size.Height; } }

        public virtual Rectangle DisplayRectangle
        {
            get
            {
                Rectangle rect = new Rectangle(this.Location, this.Size);
                if (Parent != null)
                {
                    rect.Offset(Parent.ClientRectangle.Location);
                }
                return rect;
            }
        }
        public virtual Rectangle ClientRectangle { get { return new Rectangle(Location, Size); } }
        public Point MousePosition
        {
            get
            {
                if (Scene != null && Scene.Form != null) return this.Scene.Form.PointToClient(Cursor.Position);
                return Cursor.Position;
            }
        }
        
        public bool TopMost { get { return m_TopMost; } set { m_TopMost = value; } }
        public float ZPos { get { return TopMost ? 0.0f : 1.0f; } }

        public virtual bool Visibled { get { return m_Visible; } set { m_Visible = value; } }
        public virtual bool Enabled { get { return m_Enable; } set { m_Enable = value; } }

        public bool CanFocus { get { return m_CanFocus; } set { m_CanFocus = value; } }
        public bool Focused
        {
            get
            {
                return m_Focus;
            }
            set
            {
                m_Focus = value;

                if (Focused)
                {
                    if (GotFocus != null) GotFocus(this, null);
                }
                else
                {
                    if (LostFocus != null) LostFocus(this, null);
                }
            }
        }
        public bool MouseEntered
        {
            get
            {
                return m_MouseEntered;
            }
            set
            {
                m_MouseEntered = value;
                if (MouseEntered && MouseEnter != null) MouseEnter(this, null);
                if (!MouseEntered && MouseLeave != null) MouseLeave(this, null);
            }
        }

        protected Sprite Sprite { get { return m_Sprite; } private set { m_Sprite = value; } }

        public Color TextureBackColor { get { return m_TextureBackColor; } set { m_TextureBackColor = value; } }
        public float TextureTransparency 
        {
            get { return m_TextureTransparency; }
            set 
            {
                m_TextureTransparency = value;
                if (m_TextureTransparency < 0.0f) m_TextureTransparency = 0.0f;
                if (m_TextureTransparency > 100.0f) m_TextureTransparency = 100.0f;
                TextureBackColor = Color.FromArgb((byte)(2.55f * (100.0f - TextureTransparency)), TextureBackColor);
            }
        }
        public Texture Texture { get { return m_Texture; } set { m_Texture = value; } }
        public List<Rectangle> RectanglesInTexture { get { return m_RectanglesInTexture; } private set { m_RectanglesInTexture = value; } }
        public int CurState { get { return m_CurState; } set { m_CurState = value; } }
        public virtual Rectangle SourceRectangle
        {
            get
            {
                if (RectanglesInTexture != null && RectanglesInTexture.Count != 0 && CurState >= 0 && CurState < RectanglesInTexture.Count)
                {
                    return RectanglesInTexture[CurState];
                }

                return new Rectangle();
            }
        }

        public Color BGIColor { get { return m_BGIColor; } set { m_BGIColor = value; } }
        public float BGITransparency
        {
            get { return m_BGITransparency; }
            set
            {
                m_BGITransparency = value;
                if (m_BGITransparency < 0.0f) m_BGITransparency = 0.0f;
                if (m_BGITransparency > 100.0f) m_BGITransparency = 100.0f;
                BGIColor = Color.FromArgb((byte)(2.55f * (100.0f - BGITransparency)), BGIColor);
            }
        }
        public bool BGIAutoScaleEnable
        {
            get { return m_BGIAutoScaleEnable; }
            set
            {
                m_BGIAutoScaleEnable = value;
                if (BGIAutoScaleEnable)
                {
                    BGIAutoScrollEnable = false;
                    RectangleInBGI = new Rectangle(new Point(), BGISize);
                }
            }
        }
        public bool BGIAutoScrollEnable
        {
            get { return m_BGIAutoScrollEnable; }
            set
            {
                m_BGIAutoScrollEnable = value;
                if (BGIAutoScrollEnable)
                {
                    BGIAutoScaleEnable = false;
                    RectangleInBGI = new Rectangle(new Point(), ClientRectangle.Size);
                }
            }
        }
        public Rectangle RectangleInBGI
        {
            get 
            {
                return m_RectangleInBGI; 
            }
            set { m_RectangleInBGI = value; }
        }
        public Size BGISize { get { return m_BGISize; } set { m_BGISize = value; } }
        public Texture BGITexture { get { return m_BGITexture; } set { m_BGITexture = value; } }
        public int BGIBorderWidth { get { return m_BGIBorderWidth; } set { m_BGIBorderWidth = value; } }
        public int BGIScrollDefaultStep { get { return m_BGIScrollDefaultStep; } set { m_BGIScrollDefaultStep = value; } }
        public Rectangle BGILeftBorder
        {
            get
            {
                Rectangle rect=new Rectangle();
                rect.X = ClientRectangle.Left;
                rect.Y = ClientRectangle.Top + BGIBorderWidth;
                rect.Width = BGIBorderWidth;
                rect.Height = ClientRectangle.Height - 2 * BGIBorderWidth;
                return rect;
            }
        }
        public Rectangle BGITopBorder
        {
            get
            {
                Rectangle rect = new Rectangle();
                rect.X = ClientRectangle.Left + BGIBorderWidth;
                rect.Y = ClientRectangle.Top;
                rect.Width = ClientRectangle.Width - 2 * BGIBorderWidth;
                rect.Height = BGIBorderWidth;
                return rect;
            }
        }
        public Rectangle BGIRightBorder
        {
            get
            {
                Rectangle rect = new Rectangle();
                rect.X = ClientRectangle.Right - BGIBorderWidth + 1;
                rect.Y = ClientRectangle.Top + BGIBorderWidth;
                rect.Width = BGIBorderWidth;
                rect.Height = ClientRectangle.Height - 2 * BGIBorderWidth;
                return rect;
            }
        }
        public Rectangle BGIBottomBorder
        {
            get
            {
                Rectangle rect = new Rectangle();
                rect.X = ClientRectangle.Left + BGIBorderWidth;
                rect.Y = ClientRectangle.Bottom - BGIBorderWidth + 1;
                rect.Width = ClientRectangle.Width - 2 * BGIBorderWidth;
                rect.Height = BGIBorderWidth;
                return rect;
            }
        }

        public float Transparency
        {
            set
            {
                TextureTransparency = value;
                BGITransparency = value;
            }
        }

        public string Text
        {
            get { return m_Text; }
            set
            {
                m_Text = value;
                if (TextChanged != null) TextChanged(this, null);
            }
        }
        public Color TextColor
        {
            get { return m_TextColor; }
            set { m_TextColor = value; }
        }
        public System.Drawing.Font TextFont { get { return m_TextFont; } set { m_TextFont = value; } }

        public event EventHandler Load;
        public event EventHandler LocationChanged;
        public event EventHandler SizeChanged;
        public event EventHandler GotFocus;
        public event EventHandler LostFocus;
        public event MouseEventHandler MouseClick;
        public event MouseEventHandler MouseDoubleClick;
        public event MouseEventHandler MouseDown;
        public event MouseEventHandler MouseUp;
        public event MouseEventHandler MouseMove;
        public event MouseEventHandler MouseWheel;
        public event MouseEventHandler MouseEnter;
        public event MouseEventHandler MouseLeave;
        public event EventHandler MouseHover;
        public event KeyEventHandler KeyDown;
        public event KeyEventHandler KeyUp;
        public event KeyPressEventHandler KeyPress;
        public event EventHandler Paint;
        public event EventHandler TextChanged;

        public virtual void LoadBGITextureFromFile(string filename)
        {
            BGIColor = Color.White;
            BGITransparency = 0.0f;
            BGIAutoScaleEnable = true;
            BGIAutoScrollEnable = false;
            RectangleInBGI = new Rectangle();
            BGISize = new Size();
            if (BGITexture != null) BGITexture.Dispose();
            BGITexture = null;
            BGIBorderWidth = 5;
            BGIScrollDefaultStep = 1;
            m_BGIScrollXStep = 0;
            m_BGIScrollYStep = 0;
        }
        public virtual void LoadTextureFromFile(string filename) 
        {
            TextureBackColor = Color.White;
            TextureTransparency = 0.0f;
            CurState = 0;
            if (Texture != null) Texture.Dispose();
            Texture = null;
            if (Sprite != null) Sprite.Dispose();
            Sprite = null;
            Sprite = new Sprite(this.Scene.Device);

            if (RectanglesInTexture == null) RectanglesInTexture = new List<Rectangle>();
            RectanglesInTexture.Clear();
        }
        
        public virtual bool PointOnControl(Point point)
        {
            return DisplayRectangle.Contains(point);
        }
      
        public virtual void OnFormMouseClick(object sender, MouseEventArgs e) 
        {
            if (!Visibled || !Enabled || !Show) return;
            if (PointOnControl(e.Location) && MouseClick != null) MouseClick(this, e);
            if (!PointOnControl(e.Location) && CanFocus && Focused) Focused = false;
            if (PointOnControl(e.Location) && CanFocus && !Focused) Focused = true;
        }
        public virtual void OnFormMouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (!Visibled || !Enabled || !Show) return;
            if (PointOnControl(e.Location) && MouseDoubleClick != null) MouseDoubleClick(this, e);
            if (!PointOnControl(e.Location) && CanFocus && Focused) Focused = false;
            if (PointOnControl(e.Location) && CanFocus && !Focused) Focused = true;
        }
        public virtual void OnFormMouseDown(object sender, MouseEventArgs e)
        {
            if (!Visibled || !Enabled || !Show) return;
            if (PointOnControl(e.Location) && MouseDown != null) MouseDown(this, e);
            if (!PointOnControl(e.Location) && CanFocus && Focused) Focused = false;
            if (PointOnControl(e.Location) && CanFocus && !Focused) Focused = true;
        }
        public virtual void OnFormMouseUp(object sender, MouseEventArgs e)
        {
            if (!Visibled || !Enabled || !Show) return;
            if (PointOnControl(e.Location) && MouseUp != null) MouseUp(this, e);
            if (!PointOnControl(e.Location) && CanFocus && Focused) Focused = false;
            if (PointOnControl(e.Location) && CanFocus && !Focused) Focused = true;
        }
        public virtual void OnFormMouseMove(object sender, MouseEventArgs e)
        {
            if (!Visibled || !Enabled || !Show) return;
            if (PointOnControl(e.Location) && e.Button != MouseButtons.None && MouseDown != null) MouseDown(this, e);
            if (PointOnControl(e.Location) && !MouseEntered) MouseEntered = true;
            if (!PointOnControl(e.Location) && MouseEntered) MouseEntered = false;
            if (PointOnControl(e.Location) && MouseMove != null) MouseMove(this, e);
        }
        public virtual void OnFormMouseWheel(object sender, MouseEventArgs e)
        {
            if (!Visibled || !Enabled || !Show) return;
            if (CanFocus && Focused && MouseWheel != null) MouseWheel(this, e);
        }
        public virtual void OnFormMouseHover(object sender, EventArgs e)
        {
            if (!Visibled || !Enabled || !Show) return;
            if (PointOnControl(MousePosition) && MouseHover != null) MouseHover(this, e);
        }
        public virtual void OnFormKeyDown(object sender, KeyEventArgs e)
        {
            if (!Visibled || !Enabled || !Show) return;
            if (Focused && KeyDown != null) KeyDown(this, e);
        }
        public virtual void OnFormKeyUp(object sender, KeyEventArgs e)
        {
            if (!Visibled || !Enabled || !Show) return;
            if (Focused && KeyUp != null) KeyUp(this, e);
        }
        public virtual void OnFormKeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Visibled || !Enabled || !Show) return;
            if (Focused && KeyPress != null) KeyPress(this, e);
        }    
        public virtual void OnSceneRendering(object sender, EventArgs e)
        {
            if (!Visibled || !Show) return;
            if (Paint != null) Paint(this, null);
        }
        public virtual void OnPaintBGI(object sender, EventArgs e)
        {
            if (Sprite == null || BGITexture == null || !Show) return;            
                       
            Vector2 scalingCenter = new Vector2(ClientRectangle.Left, ClientRectangle.Top);
            Vector2 scaling = new Vector2(1.0f * ClientRectangle.Width / m_RectangleInBGI.Width, 1.0f * ClientRectangle.Height / m_RectangleInBGI.Height);
            Matrix transform = Matrix.Transformation2D(scalingCenter, 0.0f, scaling, new Vector2(), 0.0f, new Vector2());
            
            Sprite.Begin(SpriteFlags.AlphaBlend);
            Sprite.Transform = transform;
            Sprite.Draw(BGITexture, m_RectangleInBGI, new Vector3(0, 0, 0), new Vector3(ClientRectangle.X, ClientRectangle.Y, ZPos), BGIColor);
            Sprite.End();
        }
        public virtual void OnPaintControl(object sender, EventArgs e)
        {
            if (Texture == null || Sprite == null || !Show) return;
            Sprite.Begin(SpriteFlags.AlphaBlend);
            Vector2 scalingCenter = new Vector2(DisplayRectangle.Left, DisplayRectangle.Top);
            Vector2 scaling = new Vector2(1.0f * Width / SourceRectangle.Width, 1.0f * Height / SourceRectangle.Height);
            Matrix transform = Matrix.Transformation2D(scalingCenter, 0.0f, scaling, new Vector2(0.0f, 0.0f), 0.0f, new Vector2(0.0f, 0.0f));
            Sprite.Transform = transform;
            Sprite.Draw(Texture, SourceRectangle, new Vector3(0, 0, 0), new Vector3(this.DisplayRectangle.X, this.DisplayRectangle.Y, ZPos), TextureBackColor);
            Sprite.End();
        }
        public virtual void OnPrepareNextFrame(object sender, EventArgs e)
        {
            if (BGIAutoScrollEnable)
            {
                m_RectangleInBGI.Offset(m_BGIScrollXStep, m_BGIScrollYStep);
                if (m_RectangleInBGI.Left < 0) m_RectangleInBGI.Offset(-m_RectangleInBGI.Left, 0);
                if (m_RectangleInBGI.Top < 0) m_RectangleInBGI.Offset(0, -m_RectangleInBGI.Top);
                if (m_RectangleInBGI.Right > BGISize.Width) m_RectangleInBGI.Offset(BGISize.Width - m_RectangleInBGI.Right, 0);
                if (m_RectangleInBGI.Bottom > BGISize.Height) m_RectangleInBGI.Offset(0, BGISize.Height - m_RectangleInBGI.Bottom);
            }
        }
        
        public virtual void OnBaseMouseMove(object sender, MouseEventArgs e)
        {
            m_BGIScrollXStep = 0;
            m_BGIScrollYStep = 0;

            if (!BGIAutoScrollEnable) return;

            Point mousePos = e.Location;
            if (BGILeftBorder.Contains(mousePos))
            {
                m_BGIScrollXStep = -BGIScrollDefaultStep;
                m_BGIScrollYStep = 0;
            }
            if (BGITopBorder.Contains(mousePos))
            {
                m_BGIScrollYStep = -BGIScrollDefaultStep;
                m_BGIScrollXStep = 0;
            }
            if (BGIRightBorder.Contains(mousePos))
            {
                m_BGIScrollXStep = BGIScrollDefaultStep;
                m_BGIScrollYStep = 0;
            }
            if (BGIBottomBorder.Contains(mousePos))
            {
                m_BGIScrollXStep = 0;
                m_BGIScrollYStep = BGIScrollDefaultStep;
            }
        }

        public D3D_Control(D3D_Scene scene = null)
        {
            m_ControlName = "";

            m_Scene = scene;
            m_Parent = null;
            m_Loaded = false;
            m_Show = false;

            m_Location = new Point();
            m_Size = new Size(64, 32);
            m_TopMost = true;

            m_Visible = true;
            m_Enable = true;

            m_CanFocus = false;
            m_Focus = false;
            m_MouseEntered = false;

            m_Text = "";
            m_TextColor = Color.White;
            m_TextFont = new System.Drawing.Font("宋体", 12);

            if (this.Scene != null && this.Scene.Form != null)
            {
                this.Scene.Form.MouseClick += this.OnFormMouseClick;
                this.Scene.Form.MouseDoubleClick += this.OnFormMouseDoubleClick;
                this.Scene.Form.MouseDown += this.OnFormMouseDown;
                this.Scene.Form.MouseUp += this.OnFormMouseUp;
                this.Scene.Form.MouseMove += this.OnFormMouseMove;
                this.Scene.Form.MouseWheel += this.OnFormMouseWheel;
                this.Scene.Form.MouseHover += this.OnFormMouseHover;
                this.Scene.Form.KeyDown += this.OnFormKeyDown;
                this.Scene.Form.KeyUp += this.OnFormKeyUp;
                this.Scene.Form.KeyPress += this.OnFormKeyPress;

                this.Scene.PrepareNextFrame += this.OnPrepareNextFrame;
                this.Scene.Rendering += this.OnSceneRendering;
            }

            this.Paint += this.OnPaintControl;
            this.Paint += this.OnPaintBGI;

            this.MouseMove += this.OnBaseMouseMove;
        }
    }

基类定义完毕后,Button、Label和CheckBox的实现将会很简单,直接放代码:

    public class D3D_Button : D3D_Control
    {
        public enum ButtonStates { MouseLeave = 0, MouseEnter = 1, MouseDown = 2, ButtonDisable = 3 };
        private ButtonStates m_CurButtonState;

        public ButtonStates CurButtonState { get { return m_CurButtonState; } set { m_CurButtonState = value; CurState = (int)m_CurButtonState; } }
        
        public event EventHandler ButtonClick;

        public D3D_Button(D3D_Scene scene = null)
            : base(scene)
        {
            TextFont = new System.Drawing.Font("楷体", 12);
            TextColor = Color.Yellow;

            LoadTextureFromFile("button1.bmp");

            Enabled = true;

            this.Paint += this.OnPaintButton;

            this.MouseDown += this.OnMouseDown;
            this.MouseMove += this.OnMouseMove;
            this.MouseClick += this.OnMouseClick;
            this.MouseUp += this.OnMouseUp;
            this.MouseEnter += this.OnMouseEnter;
            this.MouseLeave += this.OnMouseLeave;
        }

        public void OnPaintButton(object sender, EventArgs e)
        {
            D3D.Font font = new D3D.Font(Device, TextFont);
            font.DrawText(null, Text, DisplayRectangle, DrawTextFormat.Center | DrawTextFormat.VerticalCenter, TextColor);
            font.Dispose();
        }

        public override void LoadTextureFromFile(string filename)
        {
            if (Device == null) return;
            base.LoadTextureFromFile(filename);

            Bitmap bmp = BitmapWithColorKey.GetBitmapFromFile(filename);
            Texture = new Texture(Device, bmp, Usage.None, Pool.Managed);
            Rectangle rect;
            for (int index = 0; index < 4; index++)
            {
                rect = new Rectangle(2 + 64 * index, 5, 58, 28);
                RectanglesInTexture.Add(rect);
            }
            bmp.Dispose();
        }

        public void OnMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) CurButtonState = ButtonStates.MouseDown;
        }

        public void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (!Visibled) return;
            if (!Enabled)
            {
                CurButtonState = ButtonStates.ButtonDisable;
                return;
            }
            if (DisplayRectangle.Contains(e.Location))
            {
                if (e.Button == MouseButtons.Left)
                    CurButtonState = ButtonStates.MouseDown;
                else
                    CurButtonState = ButtonStates.MouseEnter;
            }
            else
            {
                CurButtonState = ButtonStates.MouseLeave;
            }
        }

        public void OnMouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (ButtonClick != null) ButtonClick(this, null);
            }
        }

        public void OnMouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) CurButtonState = ButtonStates.MouseEnter;
        }

        public void OnMouseEnter(object sender, MouseEventArgs e)
        {
            CurButtonState = ButtonStates.MouseEnter;
        }

        public void OnMouseLeave(object sender, MouseEventArgs e)
        {
            CurButtonState = ButtonStates.MouseLeave;
        }
    }

    public class D3D_Label : D3D_Control
    {
        private bool m_HighLightEnable;
        public bool HighLightEnabled { get { return m_HighLightEnable; } set { m_HighLightEnable = value; } }
        public D3D_Label(D3D_Scene scene)
            : base(scene)
        {
            HighLightEnabled = true;
            BGIColor = Color.FromArgb(0, Color.White);
            TextureBackColor = Color.FromArgb(0, Color.White);

            LoadTextureFromFile(null);
            LoadBGITextureFromFile("无缝草地.jpg");

            this.Paint += this.OnPaintLabel;
            this.MouseEnter += this.OnMouseEnter;
            this.MouseLeave += this.OnMouseLeave;
        }

        public void OnPaintLabel(object sender, EventArgs e)
        {            
            D3D.Font font = new D3D.Font(Device, TextFont);
            Rectangle textRectangle = DisplayRectangle;
            textRectangle.Offset(2, 2);
            textRectangle.Width -= 4;
            textRectangle.Height -= 4;
            font.DrawText(null, Text, textRectangle, DrawTextFormat.Left | DrawTextFormat.Top | DrawTextFormat.WordBreak, TextColor);
            font.Dispose();
        }

        public override void LoadTextureFromFile(string filename)
        {
            if (Device == null) return;
            base.LoadTextureFromFile(filename);

            Transparency = 100.0f;

            Bitmap bmp = new Bitmap(1, 1);
            bmp.SetPixel(0, 0, Color.White);
                
            Texture = Texture.FromBitmap(Device, bmp, Usage.None, Pool.Managed);
            bmp.Dispose();
            RectanglesInTexture.Add(new Rectangle(0, 0, 1, 1));
        }

        public override void LoadBGITextureFromFile(string filename)
        {
            if (Device == null) return;
            base.LoadBGITextureFromFile(filename);

            Bitmap bmp = new Bitmap(filename);
            BGITexture = Texture.FromBitmap(Device, bmp, Usage.None, Pool.Managed);
            BGISize = bmp.Size;
            bmp.Dispose();

            BGITransparency = 100.0f;
            RectangleInBGI = new Rectangle(new Point(), Size);
            BGIAutoScrollEnable = true;
        }

        private void OnMouseEnter(object sender, EventArgs e)
        {
            if (HighLightEnabled) Transparency = 50.0f;
        }

        private void OnMouseLeave(object sender, EventArgs e)
        {
            Transparency = 100.0f;
        }
    }

    public class D3D_CheckBox : D3D_Control
    {
        private bool m_Checked;
        
        public bool Checked { get { return m_Checked; } set { m_Checked = value; CurState = (Enabled ? 0 : 2) + (Checked ? 1 : 0); } }
        public override bool Enabled
        {
            get
            {
                return base.Enabled;
            }
            set
            {
                base.Enabled = value;
                CurState = (Enabled ? 0 : 2) + (Checked ? 1 : 0);
            }
        }
        
        public D3D_CheckBox(D3D_Scene scene)
            : base(scene)
        {
            m_Checked = false;
            
            LoadTextureFromFile("checkbox1.bmp");

            Checked = false;
            Enabled = true;

            this.MouseClick += this.OnMouseClick;
            this.Paint += this.OnPaintCheckbox;
        }

        public override void LoadTextureFromFile(string filename)
        {
            if (Device == null) return;
            base.LoadTextureFromFile(filename);

            Bitmap bmp = BitmapWithColorKey.GetBitmapFromFile(filename);
            Texture = new Texture(Device, bmp, Usage.None, Pool.Managed);
            Rectangle rect;
            for (int index = 0; index < 4; index++)
            {
                rect = new Rectangle(90 * index, 0, 90, 90);
                RectanglesInTexture.Add(rect);
            }
            bmp.Dispose();
        }

        public void OnMouseClick(object sender, MouseEventArgs e)
        {
            Checked = !Checked;
        }

        public void OnPaintCheckbox(object sender, EventArgs e)
        {
            if (!Visibled) return;

            Sprite.Begin(SpriteFlags.AlphaBlend);
            Vector2 scalingCenter = new Vector2(DisplayRectangle.Left, DisplayRectangle.Top);
            Vector2 scaling = new Vector2(1.0f * Height / 90.0f, 1.0f * Height / 90.0f);
            Matrix transform = Matrix.Transformation2D(scalingCenter, 0.0f, scaling, new Vector2(0.0f, 0.0f), 0.0f, new Vector2(0.0f, 0.0f));
            Sprite.Transform = transform;
            Sprite.Draw(Texture, SourceRectangle, new Vector3(0, 0, 0), new Vector3(this.DisplayRectangle.X, this.DisplayRectangle.Y, ZPos), Color.White);
            Sprite.End();

            D3D.Font font = new D3D.Font(Device, TextFont);
            Rectangle textRect = font.MeasureString(null,Text,DrawTextFormat.Left | DrawTextFormat.VerticalCenter,TextColor);
            textRect.Location = new Point(DisplayRectangle.Left + DisplayRectangle.Height, DisplayRectangle.Top);
            textRect.Size = new Size(textRect.Width, DisplayRectangle.Height);
            font.DrawText(null, Text, textRect, DrawTextFormat.Left | DrawTextFormat.VerticalCenter, TextColor);
            font.Dispose();
        }
    }

如果有朋友恰好看到这篇文章又感兴趣的话,可以私信给我,可以提供完整的代码,一起学习交流。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!