Results 1 to 22 of 22

Thread: C# Selecting/Detecting Runescape window

  1. #1
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default C# Selecting/Detecting Runescape window

    I need to do what SCAR/Simba does when you drag the crosshair over scape, except for without the crosshair and the dragging, so basically I need to get the RS window coords, or some kind of a handle n shit

    tips ples

  2. #2
    Join Date
    Nov 2007
    Location
    Chile
    Posts
    1,901
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    What I have in Delphi...
    Code:
    procedure TForm1.BitBtn3MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
      var
        tp : tpoint;
    Begin
      GetCursorPos(TP);
      RS_Handle := WindowFromPoint(TP);
    End;
    what I found on google about C# WindowFromPoint:
    Code:
    [DllImport("user32.dll")]
    public static extern IntPtr WindowFromPoint(Point lpPoint);
    
    [DllImport("user32.dll")]
    public static extern bool GetCursorPos(out Point lpPoint);
    
    public static IntPtr GetWindowUnderCursor()
    {
       Point ptCursor = new Point();
    
       if (!(PInvoke.GetCursorPos(out ptCursor)))
          return IntPtr.Zero;
    
       return WindowFromPoint(ptCursor);
    }ยก
    Hope that helps


  3. #3
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    I'm almost a 100% sure that will get the whole window (e.g FireFox, not just the RuneScape applet)

  4. #4
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Simba is open source
    Verrekte Koekwous

  5. #5
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    Quote Originally Posted by mastaraymond View Post
    Simba is open source
    But Simba is pascal isn't it?

  6. #6
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Heres what I have done, could be ported to C#.
    Pascal Code:
    library FremPlugin;

    {$R *.res}

    uses
      ShareMem, Windows, Messages, SysUtils, Variants, Classes, Controls,
      Forms,Graphics, Dialogs, StdCtrls, StrUtils, ComCtrls;

    type
      TForm1 = class( TForm )
    end;

    type
      PWindows = ^TWindows;
      TWindows = Record
      WindowHandle: HWND;
      WindowText: String;
    end;

    var AWindows: PWindows;
        InFrame, InCanvas, InCanvas2: Boolean;
        Mode: Integer;
        Return: HWND;

    function FindWindowByTitle( WindowTitle: String ): HWND; stdcall; export;
    var
      NextHandle: Hwnd;
      NextTitle: Array[0..260] Of Char;
    begin
      NextHandle := GetWindow( Application.Handle, GW_HWNDFIRST );
      while ( NextHandle > 0 ) do
      begin
        GetWindowText( NextHandle, NextTitle, 255 );

        if ( AnsiContainsText( NextTitle, WindowTitle ) ) then begin
          if ( AnsiContainsText( NextTitle, 'Opera' ) ) then begin
            Mode := 1;
          end else begin
            Mode := 0;
          end;
          Result := NextHandle;
          Exit;
        end else begin
          NextHandle := GetWindow( NextHandle, GW_HWNDNEXT );
        end;
       
      end;
      Result := 0;
    end;

    function EnumChildWindowsProc( Wnd: HWnd ): Boolean; stdcall; export;
    var
      Buffer: Array[0..99] Of Char;
      hWindowText: Array[0..260] Of char;
    begin
      GetWindowText( Wnd, Buffer, 100 );
      RealGetWindowClass( Wnd, hWindowText, 255 );
     
      if ( StrPas( hWindowText ) = '' ) then begin
        hWindowText := 'Empty';
      end;

      if ( StrPas( Buffer ) = '' ) then begin
        Buffer := 'Empty';
      end;

      new(AWindows);

      with AWindows^ do
      begin
        WindowHandle := Wnd;
        WindowText   := StrPas( Buffer );
      end;

      if ( Mode = 1 ) then begin
        if ( InCanvas2 ) then begin
          //ShowMessage( 'RS Canvas: ' + IntToStr( Wnd ) );
          Return := Wnd;
          Result := True;
          InFrame := False;
          InCanvas := False;
          InCanvas2 := False;
          Exit;
        end;

        if ( InCanvas ) and ( StrPas(hWindowText) = 'SunAwtCanvas' ) then begin
          InCanvas2 := True;
        end;
      end;

      if ( Mode = 0 ) then begin
        if ( InCanvas ) then begin
          //ShowMessage( 'RS Canvas: ' + IntToStr( Wnd ) );
          Return := Wnd;
          Result := True;
          InFrame := False;
          InCanvas := False;
          InCanvas2 := False;
          Exit;
        end;
      end;

      if ( StrPas( hWindowText ) = 'SunAwtFrame' ) then begin
        InFrame := True;
      end;

      if ( InFrame ) and ( StrPas(hWindowText) = 'SunAwtCanvas' ) then begin
        InCanvas := True;
      end;

      if ( GetWindow(Wnd, GW_CHILD) <> 0 ) then
      begin
        if not ( Result ) then begin
          Enumchildwindows(Wnd, @EnumChildWindowsProc, 0);
        end;
      end;

      Result := True;
    end;

    function GetRSHandle: HWND;
    var H: HWND;
    begin
      H := FindWindowByTitle( 'RuneScape -' );
      InFrame := False;
      InCanvas := False;
      InCanvas2 := False;
      EnumChildWindows( H, @EnumChildWindowsProc, 0 );
      Result := Return;
    end;

    function GetFunctionCount(): Integer; stdcall; export;
    begin
      Result := 3;
    end;

    function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall; export;
    begin
      case x of
        0:
          begin
            ProcAddr := @FindWindowByTitle;
            StrPCopy(ProcDef, 'function FindWindowByTitle( WindowTitle: String ): HWND;');
          end;
        1:
          begin
            ProcAddr := @EnumChildWindowsProc;
            StrPCopy(ProcDef, 'function EnumChildWindowsProc( Wnd: HWnd ): Boolean;');
          end;
        2:
          begin
            ProcAddr := @GetRSHandle;
            StrPCopy(ProcDef, 'function GetRSHandle: HWND;');
          end;
      else
        x := -1;
      end;
      Result := x;
    end;

    exports GetFunctionCount;
    exports GetFunctionInfo;

    end.

    EDIT: And heres the thread where I discussed this: http://villavu.com/forum/showthread.php?t=54922
    There used to be something meaningful here.

  7. #7
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    Looks good, I'll try porting it although I'm a complete noob to .NET and all of that

    (No FindWindowBySize isn't a cool solution).
    loled irl

  8. #8
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    I would love to hear where you are going to use this, and maybe see the ported version?

    You can add me on MSN if you want: frement@windowslive.com

    Jos muistan oikein ni olit suomalainen?
    There used to be something meaningful here.

  9. #9
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    Olen suomalainen, I live in the US nowadays so I'm rarely on at the same time as GMTers, I'm actually trying to make an utility that some people here will propably hate on, a pin logger that is...

  10. #10
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    A "pin logger", like you log the bank pins? To what purpose if I may ask?
    There used to be something meaningful here.

  11. #11
    Join Date
    Feb 2007
    Location
    Alberta, Canada
    Posts
    4,615
    Mentioned
    50 Post(s)
    Quoted
    429 Post(s)

    Default

    Quote Originally Posted by Frement View Post
    A "pin logger", like you log the bank pins? To what purpose if I may ask?
    Judging from his post, malicious purposes? If we'd hate on it

    Scripts: Edgeville Chop & Bank, GE Merchanting Aid
    Tutorials: How to Dominate the Grand Exchange

    Quote Originally Posted by YoHoJo View Post
    I like hentai.

  12. #12
    Join Date
    Nov 2007
    Location
    Chile
    Posts
    1,901
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Good luck reading the numbers on the pin screen


  13. #13
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    Good luck reading the numbers on the pin screen
    Problem?




    EDIT: Well well well, if it isnt Mr SunAwtCanvas



    Posting code later today, not quite finished


    EDIT: YES!!! THERE IS A GOD! Found out my bitmap copying was flawed and spent hours probind the DC shit for nothing lol


    As I promised: (If this is any use for anyone, esp. if the problem with grabbing the canvas still persists feel free to use this if its any use)

    c Code:
    /*
     * Created by SharpDevelop.
     * User: admin
     * Date: 4.1.2011
     * Time: 18:56
     *
     * To change this template use Tools | Options | Coding | Edit Standard Headers.
     */


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Drawing.Imaging;
    using System.Net.Mail;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Text;
    using System.Diagnostics;

    namespace PinLogger
    {
        class Program
        {
           
            internal delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
            internal delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
           
           
            class GDI32
            {
                [DllImport("GDI32.dll")]
                public static extern bool BitBlt(int hdcDest,int nXDest,int nYDest,int nWidth,int nHeight,int hdcSrc,int nXSrc,int nYSrc,int dwRop);
                [DllImport("GDI32.dll")]
                public static extern int CreateCompatibleBitmap(int hdc,int nWidth, int nHeight);[DllImport("GDI32.dll")]
                public static extern int CreateCompatibleDC(int hdc);
                [DllImport("GDI32.dll")]
                public static extern bool DeleteDC(int hdc);
                [DllImport("GDI32.dll")]
                public static extern bool DeleteObject(int hObject);
                [DllImport("GDI32.dll")]
                public static extern int GetDeviceCaps(int hdc,int nIndex);
                [DllImport("GDI32.dll")]
                public static extern int SelectObject(int hdc,int hgdiobj);
            }
           
            class User32
            {
                [DllImport("User32.dll")]
                public static extern int GetDesktopWindow();
                [DllImport("User32.dll")]
                public static extern int GetWindowDC(int hWnd);
                [DllImport("User32.dll")]
                public static extern int ReleaseDC(int hWnd,int hDC);
                [DllImport("user32.dll") ]
                public static extern int GetWindowText(int hWnd, StringBuilder text, int count);
                [DllImport("user32.dll")]  
                public static extern int GetWindow(int hWnd, GW uCmd);  
                [DllImport("user32.dll")]
                [return: MarshalAs(UnmanagedType.Bool)]
                public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
                [DllImport("user32.dll")]
                [return: MarshalAs(UnmanagedType.Bool)]
                public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
                [DllImport("user32.dll")]
                public static extern uint RealGetWindowClass(IntPtr hwnd, [Out] StringBuilder pszType, uint cchType);
                [DllImport("user32.dll")]
                [return: MarshalAs(UnmanagedType.Bool)]
                public static extern bool GetWindowRect(HandleRef hwnd, out RECT lpRect);
            }
           
            public enum GW : int
            {
                HWNDFIRST = 0,
                HWNDLAST = 1,
                HWNDNEXT = 2,
                HWNDPREV = 3,
                OWNER = 4,
                CHILD = 5,
            }
           
           
            public struct RECT
            {
                public int left;
                public int top;
                public int right;
                public int bottom;
            }
           
            private static bool EnumWindow(IntPtr handle, IntPtr pointer)
            {
                GCHandle gch = GCHandle.FromIntPtr(pointer);
                List<IntPtr> list = gch.Target as List<IntPtr>;
                if (list == null)
                {
                    throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
                }
                list.Add(handle);
                return true;
            }
           
            public String GetWindowCaption(int Hwnd)
            {
                const int nChars = 256;
                StringBuilder builder = new StringBuilder(nChars);
                String result = "";
                if (User32.GetWindowText(Hwnd, builder, nChars) > 0 )
                    result = builder.ToString();
                return result;
            }
           
            public IntPtr FindWindowByTitle(String WindowTitle)
            {
                Process[] p = Process.GetProcesses();
                IntPtr hWnd;
                foreach(Process proc in p)
                {
                    if ((hWnd = proc.MainWindowHandle) != IntPtr.Zero)
                    {
                        if (GetWindowCaption(hWnd.ToInt32()).Contains(WindowTitle))        
                            return hWnd;
                    }
                }
                return IntPtr.Zero;
            }
           
            public IntPtr[] GetChildWindows(IntPtr Parent)
            {
                List<IntPtr> children = new List<IntPtr>();
                GCHandle listHandle = GCHandle.Alloc(children);
                try
                {
                    EnumWindowsProc childProc = new EnumWindowsProc(EnumWindow);
                    User32.EnumChildWindows(Parent, childProc, GCHandle.ToIntPtr(listHandle));
                }
                finally
                {
                    if (listHandle.IsAllocated)
                        listHandle.Free();
                }
                return children.ToArray();
            }
           
            public String GetWindowC(IntPtr hWnd)
            {
                StringBuilder s = new StringBuilder();
                User32.RealGetWindowClass(hWnd, s, 255);
                return s.ToString();
            }
           
            public IntPtr SearchChildWindows(IntPtr[] ChildWindows, String caption)
            {
                foreach(IntPtr i in ChildWindows)
                {
                    if (i != IntPtr.Zero)
                    {System.Console.WriteLine(GetWindowC(i));
                        if (GetWindowC(i).Contains(caption)) {
                            System.Console.WriteLine(";;;"+GetWindowC(i));
                            IntPtr x = new IntPtr(User32.GetWindow(i.ToInt32(), GW.CHILD));
                            while (User32.GetWindow(x.ToInt32(), GW.CHILD) != 0)
                            {
                                x = new IntPtr(User32.GetWindow(x.ToInt32(), GW.CHILD));
                            }
                            return x;
                        }
                    }
                }
                return IntPtr.Zero;
            }
           
            public IntPtr FindChildWindow(String parent, String childtext)
            {
                return SearchChildWindows(GetChildWindows(FindWindowByTitle(parent)), childtext);
            }
           
            public void CaptureScreen(string fileName,ImageFormat imageFormat)
            {
                RECT r;
                User32.GetWindowRect(new HandleRef(this, FindChildWindow("RuneScape", "SunAwtCanvas")), out r);
                int w = r.right-r.left;
                int h = r.bottom-r.top;
                IntPtr hWnd = FindChildWindow("RuneScape", "SunAwtCanvas");
                int hdcSrc = User32.GetWindowDC(hWnd.ToInt32()),
                hdcDest = GDI32.CreateCompatibleDC(hdcSrc),
                hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, w, h);
                User32.GetWindowRect(new HandleRef(this, FindChildWindow("RuneScape", "SunAwtCanvas")), out r);
                System.Console.WriteLine("Width :" + (r.right-r.left) + " Height: "+(r.bottom-r.top));
                GDI32.SelectObject(hdcDest,hBitmap);
                GDI32.BitBlt(hdcDest,0,0,w,
                             h,hdcSrc,0,0,0x00CC0020);
                SaveImageAs(hBitmap,fileName,imageFormat);
                Cleanup(hBitmap,hdcSrc, hWnd, hdcDest);
            }
           
            private void Cleanup(int hBitmap,int hdcSrc, IntPtr hWnd, int hdcDest)
            {
                User32.ReleaseDC(hWnd.ToInt32(), hdcSrc);
                GDI32.DeleteDC(hdcDest);
                GDI32.DeleteObject(hBitmap);
            }
           
            private void SaveImageAs(int hBitmap,string fileName,ImageFormat imageFormat)
            {
               
                Bitmap image = new Bitmap(Image.FromHbitmap(new IntPtr(hBitmap)),
                                          Image.FromHbitmap(new IntPtr(hBitmap)).Width,
                                          Image.FromHbitmap(new IntPtr(hBitmap)).Height);
                image.Save(fileName,imageFormat);
            }
                           
            public static void Main(string[] args)
            {
                Program d = new Program();
                d.CaptureScreen("C:/Users/Admin/RS.Png", ImageFormat.Png);
                Thread.Sleep(10000);
            }
        }
       
    }


    Also, not going to take shit for not shortening code by 2 lines at some possible places, first time with C#
    Last edited by n3ss3s; 01-08-2011 at 01:58 AM.

  14. #14
    Join Date
    Feb 2006
    Location
    Belgium
    Posts
    3,137
    Mentioned
    3 Post(s)
    Quoted
    5 Post(s)

    Default

    Quote Originally Posted by n3ss3s View Post
    I'm almost a 100% sure that will get the whole window (e.g FireFox, not just the RuneScape applet)
    I'm almost 100% sure it won't get the whole window... GetCursorPos + WindowFromPoint for the handle, GetWindowRect to get the window position to calculate relative coordinates

    Only thing you rly need to dig into the client for is sending keys
    Last edited by Freddy1990; 01-09-2011 at 11:00 PM.

  15. #15
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by n3ss3s View Post
    But Simba is pascal isn't it?
    Yes.. And you are capable of reading pascal, so what was your point again
    Verrekte Koekwous

  16. #16
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Quote Originally Posted by mastaraymond View Post
    Yes.. And you are capable of reading pascal, so what was your point again
    He just wanted to be spoon fed.
    There used to be something meaningful here.

  17. #17
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Frement View Post
    He just wanted to be spoon fed.
    There is no spoon
    Verrekte Koekwous

  18. #18
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    I'm almost 100% sure it won't get the whole window... GetCursorPos + WindowFromPoint for the handle, GetWindowRect to get the window position to calculate relative coordinates
    No, that's how you get the firefox window, you have to loop into the sun canvas... Posted the source above so no flame m8... Got everything working fine but now I'm getting a System.ArgumentException it a context that has a notorious reputation for random exceptions


    Also above answers the

    He just wanted to be spoon fed.

    EDIT: managed to fix the shitface, was somehow caused by 2 pretty much consecutive calls of the method that uses getpixel, idk why but now its done..
    Last edited by n3ss3s; 01-11-2011 at 10:48 PM.

  19. #19
    Join Date
    Feb 2006
    Location
    Belgium
    Posts
    3,137
    Mentioned
    3 Post(s)
    Quoted
    5 Post(s)

    Default

    Quote Originally Posted by n3ss3s View Post
    No, that's how you get the firefox window, you have to loop into the sun canvas... Posted the source above so no flame m8... Got everything working fine but now I'm getting a System.ArgumentException it a context that has a notorious reputation for random exceptions
    I don't see how that was flaming, but ok... Here's proof: http://freddy1990.com/files/ClientSel.rar
    Open RS in firefox, drag the crosshair over it, see where ur cursor hits 0,0 on the app.

    (Srr the app isn't pretty, but I hate using a touchpad xD )

  20. #20
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    Hmm, well to be honest I didn't actually try the mouse method as somehow at the time it seemed it would return the same handle as GetDesktopWindow... Also I didn't really mean that you were flaming me

  21. #21
    Join Date
    Feb 2006
    Location
    Belgium
    Posts
    3,137
    Mentioned
    3 Post(s)
    Quoted
    5 Post(s)

    Default

    Yea, np, basically, you get other windows when you select the desktop because you can't actually see the desktop window, it's overlapped by a bunch of other windows that make up the desktop. For example, aside from the taskbar, you have a big empty space with your icons and background, you might think that would be the desktop window, but it's basically a big listview.

  22. #22
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    Woops sorry I meant GetForegroundWindow but yea...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •