self updating drawings C#

202 views Asked by At

I am trying to make an external map for a computer game. Therefore I have made a Forms Application with a picture box, that contains my map image. Now I want to draw little squares onto the map using GDI. I allready got that working using Graphics.DrawRectangle. Now I want to update the position of the rectangle every 0.2s. How do I do that?

My current source (i wnt to replace the button with an auto-update):

    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.");
        }

    }
1

There are 1 answers

3
José Corretjer-Gómez On BEST ANSWER

How about you store two time variables(DateTime) one that has the time when you started checking for that 2 second difference, another with current time and on the beginning of every iteration you verify if the difference of both times is 2 seconds. Remember, the first variable is the one that has the time when the difference was 2 seconds or when you first started checking for that difference.

You could also use the Timer class and set a timer that on ever 2secs do something.

Timer class reference: https://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx