C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

分别用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 梦在天涯 阅读(3576) 评论(3)  编辑 收藏 引用 所属分类: CPlusPlusC#/.NETWindows Script

评论

# re: 分别用c++,c#,vb script 来打开www.baidu.com 2007-06-25 21:39 永胜

我要劲舞  回复  更多评论   

# re: 分别用c++,c#,vb script 来打开www.baidu.com 2007-06-25 21:40 永胜

WOAI NI   回复  更多评论   

# re: 分别用c++,c#,vb script 来打开www.baidu.com 2012-10-25 10:27 吾要茶坊

分别用c++,c#,是什么意思?  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1785161
  • 排名 - 5

最新评论

阅读排行榜