Laden...

Forenbeiträge von helios300 Ingesamt 11 Beiträge

07.10.2021 - 19:34 Uhr

Ich habe mein Code jetzt dementsprechend angepasst und nun funktioniert es:


public static void RechnerWindowLocation()
        {
            IntPtr hwnd = FindWindow(null, "Rechner");
            Rect NotepadRect = new Rect();
            GetWindowRect(hwnd, ref NotepadRect);
            MessageBox.Show($"Left:{NotepadRect.Left} Right:{NotepadRect.Right} Top:{NotepadRect.Top} Bottom:{NotepadRect.Bottom}");
        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            RechnerWindowLocation();
        }

Danke an alle für die Hilfe!

06.10.2021 - 18:59 Uhr

Leider hat das auch nichts gebracht.. bekomme weiterhin keinen Fehler sondern nur den Wert 0 für alle 4 Parameter.

s.a. den Code bei
>

Das habe ich gerade probiert.. funktioniert allerdings nur bei dem eigenen Programm, sprich für Form1.
Wenn ich versuche diese Methode bei einem externen Process anzuwenden, bekomme ich die MessageBox mit "ERROR".

Die Methoden die ich ausprobiert habe:


        public static void RechnerWindowLocation()
        {
            Process[] processes = Process.GetProcessesByName("Calculator");
            Process lol = processes[0];
            IntPtr ptr = lol.MainWindowHandle;
            Rect NotepadRect = new Rect();
            GetWindowRect(ptr, ref NotepadRect);
            MessageBox.Show($"Left:{NotepadRect.Left} Right:{NotepadRect.Right} Top:{NotepadRect.Top} Bottom:{NotepadRect.Bottom}");
        }

        public static void RechnerWindowLocationZwei()
        {
            Rect rct;
            Rectangle myRect = new Rectangle();
            Process[] processes = Process.GetProcessesByName("Calculator");
            Process lol = processes[0];
            IntPtr ptr = lol.MainWindowHandle;
            Rect NotepadRect = new Rect();

            if (!GetWindowRect(ptr, ref NotepadRect));
            {
                MessageBox.Show("ERROR");
                return;
            }
            MessageBox.Show(rct.ToString());

            myRect.X = rct.Left;
            myRect.Y = rct.Top;
            myRect.Width = rct.Right - rct.Left + 1;
            myRect.Height = rct.Bottom - rct.Top + 1;
        }

06.10.2021 - 16:43 Uhr

Hallo liebe Community,

ich melde mich wieder mit einer Frage..
Ich möchte die Rectangle-Werte von dem Rechner Fenster anzeigen lassen.. doch wenn ich den Button klicke, bekomme ich für alle 4 Parameter den Wert 0.


        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindow(string strClassName, string strWindowName);

        [DllImport("user32.dll")]
        public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);

        public struct Rect
        {
            public int Left { get; set; }
            public int Top { get; set; }
            public int Right { get; set; }
            public int Bottom { get; set; }
        }

......

       public static void RechnerWindowLocation()
        {
            Process[] processes = Process.GetProcessesByName("Calculator"); 
            Process lol = processes[0];                                                          
            IntPtr ptr = lol.MainWindowHandle;                                            
            Rect NotepadRect = new Rect();
            GetWindowRect(ptr, ref NotepadRect);
            MessageBox.Show($"Left:{NotepadRect.Left} Right:{NotepadRect.Right} Top:{NotepadRect.Top} Bottom:{NotepadRect.Bottom}");
        }

        private void button1_Click(object sender, EventArgs e)
        {
           RechnerWindowLocation();
        }

Der Code funktioniert zwar.. doch ich bekomme die Werte " Left:0 Right:0 Top:0 Bottom:0 "

Kann mir jemand verraten, was ich falsch mache?

Vielen Dank im Voraus !!

Grüße,
h300

05.10.2021 - 19:29 Uhr

Interessant.. Ok ich werde mir das definitiv durchlesen.

Allerdings habe ich gemerkt, dass mein Form ziemlich buggy ist. Ich denke das hat was mit der Autoit Bibliothek zu tun. Es lassen sich einige Funktionen im loop nicht ausführen. Ich klick auf den Standard X Button und es schließt sich nicht usw.

Ich denke, ich muss eine rein C# PixelSearch Funktion finden.

05.10.2021 - 17:39 Uhr

Dann ist aber die Exception-Meldung eigenartig (bzgl. Int32)???

Dann probiere mal

  
dynamic pix = au3.PixelSearch(0, 0, 1920, 1080, 0x1F3560);  
if (pix.GetType() == typeof(object[]))  
{  
   object[] pixCoord = (object[])pix;  
   // ...  
}  
  

Ok das ist etwas komisch..
Ich habe zuerst den Code so umgestellt:


dynamic pix = au3.PixelSearch(0, 0, 1920, 1080, 0x1F3560);  //PixelSearch gibt eine 1 aus falls es nichts findet
                    if (pix.ToString() != "1")
                    {
                        object[] pixCoord = (object[])pix;
                        au3.MouseMove((int)pix[0], (int)pix[1], 10); // Parameter: x, y, speed
                        au3.MouseClick("LEFT", (int)pix[0], (int)pix[1], 2, 10); // Parameter; x, y, x mal clicken, speed
                        break;
                    }

Dann hab ich den Error bekommen:

Fehlermeldung:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot convert type 'int' to 'object[]''

Doch dann nahm ich den Code was du geschrieben hast 1 zu 1:


dynamic pix = au3.PixelSearch(0, 0, 1920, 1080, 0x1F3560);
                    if (pix.GetType() == typeof(object[]))
                    {
                        object[] pixCoord = (object[])pix;
                        au3.MouseMove((int)pixCoord[0], (int)pixCoord[1], 10);
                        au3.MouseClick("LEFT", (int)pixCoord[0], (int)pixCoord[1], 2, 10);
                        break;
                    }

Nun funktioniert es..

Was hat


if (pix.GetType() == typeof(object[]))

denn geändert? Ich verstehe das im Moment nicht..

05.10.2021 - 16:20 Uhr

dynamic PixelSearch(int nLeft, int nTop, int nRight, int nBottom, int nCol, int nVar = 0, int nStep = 1);

05.10.2021 - 15:23 Uhr

Hey Th69,

danke für deine Antwort.

Ich hab die Array Deklaration jetzt entfernt, doch bekomme denselben Fehler bei:


object pix = au3.PixelSearch(0, 0, 1920, 1080, 0x1F3560);
....
object[] pixCoord = (object[])pix;

Gruß,
h300

05.10.2021 - 14:34 Uhr

Liebe Community,

ich bin neu zu CSharp und bin im Moment am experimentieren..

Ich bekomme jedoch bei einer meiner Funktionen dieses Error und weiß nicht wie ich das beheben soll.
Ich habe gegoogelt aber wurde jedoch nicht schlau, da fast alle ein Problem bei Int32 -> String haben und nicht Int32 -> Object[].

Mein Code:


AutoItX3 au3 = new AutoItX3();
......

while(true)
            {
                au3.WinWaitActive("Notepad");
                while (true)
                {
                    object[] pix = au3.PixelSearch(0, 0, 1920, 1080, 0x1F3560);  //PixelSearch gibt eine 1 aus falls es nichts findet 
                    if (pix.ToString() != "1")
                    {
                        object[] pixCoord = (object[])pix;
                        au3.MouseMove((int)pix[0], (int)pix[1], 10); // Parameter: x, y, speed
                        au3.MouseClick("LEFT", (int)pix[0], (int)pix[1], 2, 10); // Parameter; x, y, x mal clicken, speed
                        break;
                    }
                    Thread.Sleep(200);
                }

                Thread.Sleep(200);
            }
.....

Error:> Fehlermeldung:

System.InvalidCastException: 'Unable to cast object of type 'System.Int32' to type 'System.Object[]'

Viele Grüße,
h300

07.07.2021 - 19:15 Uhr

Ok sorry! 😁

07.07.2021 - 19:12 Uhr

Ok das werde ich ausprobieren, danke!
Komisch das Backgroundworker so veraltet ist. Auf youtube nutzen das noch sehr viele die "c# tutorials" aufnehmen.

Danke für die Antwort!
Ok keine Bilder. Ich lese mir die Woche die Regeln durch =)

07.07.2021 - 18:15 Uhr

Hallo liebe Community,

ich bin neu zu C# und dem Forum hier.

Ich habe ein GUI erstellt womit ich "checke" ob ein Process existiert oder nicht. Doch ich habe Probleme beim updaten der Label.

Wenn ich den Target-Process öffne, dann mein Tool, dann steht da "Process Found!"
Edit Abt: Externes Bild entfernt.

Doch wenn ich den Target-Process schließe, bleibt mein Label bei "Process Found!"

Und bei "Process Not Found!" ist es genau andersrum.
Edit Abt: Externes Bild entfernt.

Mein Code:


private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int pID = m.GetProcIdFromName("AoEDE_s");

            while (true)
            {
                if (pID > 0) //Falls der Process existiert
                {
                    openProc = m.OpenProcess(pID);
                    processLabel.Invoke(new MethodInvoker(delegate () { processLabel.Text = "Process Found!"; }));
                    processLabel.ForeColor = Color.Green;
                    processLabel.Invoke(new MethodInvoker(delegate () { processLabel.Update(); }));
                    processLabel.Invoke(new MethodInvoker(delegate () { processLabel.Refresh(); }));
                }
                else if (pID == 0) //Falls der Process nicht existiert
                {
                    processLabel.Invoke(new MethodInvoker(delegate () { processLabel.Text = "Process not Found!"; }));
                    processLabel.ForeColor = Color.Red;
                    processLabel.Invoke(new MethodInvoker(delegate () { processLabel.Update(); }));
                    processLabel.Invoke(new MethodInvoker(delegate () { processLabel.Refresh(); }));
                }
                Thread.Sleep(500);
            }
        }

Ich muss noch sagen, dass processLabel.Text = "Process Found!" so nicht geklappt hat. Es gab immer ein Error dann habe ich hier im Forum das mit dem Invoke gefunden.
Bin mir auch nicht sicher ob ich das richtig benutze, aber so bekomme ich kein Error.

Ich bedanke mich für jegliche Hilfe und Antwort

MfG,
helios300