天天看點

C# 繪制自定義搖杆(WinForm),實作自由移動物體

作者:快樂鯉漁
C# 繪制自定義搖杆(WinForm),實作自由移動物體

聲明變量與初始化:

private Point joystickCenter;
        private Point joystickPosition;
        private bool isJoystickPressed;

        public event EventHandler JoystickMoved;
        public UserControl1()
        {
            InitializeComponent();

            joystickCenter = new Point(Width / 2, Height / 2);
            joystickPosition = joystickCenter;
            isJoystickPressed = false;
        }           

繪制搖杆圓形:

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            // 繪制搖杆圓形
            e.Graphics.FillEllipse(Brushes.Red, joystickCenter.X -50, joystickCenter.Y - 50, 100, 100);

            // 繪制箭頭
            if (isJoystickPressed)
            {
                e.Graphics.FillPolygon(Brushes.Black, GetArrowPoints());
            }
        }

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

            // 檢查是否在搖杆範圍内按下滑鼠
            if (Math.Pow(e.X - joystickCenter.X, 2) + Math.Pow(e.Y - joystickCenter.Y, 2) <= Math.Pow(50, 2))
            {
                // 更新搖杆位置
                joystickPosition = e.Location;
                isJoystickPressed = true;
                Invalidate();

                // 觸發搖杆移動事件
                OnJoystickMoved();
            }
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (isJoystickPressed)
            {
                // 更新搖杆位置
                joystickPosition = e.Location;
                Invalidate();

                // 觸發搖杆移動事件
                OnJoystickMoved();
            }
        }

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

            // 重置搖杆位置
            joystickPosition = joystickCenter;
            isJoystickPressed = false;
            Invalidate();

            // 觸發搖杆移動事件
            OnJoystickMoved();
        }
        private Point[] GetArrowPoints()
        {
            int x = joystickPosition.X;
            int y = joystickPosition.Y;
            int arrowSize = 25;

            return new Point[]
            {
                new Point(x, y - arrowSize),
                new Point(x - arrowSize / 2, y - arrowSize / 2),
                new Point(x + arrowSize / 2, y - arrowSize / 2),
                new Point(x, y - arrowSize),
                new Point(x - arrowSize / 2, y - arrowSize / 2),
                new Point(x - arrowSize / 2, y + arrowSize / 2),
                new Point(x, y),
                new Point(x - arrowSize / 2, y + arrowSize / 2),
                new Point(x + arrowSize / 2, y + arrowSize / 2),
                new Point(x, y)
            };
        }

        protected virtual void OnJoystickMoved()
        {
            JoystickMoved?.Invoke(this, EventArgs.Empty);
        }           
C# 繪制自定義搖杆(WinForm),實作自由移動物體

移動搖杆:

private void UpdateImagePosition()
        {
            // 計算搖杆與中心位置的距離
            int distanceX = joystickPosition.X - pictureBox2.Width / 2;
            int distanceY = joystickPosition.Y - pictureBox2.Height / 2;

            // 根據距離計算圖檔的移動方向
            if (Math.Abs(distanceX) > Math.Abs(distanceY))
            {
                if (distanceX > 0)
                {
                    // 向右移動
                    imagePosition.X += imageSpeed;
                }
                else
                {
                    // 向左移動
                    imagePosition.X -= imageSpeed;
                }
            }
            else
            {
                if (distanceY > 0)
                {
                    // 向下移動
                    imagePosition.Y += imageSpeed;
                }
                else
                {
                    // 向上移動
                    imagePosition.Y -= imageSpeed;
                }
            }

            // 更新圖檔位置
            pictureBox3.Location = imagePosition;
        }

        private void userControl11_MouseDown(object sender, MouseEventArgs e)
        {
            // 更新搖杆位置
            joystickPosition = e.Location;
            UpdateImagePosition();
        }

        private void userControl11_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                // 更新搖杆位置
                joystickPosition = e.Location;
                UpdateImagePosition();
            }
        }
        private Point joystickPosition;
        private Point imagePosition;
        private int imageSpeed = 1;           

#頭條文章養成計劃#