如何用winform痛苦的双手。 (C#) [英] how to pain a hands with winform. (C#)

查看:84
本文介绍了如何用winform痛苦的双手。 (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参考下面的截图,我想用winform痛苦,当我点击一根手指时,它会突出显示。 

please refer to the following screenshot, I want to pain it with winform and when I click one finger, it will be highlighted. 

怎么办?这个。

推荐答案

我会使用一些GraphicsPath来保存每个Finger的数据,并使用它的IsVisible方法来命名HitTest鼠标位置。

I'd use some GraphicsPaths to hold the data for each Finger, and use its IsVisible Method to HitTest the mouseLocation.

[我只是粗略地实现了一个手指路径(使用硬编码的"魔术"数字),你需要为所有人做手部分 - 提示:你可以克隆一个GraphicsPath并通过变换拉伸/缩放它]

[I just implemented one finger-Path in a rough way (using hardcoded "magic" numbers), you would need to do for all parts of the hand - hint: You can clone a GraphicsPath and stretch/scale it by a transform]

    public partial class Form1 : Form
    {
        private Hand _left = null;
        public Form1()
        {
            InitializeComponent();
            this.ClientSize = new System.Drawing.Size(687, 515);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _left = new Hand(LRHand.Left);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (_left != null)
                _left.Render(e.Graphics);
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);

            if (_left != null)
                _left.Dispose();
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            _left.HitTest(e.X, e.Y);

            this.Invalidate();
        }
    }

    public class Hand : IDisposable
    {
        public LRHand LeftRight { get; set; }
        private List<System.Drawing.Drawing2D.GraphicsPath> _drawings = new List<System.Drawing.Drawing2D.GraphicsPath>();
        public Color NormalColor { get; set; }
        public Color SelectedColor { get; set; }
        public float PenWidth { get; set; }
        private int _selectedNum = -1;

        public Hand(LRHand lr)
        {
            this.LeftRight = lr;
            this.NormalColor = Color.Blue;
            this.SelectedColor = Color.Red;
            this.PenWidth = 2;
            SetupPaths();
        }

        private void SetupPaths()
        {
            if (_drawings == null)
                _drawings = new List<System.Drawing.Drawing2D.GraphicsPath>();

            //Just one finger to show (or use a class Finger and setup the Path there)
            System.Drawing.Drawing2D.GraphicsPath t = new System.Drawing.Drawing2D.GraphicsPath();
            t.AddLine(new Point(20, 100), new Point(20, 200));
            t.AddArc(new Rectangle(20, 200, 50, 50), 180, -180);
            t.AddLine(new Point(70, 200), new Point(70, 100));
            t.AddArc(new Rectangle(20, 75, 50, 50), 0, -180);

            RectangleF rc = t.GetBounds();
            double rad = 30.0 / 180.0 * Math.PI;

            using (System.Drawing.Drawing2D.Matrix mx = new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, -rc.X - rc.Width / 2f, -rc.Y - rc.Height / 2f))
            {
                t.Transform(mx);
            }
            using (System.Drawing.Drawing2D.Matrix mx = new System.Drawing.Drawing2D.Matrix(
                (float)(Math.Cos(rad)),
                (float)(-Math.Sin(rad)),
                (float)(Math.Sin(rad)),
                (float)(Math.Cos(rad)), 0, 0))
            {
                t.Transform(mx);
            }
            rc = t.GetBounds();
            using (System.Drawing.Drawing2D.Matrix mx = new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, -rc.X + rc.Width / 2f, -rc.Y + rc.Height / 2f))
            {
                t.Transform(mx);
            }

            this._drawings.Add(t);
        }

        public void Render(Graphics g)
        {
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            if (this._drawings != null)
                for (int i = 0; i < this._drawings.Count; i++)
                {
                    if (i == _selectedNum)
                    {
                        using (Pen pen = new Pen(this.SelectedColor, this.PenWidth))
                            g.DrawPath(pen, this._drawings[i]);
                    }
                    else
                    {
                        using (Pen pen = new Pen(this.NormalColor, this.PenWidth))
                            g.DrawPath(pen, this._drawings[i]);
                    }
                }
        }

        public bool HitTest(int x, int y)
        {
            bool b = false;
            this._selectedNum = -1;
            if (this._drawings != null)
                for (int i = 0; i < this._drawings.Count; i++)
                {
                    if (this._drawings[i].IsVisible(x, y))
                    {
                        this._selectedNum = i;
                        b = true;
                        break;
                    }
                }

            return b;
        }

        #region IDisposable Support
        private bool disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    if (this._drawings != null)
                        for (int i = this._drawings.Count - 1; i >= 0; i--)
                        {
                            this._drawings[i].Dispose();
                        }
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.

                disposedValue = true;
            }
        }

        // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
        // ~Hand() {
        //   // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
        //   Dispose(false);
        // }

        // This code added to correctly implement the disposable pattern.
        public void Dispose()
        {
            // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
            Dispose(true);
            // TODO: uncomment the following line if the finalizer is overridden above.
            // GC.SuppressFinalize(this);
        }
        #endregion

    }

    public enum LRHand
    {
        Left,
        Right
    }

问候,

  Thorsten

  Thorsten


这篇关于如何用winform痛苦的双手。 (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆