I am trying to close this startup pop up in program by sending an enter key programmatically. I have followed some of the examples in this site about creating a class within the same namespace to handel this, but I don't know how to use it in main form. check the code below from the sendkey class. thank you in advance
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}");
}
}
}
I work with a lot of password protected excel sheets and I wrote something similar to type the password for me whenever it is requested.
Here is an example of a form that waits for a window containing "Notepad" to open before typing into it and closing.
This example uses a timer to check for the window every 500 milliseconds. Obviously, if you're expecting the window to appear, you could simply use a while loop until it appears or something similar.
I'm not 100% sure which part it is that you're having trouble with, but please feel free to let me know if this doesn't answer your question :)
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;
}
}
}
User contributions licensed under CC BY-SA 3.0