windows客户端开发--通过ShellExecute函数打开浏览器
2016-02-19 21:35
489 查看
在我们的客户端中常常会有一些link,点击后希望通过浏览器导航到该链接。
我们是通过ShellExecute函数来实现的。
ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件、打开一个目录、打印一个文件等等),并对外部程序有一定的控制。
注意,要使用这个函数,要引入头文件:
看看函数原型:
可以通过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.
所以,我们可以这样使用:
我们是通过ShellExecute函数来实现的。
ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件、打开一个目录、打印一个文件等等),并对外部程序有一定的控制。
注意,要使用这个函数,要引入头文件:
#include <shellapi.h>
看看函数原型:
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.
所以,我们可以这样使用:
#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; }
相关文章推荐
- windows客户端开发--通过ShellExecute函数打开浏览器
- (7) linux shell 命令 – mv
- shell脚本自动测试+自动覆盖率统计+自动转PDF+自动发邮件
- GNU Linux shell中如何进行各进制编码间(二进制、8进制、16进制、base64)的转换
- shell脚本教程
- 启动spark-shell提示scala版本不一致的解决
- shell脚本:批量修改文件名(删除文件名中字符)
- shell脚本:批量修改文件名(删除文件名中字符)
- shell脚本命令继续执行
- shell 获取当前的事件戳
- PowerShell 批量添加域用户(Windows 2012 R2)
- 在shell 中利用命令查看某个进程的进程id
- shell编程——if语句 if -z -n -f -eq -ne -lt
- scp命令报错-bash: scp: command not found
- powershell中使用New-WebServiceProxy调用webapi(webservice使用可,wcf使用不可)
- 怎样拿shell的技巧
- 当powershell遇上mysql引发的血案
- 入门参考两相宜——观《Windows PowerShell实战指南》有感
- php传参调用shell脚本执行代码
- 48个shell技巧