Jump to content

Any feedback on this c# script


ramadash
 Share

Recommended Posts

Hey I wonder what you think about this script that I find very usefull, I made this to have direct acess autoitx functions from simply including a .dll. So I "remade" some of the functions (this was designed to work with games, so alot of functions are missing) . also the pixelsearch have been remade because I never been able to get it work with autoitx it was returning 0,0 or empty string I don't remember what exactly, but PixelGetColor work's wonder.

Im not really good in programming I only do this as a hobby but I do this since around 1 year and I'm starting to get a little better.

Some feedback's or suggestions would be nice, and if someone can make a use of it then I will be pretty happy about it.

First of all there are 2 references, wich are the autoitx.dll and the other is the type library that referencing a COM with vs.net generates. that's all this script need's to be included as a .dll extension.

I made a description of each variables and theyre value in the source itself, also sorry for my bad english!

to use this from outside, add a using directive to Rama.Aux.Wrapper and then call Functions.Init() to create a static instance of autoit

using System;

namespace Rama.AuX.Wrapper
{
    /// <summary>
    /// Summary description for autoit.
    /// </summary>
    public class Functions
    {
        public Functions()
        {
            //
            // TODO: Add constructor logic here
            //
            autoit.AutoItSetOption("CaretCoordMode",0);
            autoit.AutoItSetOption("ColorMode",1);
            autoit.AutoItSetOption("MouseCoordMode",0);
            autoit.AutoItSetOption("PixelCoordMode",0);
        }
        /// <summary>
        /// Initialize options and create an instance of autoit.
        /// </summary>
        public static void Init()
        {
            autoit=new AutoItX3Lib.AutoItX3Class();
            //autoit.AutoItSetOption("CaretCoordMode",0);
            //autoit.AutoItSetOption("ColorMode",1);
            //autoit.AutoItSetOption("MouseCoordMode",0);
            //autoit.AutoItSetOption("PixelCoordMode",0);
        }
        private static AutoItX3Lib.AutoItX3Class autoit;//=new AutoItX3Lib.AutoItX3Class();

        /// <summary>
        /// ProcessExist autoix wrapper
        /// </summary>
        /// <param name="process">the name of the process</param>
        /// <returns>true if it exist, false otherwise</returns>
        public static bool ProcessExist(string process)
        {
            if (autoit.ProcessExists(process)==0)
                return false;
            else
                return true;
        }
        /// <summary>
        /// Run autoitx wrapper
        /// </summary>
        /// <param name="process">the path of the process to run</param>
        public static void Run(string process)
        {
            Run(process,"");
        }
        public static void Run(string process,string dir)
        {
            Run(process,dir,autoit.SW_MAXIMIZE);
        }
        private static void Run(string process,string dir,int showflag)
        {
            autoit.Run(process,dir,showflag);
        }
        /// <summary>
        /// Process Wait Close autoitx wrapper
        /// </summary>
        /// <param name="process">the process name to close</param>
        /// <param name="timeout">timeout in milisecond for process to close</param>
        public static void ProcessWaitClose(string process,int timeout)
        {
            autoit.ProcessWaitClose(process,timeout);
        }
        /// <summary>
        /// process wait autoitx wrapper
        /// </summary>
        /// <param name="process">process to wait for</param>
        /// <param name="timeout">timeout in milisecond for process to close</param>
        public static void ProcessWait(string process,int timeout)
        {
            autoit.ProcessWait(process,timeout);
        }
        /// <summary>
        /// Process Close autoitx wrapper
        /// </summary>
        /// <param name="process">the name of the process to close</param>
        public static void ProcessClose(string process)
        {
            autoit.ProcessClose(process);
        }
        /// <summary>
        /// Send autoitx wrapper
        /// </summary>
        /// <param name="text">the string text to be sent</param>
        public static void Send(string text)
        {
            Send(text,5);
        }
        public static void Send(string text,int speed)
        {
            Send(text,speed,10);
        }
        public static void Send(string text,int speed,int downLen)
        {
            autoit.Opt("SendKeyDelay",speed);
            autoit.Opt("SendKeyDownDelay",downLen);
            autoit.Send(text,0);
        }
        
        /// <summary>
        /// PixelCheckSum autoitx wrapper
        /// </summary>
        /// <param name="TopLeft">xy value of top left corner</param>
        /// <param name="BottomRight">xy value of bottom right corner</param>
        /// <param name="step">step the pixels are checked</param>
        /// <returns></returns>
        public static int PixelCheckSum(int[] TopLeft,int[] BottomRight,int step)
        {
            sum=0;
            for (int x=TopLeft[0];x<BottomRight[0];x+=step)
            {
                for (int y=TopLeft[1];y<BottomRight[1];y+=step)
                {           
                    sum+=autoit.PixelGetColor(x,y);
                }
            }
            return sum;
        }
        private static int sum=0;
        /// <summary>
        /// PixelSearch autoitx wrapper
        /// </summary>
        /// <param name="TopLeft">xy value of top left corner</param>
        /// <param name="BottomRight">xy value of bottom right corner</param>
        /// <param name="color">the color to look for</param>
        /// <param name="shade">the acceptable color difference</param>
        /// <param name="step">the step btw each pixel searched</param>
        /// <returns></returns>
        public static int[] PixelSearch(int[] TopLeft, int[] BottomRight, int color, int shade, int step)
        {
            for (int x=TopLeft[0];x<BottomRight[0];x+=step)
            {
                for (int y=TopLeft[1];y<BottomRight[1];y+=step)
                {
                    if (autoit.PixelGetColor(x,y)==color)
                        return new int[]{ x,y };
                }
            }
            throw new ApplicationException("Failed to find color");
            //return new int[]{0,0};
        }
        /// <summary>
        /// WinActivate autoitx wrapper (note that this function performs a WinWaitActive)
        /// </summary>
        /// <param name="window">the name of the window to active</param>
        /// <param name="waitactivetimeout">the timeout for WinWaitActive</param>
        public static void WinActivate(string window,int waitactivetimeout)
        {
            autoit.WinActivate(window,"");
            autoit.WinWaitActive(window,"",waitactivetimeout);
            System.Threading.Thread.Sleep(1000);
        }                           
        /// <summary>
        /// WinMove autoitx wrapper
        /// </summary>
        /// <param name="title">the name of the window to move</param>
        /// <param name="x">the x position to move win to</param>
        /// <param name="y">the y position to move win to</param>
        public static void WinMove(string title,int x,int y)
        {
            int xLen=autoit.WinGetPosWidth(title,"");
            int yLen=autoit.WinGetPosHeight(title,"");
            autoit.WinMove(title,"",x,y,xLen,yLen);
        }
        /// <summary>
        /// return mouse x,y
        /// </summary>
        /// <returns></returns>
        public static int[] MouseGetPos()
        {
            return new int[]{autoit.MouseGetPosX(),autoit.MouseGetPosY()};
        }
        /// <summary>
        /// MouseDown and MouseUp autoitx wrapper
        /// </summary>
        /// <param name="button">"Left" or "Right"</param>
        public static void MouseDown(string button)
        {
            autoit.MouseDown(button);
        }
        public static void MouseUp(string button)
        {
            autoit.MouseUp(button);
        }
        /// <summary>
        /// Mouseclick autoitx wrapper
        /// </summary>
        /// <param name="button">"Left" or "Right"</param>
        public static void MouseClick(string button)
        {
            int mousex=autoit.MouseGetPosX();
            int mousey=autoit.MouseGetPosY();
            MouseClick(button,mousex,mousey);
        }
        public static void MouseClick(string button,int x,int y)
        {
            MouseClick(button,x,y,1,1);
        }
        public static void MouseClick(string button,int x,int y,int clicks, int speed)
        {
            autoit.MouseClick(button,x,y,clicks,speed);
        }

        /// <summary>
        /// MouseMove autoit functions wrapper
        /// </summary>
        /// <param name="x">x position to move the mouse to</param>
        /// <param name="y">y position to move the mouse to</param>
        public static void MouseMove(int x, int y)
        {
            MouseMove(x,y,1);
        }
        public static void MouseMove(int x, int y, int speed)
        {
            autoit.MouseMove(x,y,speed);
        }
        public static int PixelGetColor(int x, int y)
        {
            return autoit.PixelGetColor(x,y);
        }
    }
}
Link to comment
Share on other sites

Very cool, great idea. The only thing is, i think this would be more useful in VB, or C++ because C# is mostly a .NET language.

But, this is a great idea, and very well written/commented.

I'd like to help you continue this project, if you'd want some help with it, I have a good amount of C# experience. IM me or PM me if ya want me to help you out.

~cdkid

Edited by cdkid
AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!
Link to comment
Share on other sites

well c# is my favorite language and I don't know anything about c++ and vb. I like .net because anyone can use it from any .net language and also the class libraries are very usefull and vs.net is truly amazing.

I will pm you soon.

Link to comment
Share on other sites

Finaly! someone who thinks the same of .NET as I do! And yes, C# is completely a .NET language. It doesn't work without it.

And in C++, why could you not just use standard Windows APIs?

Yeah, i love .NET, but AutoIt doesnt seem very practical for it, ya know? I think this would do the most good in VB,

--my two cents

~cdkid

AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...