#3 VladislavK
Tak i tento problémek jsem vyřešil, FindForm se mi nakonec zdálo no.... spiš nepoužitelny a tak jsem na to šel přes WinApi, Kdyby to někoho zajímalo to ta přikládám kod který řeší můj problem, přiwohnout a doladit se dá kdykoliv
using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Timers;
namespace MK_ServerFeature
{
public class ConfirmDialogHandler : IDisposable
{
const int WM_GETTEXT = 0xD;
const int WM_GETTEXTLENGTH = 0xE;
const int GW_ENABLEDPOPUP = 6;
const uint BM_CLICK = 0xF5;
const int GW_CHILD = 5;
const int GW_HWNDNEXT = 2;
private Timer timer;
private string buttonText;
private IntPtr winHandle;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern IntPtr SendMessageA(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, EntryPoint = "SendMessage")]
public static extern IntPtr SendMessageString(IntPtr hwnd, int wMsg, int wparam, StringBuilder lparam);
public ConfirmDialogHandler(string buttonText,IntPtr winHandle)
{
this.buttonText=buttonText;
this.winHandle=winHandle;
buttonClicked=false;
timer = new Timer();
timer.Interval = 50;
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
timer.Stop();
buttonClicked=LookForAndCloseIEPopup();
if(!buttonClicked)
timer.Start();
}
public ArrayList GetChildWindowHandles(IntPtr ParentWindowHandle)
{
IntPtr ptrChild;
ArrayList clsRet = new ArrayList();
ptrChild = GetChildWindowHandle(ParentWindowHandle);
while (!ptrChild.Equals(IntPtr.Zero))
{
clsRet.Add(ptrChild);
ptrChild = GetNextWindowHandle(ptrChild);
}
return clsRet;
}
public IntPtr GetChildWindowHandle(IntPtr ParentWindowHandle)
{
return GetWindow(ParentWindowHandle, GW_CHILD);
}
public IntPtr GetNextWindowHandle(IntPtr CurrentWindowhandle)
{
return GetWindow(CurrentWindowhandle, GW_HWNDNEXT);
}
public string GetWindowText(IntPtr WindowHandle)
{
IntPtr ptrRet;
IntPtr ptrLength;
ptrLength = SendMessageA(WindowHandle, WM_GETTEXTLENGTH, 0, 0);
StringBuilder sb = new StringBuilder(ptrLength.ToInt32() + 1);
ptrRet = SendMessageString(WindowHandle, WM_GETTEXT, ptrLength.ToInt32() + 1, sb);
return sb.ToString();
}
public bool ClickButton(IntPtr WindowHandle)
{
//IntPtr pp = IntPtr.Zero;
SendMessage(WindowHandle, BM_CLICK, 0, 0);
return true;
}
public bool LookForAndCloseIEPopup()
{
//Process p = Process.GetCurrentProcess();
IntPtr ptrDialogWindow = GetWindow(winHandle/* p.MainWindowHandle*/, GW_ENABLEDPOPUP);
//string txt = GetWindowText(ptrDialogWindow);
if (ptrDialogWindow.ToInt64() > 0)// txt == "Zpráva z webové stránky")// || txt == "Message from webpage" || txt == "Windows Internet Explorer")
{
return ClosePopup(ptrDialogWindow);
}
return false;
}
public bool ClosePopup(IntPtr WindowHandle)
{
ArrayList clsChildHandles = GetChildWindowHandles(WindowHandle);
foreach (IntPtr ptrHandle in clsChildHandles)
{
string tx = GetWindowText(ptrHandle);
if (tx == buttonText)
return ClickButton(ptrHandle);
}
return false;
}
public void Dispose()
{
this.Dispose();
}
public bool buttonClicked { get; set; }
}
}