自更新绘图 C#

self updating drawings C#

我正在尝试为电脑游戏制作外部地图。 因此,我制作了一个带有图片框的表单应用程序,其中包含我的地图图像。 现在我想使用 GDI 在地图上绘制小方块。我已经使用 Graphics.DrawRectangle 完成了这项工作。 现在我想每 0.2 秒更新一次矩形的位置。 我该怎么做?

我当前的来源(我想用自动更新替换按钮):

    public partial class Form1 : Form
{
    //choords local player
    int localX;
    int localY;
    int running;
    const int Basex = 0x05303898;
    const int Basey = 0x05303894;
    const string Game = "ac_client";
    //map drawing
    Pen aPen = new Pen(Color.Black);
    Graphics localp;



    //choords enemy

    //permission to read process memory
    const int PROCESS_VM_READ = 0x0010; //needed for reading memory


    [DllImport("kernel32.dll")]
    public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

    [DllImport("kernel32.dll")]
    public static extern bool ReadProcessMemory(int hProcess,
    int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

        if (Process.GetProcessesByName(Game).Length > 0)
        {
            Process process = Process.GetProcessesByName(Game)[0];
            IntPtr procHandle = OpenProcess(PROCESS_VM_READ, false, process.Id);

            int bytesRead = 0;
            byte[] buffer = new byte[24]; //'Hello World!' takes 12*2 bytes because of Unicode 


            // 0x0046A3B8 is the address where I found the string, replace it with what you found
            ReadProcessMemory((int)procHandle, Basex, buffer, buffer.Length, ref bytesRead);
            localX = BitConverter.ToInt32(buffer, 0);
            LBlocalx.Text = Convert.ToString(Math.Ceiling(Convert.ToDecimal(localX)));


            ReadProcessMemory((int)procHandle, Basey, buffer, buffer.Length, ref bytesRead);
            localY = BitConverter.ToInt32(buffer, 0);
            LBlocaly.Text = Convert.ToString(Math.Ceiling(Convert.ToDecimal(localY)));


            localp = pictureBox1.CreateGraphics();
            localp.DrawRectangle(aPen, (Convert.ToInt32(Convert.ToString(Math.Ceiling(Convert.ToDecimal(localX))))/1000), (Convert.ToInt32(Convert.ToString(Math.Ceiling(Convert.ToDecimal(localY))))/1000), 10, 10);

        }
        else
        {
            MessageBox.Show("Error! Process not running.");
        }

    }

你如何存储两个时间变量 (DateTime),一个是你开始检查 2 秒差异的时间,另一个是当前时间,在每次迭代开始时你验证差异两次都是2秒。请记住,第一个变量是差异为 2 秒或您第一次开始检查该差异时的时间。

您还可以使用 Timer class 并设置一个计时器,每 2 秒执行一次操作。

定时器class参考: https://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx