分別用c++,c#,vb script 來打開www.baidu.com
使用c++:
(下面是傳遞參數的結構)
typedef struct _SHELLEXECUTEINFO {
DWORD cbSize;
ULONG fMask;
HWND hwnd;
LPCTSTR lpVerb;
LPCTSTR lpFile;
LPCTSTR lpParameters;
LPCTSTR lpDirectory;
int nShow;
HINSTANCE hInstApp;
LPVOID lpIDList;
LPCTSTR lpClass;
HKEY hkeyClass;
DWORD dwHotKey;
union {
HANDLE hIcon;
HANDLE hMonitor;
} DUMMYUNIONNAME;
HANDLE hProcess;
} SHELLEXECUTEINFO, * LPSHELLEXECUTEINFO;
(打開實例)
#include < windows.h >
#include < shellapi.h >
#include < stdio.h >
int main( int argc, char * argv[])
{
SHELLEXECUTEINFO shell = { sizeof(shell) };
shell.fMask = SEE_MASK_FLAG_DDEWAIT;
shell.lpVerb = " open " ;
shell.lpFile = " IEXPLORE.EXE " ;
shell.lpParameters = " http://www.baidu.com " ;
shell.nShow = SW_SHOWNORMAL;
BOOL ret = ShellExecuteEx( & shell);
return 0 ;
} 使用c#:
(參數ProcessStartInfo ,參看msdn)
(使用實例 :只有在STA線程上ShellExecute 才能確保工作無誤。在Process的實現中,并沒有考慮到這個問題,所以使用Process類運行ShellExecute可能會出錯。如果你不能保證調用Process.Start的線程的ApartmentState,可以使用如下的代碼來避免這個問題:)
using System;
using System.Threading;
public class Foo
{
public static void OpenUrl()
{
System.Diagnostics.Process.Start(@"http://www.baidu.com");
}
public static void Main()
{
ThreadStart openUrlDelegate = new ThreadStart(Foo.OpenUrl);
Thread myThread = new Thread(openUrlDelegate);
myThread.SetApartmentState(ApartmentState.STA);
myThread.Start();
myThread.Join();
}
}
使用windows腳本:
javascript:
// Create Internet Explorer Object
//InternetExplorer.Application 運行IE瀏覽器
ie = new ActiveXObject("InternetExplorer.Application");
//var ie=wscript.createobject("internetexplorer.application","event_");
// Define how the window should look
ie.left=-5;
ie.top=-25;
ie.height=860;//高度
ie.width=1035;//寬度
ie.menubar=0;//取消菜單欄
ie.addressbar=0;//取消地址欄
ie.toolbar=0;//取消工具欄
ie.statusbar=0;//取消狀態欄
ie.resizable=0;//不允許用戶改變窗口大小
ie.visible=1;//窗口可見
ie.navigate("http://www.baidu.com")
//打開窗口
VBScript:
Dim objIE
Set objIE = WScript.CreateObject ("InternetExplorer.Application")
objIE.AddressBar = true
objIE.Visible = true
objIE.Navigate("www.baidu.com")
參考:
http://blogs.msdn.com/oldnewthing/archive/2004/11/26/270710.aspx
http://blog.joycode.com/gangp/archive/2004/08/14/30668.aspx
附錄:(來自msdn)
c#:
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample

{
/**//// <summary>
/// Shell for the sample.
/// </summary>
class MyProcess
{

/**//// <summary>
/// Opens the Internet Explorer application.
/// </summary>
void OpenApplication(string myFavoritesPath)
{
// Start Internet Explorer. Defaults to the home page.
Process.Start("IExplore.exe");
// Display the contents of the favorites folder in the browser.
Process.Start(myFavoritesPath);
}

/**//// <summary>
/// Opens urls and .html documents using Internet Explorer.
/// </summary>
void OpenWithArguments()
{
// url's are not considered documents. They can only be opened
// by passing them as arguments.
Process.Start("IExplore.exe", "www.northwindtraders.com");
// Start a Web page using a browser associated with .html and .asp files.
Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
}

/**//// <summary>
/// Uses the ProcessStartInfo class to start new processes, both in a minimized
/// mode.
/// </summary>
void OpenWithStartInfo()
{
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
startInfo.Arguments = "www.northwindtraders.com";
Process.Start(startInfo);
}
static void Main()
{
// Get the path that stores favorite links.
string myFavoritesPath =
Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
MyProcess myProcess = new MyProcess();
myProcess.OpenApplication(myFavoritesPath);
myProcess.OpenWithArguments();
myProcess.OpenWithStartInfo();
}
}
}
CLI/C++:
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;

/**//// <summary>
/// Opens the Internet Explorer application.
/// </summary>
void OpenApplication( String^ myFavoritesPath )

{
// Start Internet Explorer. Defaults to the home page.
Process::Start( "IExplore.exe" );
// Display the contents of the favorites folder in the browser.
Process::Start( myFavoritesPath );
}


/**//// <summary>
/// Opens urls and .html documents using Internet Explorer.
/// </summary>
void OpenWithArguments()

{
// url's are not considered documents. They can only be opened
// by passing them as arguments.
Process::Start( "IExplore.exe", "www.northwindtraders.com" );
// Start a Web page using a browser associated with .html and .asp files.
Process::Start( "IExplore.exe", "C:\\myPath\\myFile.htm" );
Process::Start( "IExplore.exe", "C:\\myPath\\myFile.asp" );
}


/**//// <summary>
/// Uses the ProcessStartInfo class to start new processes, both in a minimized
/// mode.
/// </summary>
void OpenWithStartInfo()

{
ProcessStartInfo^ startInfo = gcnew ProcessStartInfo( "IExplore.exe" );
startInfo->WindowStyle = ProcessWindowStyle::Minimized;
Process::Start( startInfo );
startInfo->Arguments = "www.northwindtraders.com";
Process::Start( startInfo );
}
int main()

{
// Get the path that stores favorite links.
String^ myFavoritesPath = Environment::GetFolderPath( Environment::SpecialFolder::Favorites );
OpenApplication( myFavoritesPath );
OpenWithArguments();
OpenWithStartInfo();
}
posted on 2006-09-27 15:59 夢在天涯 閱讀(3635) 評論(3) 編輯 收藏 引用 所屬分類: CPlusPlus 、C#/.NET 、Windows Script

