sendkey输入到c#中应用程序上的弹出窗口

本文关键字:程序上 窗口 应用程序 应用 输入 sendkey | 更新日期: 2023-09-27 18:29:00

我正试图通过以编程方式发送回车键来关闭程序中的这个启动弹出窗口。我已经在这个网站上学习了一些关于在同一命名空间中创建一个类来处理这个问题的例子,但我不知道如何在主形式中使用它。请从sendkey类中查看下面的代码。提前感谢

class winhandler
{
    [DllImport("user32.dll", SetLastError = true)]
    private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint procId);
    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool SetForegroundWindow(IntPtr hWnd);
    public const uint WM_SETTEXT = 0x000C;
    public const uint WM_GETTEXT = 0x000D;
    public const uint EM_SETSEL = 0x000000B1;
    public const uint WM_GETTEXTLENGTH = 0x000E;
    public const uint SMTO_ABORTIFHUNG = 0x0002;
    public const int BM_CLICK = 0x00F5;
    string title = "Card ....";
    public void cancelwindows()
    {
        IntPtr hWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, null, title);
        uint loginWindowProcId;
        uint loginWindowThreadId = GetWindowThreadProcessId(hWnd, out loginWindowProcId);
        // now I can just use .NET to find the Process for me...
        Process loginWindowProcess = null;
        if (0 != loginWindowProcId)
        {
            loginWindowProcess = Process.GetProcessById((int)loginWindowProcId);
            loginWindowProcess.WaitForInputIdle();
            SetForegroundWindow(hWnd);
            SendKeys.SendWait("{ENTER}"); 
        }
    }
}

sendkey输入到c#中应用程序上的弹出窗口

我处理了很多受密码保护的excel表,我写了一些类似的东西,以便在需要时为我键入密码。

下面是一个表单示例,它等待一个包含"记事本"的窗口打开,然后再键入并关闭。

此示例使用计时器每隔500毫秒检查一次窗口。显然,如果您希望窗口出现,您可以简单地使用while循环,直到它出现或类似的东西。

我不能100%确定你遇到的问题是哪一部分,但如果这不能回答你的问题,请随时告诉我:)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CloseWindowTest
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
        private System.Windows.Forms.Timer windowTimer;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            windowTimer = new System.Windows.Forms.Timer();
            windowTimer.Interval = 500;
            windowTimer.Tick += t_Tick;
            windowTimer.Enabled = true;
        }
        private void t_Tick(object sender, EventArgs e)
        {
            if (GetActiveWindowTitle().Contains("Notepad"))
            {
                try
                {
                    windowTimer.Enabled = false;
                    SendKeys.SendWait("Notepad Detected");
                    Environment.Exit(0);
                }
                catch (Exception wtf) { MessageBox.Show(wtf.ToString()); }
            }
        }
        private string GetActiveWindowTitle()
        {
            const int nChars = 256;
            StringBuilder Buff = new StringBuilder(nChars);
            IntPtr handle = GetForegroundWindow();
            if (GetWindowText(handle, Buff, nChars) > 0)
            {
                return Buff.ToString();
            }
            return null;
        }
    }
}