簡體   English   中英

sendkey在C#中進入應用程序的彈出窗口

[英]sendkey enter to a pop up on application in c#

我試圖通過以編程方式發送回車鍵來關閉此啟動彈出窗口。 我已經按照本站點中的一些示例在同一個名稱空間中創建一個類來處理此問題,但是我不知道如何以主要形式使用它。 從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}"); 
        }
    }
}

我使用許多受密碼保護的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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM