ich bin gerade dabei, einen KeyPresser zu schreiben.
Er soll in x-Sekunden-Abständen immer eine Taste drücken.
Nun stehe ich vor dem Problem, dass ich das Fenster nicht in den Vordergrund holen kann.
Wie kriege ich es also hin, dass er einen Prozess (ein Fenster) in den Vordergrund holt, und dann die Tasten drückt?
Das ist mein momentaner Code:
Quellcode
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- namespace KeyPresser
- {
- public partial class Presser : Form
- {
- public Presser()
- {
- try
- {
- InitializeComponent();
- lblRunTimeCount.Visible = false;
- lblFinishedRunTimeCount.Visible = false;
- lblRanFor.Visible = false;
- }
- catch(Exception ex)
- {
- throw ex;
- }
- }
- [DllImport('user32.dll')]
- static extern bool IsIconic(IntPtr hWnd);
- [DllImport('user32.dll')]
- private static extern
- bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
- // Win32 API Constants for ShowWindowAsync()
- public const int SW_HIDE = 0;
- public const int SW_SHOWNORMAL = 1;
- public const int SW_SHOWMINIMIZED = 2;
- public const int SW_SHOWMAXIMIZED = 3;
- public const int SW_SHOWNOACTIVATE = 4;
- public const int SW_RESTORE = 9;
- public const int SW_SHOWDEFAULT = 10;
- private void btnStart_Click(object sender, EventArgs e)
- {
- try
- {
- if (tbCharName.Text == "")
- {
- MessageBox.Show("Charname can not be empty!\nPlease enter a Character Name!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- else if (tbCharServer.Text == "")
- {
- MessageBox.Show("Server can not be empty!\nPlease enter your Server!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- else if (tbKeyToPress.Text == "" || tbKeyToPress.Text.Contains(" "))
- {
- MessageBox.Show("Key-Field is not correct!\nPlease enter a correct Value!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- lblRunTimeCount.Visible = true;
- SwitchToProcess("notepad.exe");
- DirectInput.Send(tbKeyToPress.Text);
- }
- catch(Exception ex)
- {
- MessageBox.Show("A unforeseen error has occured", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- throw ex;
- }
- }
- private void btnStop_Click(object sender, EventArgs e)
- {
- try
- {
- string datetime = System.DateTime.Now.ToString();
- string runtimecount = lblRunTimeCount.Text;
- lblRunTimeCount.Text = "Stopped on " + datetime;
- lblFinishedRunTimeCount.Text = runtimecount;
- lblRanFor.Text = System.DateTime.Now.ToString();
- lblRanFor.Visible = true;
- lblFinishedRunTimeCount.Visible = true;
- }
- catch(Exception ex)
- {
- MessageBox.Show("A unforeseen error has occured", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
- throw ex;
- }
- }
- private void timer_Tick(object sender, EventArgs e)
- {
- return;
- }
- private void SwitchToProcess(string m_processName)
- {
- }
- }
- }
Grüße