Windows Client development-browser connection through the ShellExecute Function
There are often some links in our client. After clicking, we hope to navigate to the link through the browser.
We implemented it through the ShellExecute Function.
The function of ShellExecute is to run an external program (or open a registered file, open a directory, print a file, etc.) and control the external program.
Note: To use this function, introduce the header file:
#include
Look at the function prototype:
ShellExecute (hWnd: HWND; {specify parent window handle} Operation: PChar; {specify actions, such as open, runas, print, edit, e, find [2]} FileName: PChar; {specify the file or program to be opened} Parameters: PChar; {specify Parameters for the program to be opened; if the file is opened, it should be nil} Directory: PChar; {default directory} ShowCmd: Integer {open option })
You can use ShellExecute to open notepad, calculator, and so on in windows.
What we need here is to open a link, such as www.baidu.com
Further, how do we specify a browser to open www.baidu.com.
We should repeat the parameters of this function:
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 shocould 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.
Therefore, we can use:
# Include
# Include
# Include
Int main () {// open www.baidu.com ShellExecute (NULL, L "open", L "iexplore.exe", L "www.baidu.com", NULL, SW_MAXIMIZE) in IE browser ); // use the sogou browser to open www.baidu.com ShellExecute (NULL, L "open", L "SogouExplorer.exe", L "www.baidu.com", NULL, SW_MAXIMIZE); // open www.baidu.com in the default browser, chrome ShellExecute (NULL, L "Open", L "www.baidu.com", 0, 0, SW_SHOWNORMAL); return 0 ;}