您的位置:首页 > 运维架构 > Shell

windows客户端开发--通过ShellExecute函数打开浏览器

2016-02-19 21:35 489 查看
在我们的客户端中常常会有一些link,点击后希望通过浏览器导航到该链接。

我们是通过ShellExecute函数来实现的。

ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件、打开一个目录、打印一个文件等等),并对外部程序有一定的控制。

注意,要使用这个函数,要引入头文件:

[code]#include <shellapi.h>


看看函数原型:

[code]ShellExecute(
hWnd: HWND; {指定父窗口句柄}
Operation: PChar; {指定动作, 譬如: open、runas、print、edit、explore、find[2]  }
FileName: PChar; {指定要打开的文件或程序}
Parameters: PChar; {给要打开的程序指定参数; 如果打开的是文件这里应该是 nil}
Directory: PChar; {缺省目录}
ShowCmd: Integer {打开选项}
)


可以通过ShellExecute打开windows系统自带的记事本、计算器等等。

我们这里需要的是打开一个链接,如www.baidu.com

更进一步,我们如何指定浏览器来打开www.baidu.com.

我们应该再一次关于一下这个函数的参数:

lpFile [in]

Type: LPCTSTR

A pointer to a null-terminated string that specifies the file or object on which to execute the specified verb. To specify a Shell namespace object, pass the fully qualified parse name. Note that not all verbs are supported on all objects. For example, not all document types support the “print” verb. If a relative path is used for the lpDirectory parameter do not use a relative path for lpFile.

lpParameters [in, optional]

Type: LPCTSTR

If lpFile specifies an executable file, this parameter is a pointer to a null-terminated string that specifies the parameters to be passed to the application. The format of this string is determined by the verb that is to be invoked. If lpFile specifies a document file, lpParameters should be NULL.

lpDirectory [in, optional]

Type: LPCTSTR

A pointer to a null-terminated string that specifies the default (working) directory for the action. If this value is NULL, the current working directory is used. If a relative path is provided at lpFile, do not use a relative path for lpDirectory.

所以,我们可以这样使用:

[code]#include<iostream>
#include<Windows.h>
#include<shellapi.h>
int main()
{
    //使用IE浏览器打开www.baidu.com
    ShellExecute(NULL, L"open", L"iexplore.exe", L"www.baidu.com", NULL, SW_MAXIMIZE);

    //使用搜狗浏览器打开www.baidu.com
    ShellExecute(NULL, L"open", L"SogouExplorer.exe", L"www.baidu.com", NULL, SW_MAXIMIZE);

   //使用默认浏览器打开www.baidu.com,我用的是chrome
    ShellExecute(NULL, L"Open", L"www.baidu.com", 0, 0, SW_SHOWNORMAL);

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: