sendkey进入c#中的应用程序弹出窗口

时间:2014-11-03 21:52:58

标签: c#

我试图通过以编程方式发送回车键来关闭此启动程序弹出程序。我已经按照本网站中的一些示例关于在同一名称空间中创建一个类来进行handel,但我不知道如何在主窗体中使用它。从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}"); 
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我使用了许多受密码保护的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;
        }
    }
}