像Microsoft Internet控件一样,在C#.net中控制Firefox
我想控制Firefox标签。我有一个动态生成固定url参数的应用程序,我将其与url连接并在浏览器上打开 我的问题是,当我通过下面提到的代码从C#应用程序中打开任何url时,它;s始终打开新选项卡,而不是在同一可用选项卡上打开url像Microsoft Internet控件一样,在C#.net中控制Firefox,c#,windows,vb.net,visual-studio,firefox,C#,Windows,Vb.net,Visual Studio,Firefox,我想控制Firefox标签。我有一个动态生成固定url参数的应用程序,我将其与url连接并在浏览器上打开 我的问题是,当我通过下面提到的代码从C#应用程序中打开任何url时,它;s始终打开新选项卡,而不是在同一可用选项卡上打开url Process.Start("firefox", "-url " + newurlscn.Text.ToString()); 我也希望chrome浏览器有这个功能 我有IE浏览器代码,可以在IE中正常工作,但需要Firefox和chrome的帮助 IE代码: S
Process.Start("firefox", "-url " + newurlscn.Text.ToString());
我也希望chrome浏览器有这个功能
我有IE浏览器代码,可以在IE中正常工作,但需要Firefox和chrome的帮助
IE代码:
SHDocVw.ShellWindows pvShell = new ShellWindows();
SHDocVw.InternetExplorer pvWeb2 = null;
SHDocVw.ShellBrowserWindow pvweb3 = null;
int dwCount;
Boolean IsNavigate = false;
if (pvShell.Count > 0)
{
for (dwCount = 0; dwCount < pvShell.Count; dwCount++)
{
var ovIE = pvShell.Item(dwCount);
if (((ovIE.LocationURL.IndexOf("callprocrm.com") > 0) && (ovIE.LocationURL.IndexOf("index.php") > 0)))
{
pvWeb2 = ovIE;
pvWeb2.Navigate(newurlscn.Text, ovIE, ovIE, ovIE, ovIE);
pvWeb2 = null;
IsNavigate = true;
break;
}
}
if (!IsNavigate)
Process.Start("iexplore", newurlscn.Text.ToString());
}
else
Process.Start("iexplore", newurlscn.Text.ToString());
SHDocVw.ShellWindows pvShell=new ShellWindows();
SHDocVw.InternetExplorer pvWeb2=null;
SHDocVw.ShellBrowserWindow pvweb3=null;
整数dwCount;
布尔值=false;
如果(pvShell.Count>0)
{
对于(dwCount=0;dwCount0)和&(ovIE.LocationURL.IndexOf(“index.php”)>0)))
{
pvWeb2=ovIE;
pvWeb2.导航(newurlscn.Text、ovIE、ovIE、ovIE、ovIE);
pvWeb2=null;
IsNavigate=true;
打破
}
}
如果(!IsNavigate)
Process.Start(“iexplore”,newurlscn.Text.ToString());
}
其他的
Process.Start(“iexplore”,newurlscn.Text.ToString());
我请求你尽快帮助我
提前感谢。这是firefox搜索栏中的搜索字符串示例。如果firefox未打开,它将使用搜索选项打开。如果它已经打开,那么它将把它带到前面,并再次搜索字符串。。您可以修改以实现您想要的。使用UI SPY工具实际查看windows UI自动化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Automation;
using Utils;
namespace ControllerX.Executions
{
/// <summary>
/// this class provide searching mechanisms
/// </summary>
class searching
{
public static void search(string Sentence)
{
if ( String.IsNullOrEmpty(Sentence)==true ) return; //no need to search empty value
try
{
searchInFireFox( Sentence);
}
catch (Exception ex)
{
Util.Debuglog(ex.Message);
}//ex
}
private static AutomationElement cached;
private static Process oldproc;
/// <summary>
/// search inside firefox
/// </summary>
/// <param name="search_term"></param>
private static void searchInFireFox(string search_term)
{
//search using firefox
if (oldproc != null && oldproc.HasExited == false && cached!=null )
{
object cachedPattern;
if (true == cached.TryGetCachedPattern(InvokePattern.Pattern, out cachedPattern))
{
InvokePattern iPattern = cachedPattern as InvokePattern ;
Util.BringToFront(oldproc.MainWindowHandle);
if (iPattern != null) { iPattern.Invoke(); Util.SimulateMessage(search_term, true); }
Util.Debuglog("Invoked within cached :" + search_term);
}
return;
}//
Process firefox = null;
if (oldproc != null && oldproc.HasExited == true) { oldproc.Close();}
else { firefox=oldproc;Util.Debuglog("firefox=oldproc"); };
cached = null;
if (firefox==null) firefox = Util.FindByName("firefox");
if (firefox != null)
{
cached = InvokeAndCache(search_term,firefox.MainWindowHandle);
oldproc=firefox;
}//if
else if( open_new(search_term)==false )
Utils.ErrorHandle.notify_user("Could not search with firefox");
}//
private static bool open_new(string search_term)
{
bool ok = false;
Process pfirefox = null;
pfirefox =Util.StartProc(@"firefox.exe" ,
"-new-tab -search \"" + search_term + "\"",
null,
false);
oldproc = pfirefox;
if (pfirefox != null) { ok = true; Util.Debuglog("opened in new : " + search_term); }
return ok;
}//open new
private static AutomationElement InvokeAndCache(string search_term,IntPtr handle)
{
AutomationElement search = null;
try
{
AutomationElement aeDesktop = AutomationElement.RootElement;
AutomationElement aeBrowser = AutomationElement.FromHandle(handle);
// Set up the request.
CacheRequest cacheRequest = new CacheRequest();
cacheRequest.AutomationElementMode = AutomationElementMode.None;
cacheRequest.TreeFilter = Automation.ControlViewCondition;
cacheRequest.Add(AutomationElement.ControlTypeProperty);
cacheRequest.Add(InvokePattern.Pattern);
System.Windows.Automation.Condition conLocation = new OrCondition(
new PropertyCondition(AutomationElement.NameProperty, "Navigation Toolbar"),
new PropertyCondition(AutomationElement.NameProperty, "Панель навигации") );//russian name of the tab
AutomationElement navigation = null;
navigation = aeBrowser.FindFirst(TreeScope.Descendants, conLocation);
cacheRequest.Push();
if (navigation != null)
{
AutomationElementCollection elList = navigation.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
if (elList != null && elList.Count > 1)
search = elList[elList.Count - 1];
}
if (search != null)
{
Util.BringToFront(handle);
InvokePattern iPattern = search.GetCachedPattern(InvokePattern.Pattern) as InvokePattern;
cached = search;
if (iPattern != null) {
iPattern.Invoke();
Util.SimulateMessage(search_term,true); //above what simulateMessage does
// winform.SendKeys.SendWait(message);
// winform.SendKeys.SendWait("{ENTER}");
Util.Debuglog("cached then invoked : " + search_term);
}//if iPattern
}//if
cacheRequest.Pop();
}
catch (Exception ex)
{
Util.Debuglog(ex.Message);
}//try catch
return search;
}
}//end class
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统诊断;
使用System.Windows.Automation;
使用UTIL;
命名空间控制器x.Executions
{
///
///此类提供搜索机制
///
类搜索
{
公共静态无效搜索(字符串语句)
{
if(String.IsNullOrEmpty(句子)==true)返回;//无需搜索空值
尝试
{
搜索中缀(句子);
}
捕获(例外情况除外)
{
Util.Debuglog(例如Message);
}//前
}
私有静态自动元素缓存;
私有静态进程oldproc;
///
///在firefox中搜索
///
///
私有静态void searchInFireFox(字符串搜索\u术语)
{
//使用firefox进行搜索
if(oldproc!=null&&oldproc.HasExited==false&&cached!=null)
{
对象缓存模式;
if(true==cached.TryGetCachedPattern(InvokePattern.Pattern,out cachedPattern))
{
InvokePattern iPattern=缓存模式为InvokePattern;
Util.BringToFront(oldproc.MainWindowHandle);
if(iPattern!=null){iPattern.Invoke();Util.SimulateMessage(search_term,true);}
Debuglog(“在缓存中调用:“+search_term”);
}
返回;
}//
进程firefox=null;
如果(oldproc!=null&&oldproc.HasExited==true){oldproc.Close();}
else{firefox=oldproc;Util.Debuglog(“firefox=oldproc”);};
cached=null;
如果(firefox==null)firefox=Util.FindByName(“firefox”);
如果(firefox!=null)
{
cached=InvokeAndCache(搜索词,firefox.MainWindowHandle);
oldproc=firefox;
}//如果
else if(打开新项(搜索项)==false)
Utils.ErrorHandle.notify_用户(“无法使用firefox搜索”);
}//
私有静态bool open\u new(字符串搜索\u术语)
{
bool ok=false;
进程pfirefox=null;
pfirefox=Util.StartProc(@“firefox.exe”,
“-新选项卡-搜索\”“+搜索\术语+”\”“,
无效的
假);
oldproc=pfirefox;
if(pfirefox!=null){ok=true;Util.Debuglog(“以新方式打开:“+search_term”);}
返回ok;
}//打开新的
私有静态AutomationElement InvokeAndCache(字符串搜索词,IntPtr句柄)
{
AutomationElement搜索=null;
尝试
{
AutomationElement aeDesktop=AutomationElement.RootElement;
AutomationElement aeBrowser=AutomationElement.FromHandle(句柄);
//设置请求。
CacheRequest CacheRequest=新的CacheRequest();
cacheRequest.AutomationElementMode=AutomationElementMode.None;
cacheRequest.TreeFilter=Automation.ControlViewCondition;
cacheRequest.Add(AutomationElement.ControlTypeProperty);
cacheRequest.Add(InvokePattern.Pattern);
System.Windows.Automation.conLocation=新的或条件(
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
//using winform = System.Windows.Forms;
using WindowsInput;
/*
* author:qwr
*
* */
namespace Utils
{
/// <summary>
/// Utility methods for using through the program
/// </summary>
class Util
{
/// <summary>
/// Starting external program
/// </summary>
/// <param name="file_name">path of the executable file</param>
/// <param name="arguments">arguments to be passed </param>
/// <param name="hidden">options for showing windows</param>
/// <returns>return Process if the function succeeds otherwise null</returns>
public static Process StartProc(string file_name, string arguments,string workdir, bool hidden)
{
Process proc = null;
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
if (workdir!=null && workdir.Length>0)
startInfo.WorkingDirectory = workdir;
if (arguments != null && arguments.Length > 0)
startInfo.Arguments = arguments;
startInfo.FileName = file_name;
if (hidden == true)
{
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
}//hidden
proc = new Process();
proc.StartInfo = startInfo;
proc.Start();
Debuglog("startProc started " + file_name);
}//try
catch(Exception ex)
{
Debuglog("startProc " + file_name);
Debuglog("startProc error :"+ ex.Message);
proc = null;
}//try-catch
return proc;
}//startproc
/// <summary>
/// Kill all instances of process by name
/// </summary>
/// <param name="name">name of process</param>
public static void KillProc(string name)
{
Process[] Proclist = Process.GetProcessesByName(name);
foreach (Process proc in Proclist)
{
proc.Kill();
}//for
return;
}//killproc
/// <summary>
/// Kill Process
/// </summary>
/// <param name="process"></param>
public static void KillProc( Process process)
{
try
{
if (process != null && process.HasExited == false)
{
process.Kill();
Util.Debuglog("trying to kill process..."+process.Id);
}//if null
}//try
catch (Exception ex)
{
Util.Debuglog("fail to kill");
ex.Message.toLog();
ex.StackTrace.toLog();
}//try-catch
}
/// <summary>
/// Finds first occurence of the process instance by its name
/// </summary>
/// <param name="name">name of processed that will be searched</param>
/// <returns>return Process on succed otherwise null</returns>
public static Process FindByName(string name)
{
Process found = null;
//need to find through name
Process[] myProcess = Process.GetProcesses();
foreach (Process p in myProcess)
if (p.ProcessName == name) { found = p; break; }
return found;
}
/// <summary>
/// Convert unix time to C# date
/// </summary>
/// <param name="unixTime"></param>
/// <returns></returns>
public static DateTime CSharpTime(double unixTime)
{
DateTime unixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0,DateTimeKind.Utc);
return unixStartTime.AddSeconds(unixTime).ToLocalTime();
}//ToCSharpTime
/// <summary>
/// Makes windows appear on the top
/// </summary>
/// <param name="handle">hadnle of window</param>
public static void BringToFront(IntPtr handle)
{
native.WINDOWPLACEMENT wndP;
native.GetWindowPlacement(handle, out wndP);
if (wndP.ShowCmd == native.WindowShowStyle.ShowMinimized || wndP.ShowCmd == native.WindowShowStyle.Minimize)
native.ShowWindow(handle, native.WindowShowStyle.ShowDefault);
native.SetForegroundWindow(handle);
}
/// <summary>
/// simulating keyboard .
/// </summary>
/// <param name="message">message that will be typed</param>
/// <param name="enterKey">indicates if enter key will be send after message</param>
/// <remarks>this function uses windows.forms.sendkeys.sendwait
/// for better performance you can use other simulators
/// or direct text manipulation methods
/// </remarks>
public static void SimulateMessage(string message, bool enterKey)
{
// winform.SendKeys.SendWait(message); //calls application doevents inside
// if (enterKey) winform.SendKeys.SendWait("{ENTER}");
InputSimulator.SimulateTextEntry(message);
if (enterKey) InputSimulator.SimulateKeyPress(VirtualKeyCode.RETURN);
}
private static readonly object locker = new object();
/// <summary>
/// small log . debug only
/// </summary>
/// <param name="s"></param>
// [ConditionalAttribute("DEBUG")]
public static void Debuglog(string s)
{
lock (locker)
using (StreamWriter nm = new StreamWriter("log", true, Encoding.UTF8))
{
nm.Write(DateTime.Now.ToLongTimeString());
nm.Write("--- > ");
nm.WriteLine(s);
}//using
}
}//end class
}//end namespace