光标在移动,但不能在游戏中移动

时间:2019-07-14 02:09:41

标签: c# cursor

我正在为自己编程一个小工具,只是为了好玩。这是一款您可以弹吉他的游戏,我想编写一个程序,使吉他自动演奏一些优美的小歌曲。问题是:我可以在桌面上毫无问题地移动光标。光标也可以在游戏的设置菜单中移动。但是当我尝试时,当我在游戏中(控制玩家/摄像机)时,光标没有任何作用。 Playermodel甚至没有移动头部。 (它是Windows窗体)

我试图激活菜单中的程序(以确保它能正常工作),并且该程序可以工作并切换到播放器/相机,并且它不会移动。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ghk = new KeyHandler(Keys.Multiply, this);
            ghk.Register();
            KeyPositioner();

        }
        bool guitarPlay = false;
        private void HandleHotkey()
        {
            if(checkBox1.Checked)
            {
                guitarPlay = true;
            }

            if(guitarPlay = true)
            {
                for(int i = Cursor.Position.Y; i < 1000; i++)
                {
                    Cursor.Position = new Point(i);
                    System.Threading.Thread.Sleep(100);
                }
                //Cursor.Position = new Point(Cursor.Position.X - 0, Cursor.Position.Y + 30);
                //System.Threading.Thread.Sleep(100);
                //Cursor.Position = new Point(Cursor.Position.X - 0, Cursor.Position.Y - 80);
                //System.Threading.Thread.Sleep(100);
                //Cursor.Position = new Point(Cursor.Position.X - 0, Cursor.Position.Y - 90);
                //System.Threading.Thread.Sleep(100);
                //Cursor.Position = new Point(Cursor.Position.X - 0, Cursor.Position.Y + 10);
                //System.Threading.Thread.Sleep(100);
                //Cursor.Position = new Point(Cursor.Position.X - 0, Cursor.Position.Y - 70);
            }
            KeyPositioner();
            // Do stuff...
            //this.Cursor = new Cursor(Cursor.Current.Handle);
            Cursor.Position = new Point(Cursor.Position.X - 0, Cursor.Position.Y + 10);
            KeyPositioner();
        }

        private void KeyPositioner()
        {
            int posx = Cursor.Position.X;
            int posy = Cursor.Position.Y;
            string posxx;
            string posyy;


            posxx = Convert.ToString(posx);
            posyy = Convert.ToString(posy);

            label1.Text = posxx;
            label4.Text = posyy;
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Constants.WM_HOTKEY_MSG_ID)
                HandleHotkey();
            base.WndProc(ref m);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public class KeyHandler
        {
            [DllImport("user32.dll")]
            private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);

            [DllImport("user32.dll")]
            private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

            private int key;
            private IntPtr hWnd;
            private int id;

            public KeyHandler(Keys key, Form form)
            {
                this.key = (int)key;
                this.hWnd = form.Handle;
                id = this.GetHashCode();
            }

            public override int GetHashCode()
            {
                return key ^ hWnd.ToInt32();
            }

            public bool Register()
            {
                return RegisterHotKey(hWnd, id, 0, key);
            }

            public bool Unregiser()
            {
                return UnregisterHotKey(hWnd, id);
            }
        }

        private KeyHandler ghk;

        public static class Constants
        {
            //windows message id for hotkey
            public const int WM_HOTKEY_MSG_ID = 0x0312;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}

我希望代码可以将播放器模型的摄像机视图移到其中。

0 个答案:

没有答案