C ++ activates external files or programs

Source: Internet
Author: User

I used MFC to write an APP and asked the About dialog box to display the LOGO, company name, address, and company URL link. After the user clicks the URL link, start the browser to access the Web page. The hot link in this dialog box adds a lot of colors to the application. The shellexecute.pdf of Microsoft is able to complete this function. This function can not only open a specific file (.txt;. word...), but also open a fixed website (www.baidu.com)

The meanings of the parameters and prototype of the ShellExecute Function are as follows:

HINSTANCE ShellExecute (hWnd: HWND; Operation, FileName, Parameters, Directory: PChar; ShowCmd: Integer );

HWnd: Specifies the parent window handle. When an error occurs during a function call, it is used as the parent window of the Windows message window. For example, you can set it to the Application Main Window Handle, that is, Application. Handle, or desktop window Handle (obtained using the GetDesktopWindow function ).
Operation: Specifies the Operation to be performed. The "open" operation indicates that the program specified by the FileName parameter is executed, or the file or folder specified by the FileName parameter is opened. The "print" operation indicates that the file specified by the FileName parameter is printed; the "Browse e" operation indicates browsing the folder specified by the FileName parameter. If the parameter is set to nil, the default operation "open" is executed ".
FileName: Specifies the name of the file to be opened, the name of the program file to be executed, or the name and path of the folder to be browsed.
Parameters: If the FileName parameter is an executable program, this parameter specifies the command line parameter; otherwise, this parameter should be NULL.
Directory: Specifies the default Directory. Generally, this parameter must be NULL.
ShowCmd: If the FileName parameter is an executable program, this parameter specifies the initial display mode of the program window. Otherwise, this parameter should be set to 0.
If the ShellExecute Function is successfully called, the returned value is the instance handle of the executed program. If the returned value is smaller than 32, an error occurs.

// Possible errors returned include:
= 0 {insufficient memory}
ERROR_FILE_NOT_FOUND = 2; {file name error}
ERROR_PATH_NOT_FOUND = 3; {path name error}
ERROR_BAD_FORMAT = 11; {the EXE file is invalid}
SE_ERR_SHARE = 26; {share error}
SE_ERR_ASSOCINCOMPLETE = 27; {the file name is incomplete or invalid}
SE_ERR_DDETIMEOUT = 28; {timeout}
SE_ERR_DDEFAIL = 29; {DDE Transaction Failed}
SE_ERR_DDEBUSY = 30; {another DDE transaction is being processed and this DDE transaction cannot be completed}
SE_ERR_NOASSOC = 31; {No associated application}

Optional values of the/ShowCmd parameter:
SW_HIDE = 0; {hide}
SW_SHOWNORMAL = 1; {display with the nearest size and position, activate}
SW_NORMAL = 1; {same as SW_SHOWNORMAL}
SW_SHOWMINIMIZED = 2; {minimize, activate}
SW_SHOWMAXIMIZED = 3; {maximize, activate}
SW_MAXIMIZE = 3; {same as SW_SHOWMAXIMIZED}
SW_SHOWNOACTIVATE = 4; {display with the nearest size and position, not activated}
SW_SHOW = 5; {same as SW_SHOWNORMAL}
SW_MINIMIZE = 6; {minimized, not activated}
SW_SHOWMINNOACTIVE = 7; {same as SW_MINIMIZE}
SW_SHOWNA = 8; {same as SW_SHOWNOACTIVATE}
SW_RESTORE = 9; {same as SW_SHOWNORMAL}
SW_SHOWDEFAULT = 10; {same as SW_SHOWNORMAL}
SW_MAX = 10; {same as SW_SHOWNORMAL}


Example:


In the button function of a dialog box in MFC

Open a new txt file on the desktop:
HINSTANCE I = ShellExecute (this-> m_hWnd, _ T ("open"), _ T ("D: \ My Documents \ Desktop \ new document .txt "), NULL, NULL, SW_SHOWNORMAL );

Open Baidu homepage: HINSTANCE I = ShellExecute (0, _ T ("open"), _ T ("www.baidu.com"), NULL, NULL, SW_SHOWNORMAL );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.