In the C language for DOS, there is a system () function that executes a command-line program and a shell () function in VB, and WINAPI also provides us with similar functions in Windows programming, which are winexec () and ShellExecute (), let's discuss the use of these two functions.
1) winexec ()
Function prototype: UINT winexec (LPCSTR lpcmdline,uint ucmdshow);
Parameter description: (activation means to be able to accept the focus, that is, the title bar becomes blue)
lpCmdLine: A 0-terminated string with command-line arguments.
Ucmdshow: How the new application works. The values are as follows:
Sw_hide Hide
Sw_maximize maximization
Sw_minimize minimized, and the z order sequence is activated after this window (that is, the window's next layer)
Sw_restore activates the window and restores it to its initial size
Sw_show Activate window with current size and status
Sw_showdefault Run as Default
Sw_showmaximized activates the window and maximizes
Sw_showminimized activates the window and minimizes
Sw_showminnoactive minimized but does not change the currently active window
Sw_showna Displays the window in its current state but does not change the currently active window
Sw_shownoactivate to initialize the size display window without changing the currently active window
Sw_shownormal activates and displays the window, and if it is the largest (small), the window will be restored. You should use this value the first time you run a program
For example, I want to use Notepad to open "C:\HDC." TXT ", running in the normal way:
WinExec ("Notepad c:\\hdc.txt", SW_SHOWNORMAL);
If the call succeeds, the function returns a value of not less than 31, otherwise the call fails with the value returned as follows:
0 system memory or insufficient resources
Error_bad_format. EXE file format is not valid (for example, not 32-bit applications)
Error_file_not_found The specified document is provided with a find
Error_path_not_found the specified path was not found
This function is very similar to system () and can only be run. EXE file so that it's not desirable in windows, such as not using this method to open a file through an associated method, such as WinExec ("1.html", sw_showna); You cannot open this document.
2) ShellExecute ()
Function Prototypes:
HINSTANCE ShellExecute (HWND hwnd, LPCTSTR lpoperation, LPCTSTR lpfile, LPCTSTR lpparameters, LPCTSTR lpDirectory, INT nSh Owcmd);
Parameter description:
Handle to the HWND window
Lpoperation operations, such as "open", "print", "explore" correspond to "on", "Print", "tour", or null (""), which means the default action. Lpfile the file to be manipulated.
Lpparameters represents a parameter if lpfile specifies an executable file
Directory for the lpdirectory operation
The operation mode of nShowCmd program, its value is shown in the above example.
If the function call succeeds, the handle of the instance is returned, and if it is unsuccessful, the return value contains the error message, which is not listed here because of the more types, as detailed in the WINAPI Help.
Thus, the example above can be changed to (assuming that the handle of the window is handle)
ShellExecute (Handle, "open", "notepad", "C:\\hdc.txt", "", SW_SHOWNORMAL);
It doesn't have to be that complicated, because. TXT's Association program is Notepad, so as long as this is OK
ShellExecute (Handle, "", "C:\\hdc.txt", "", "", SW_SHOWNORMAL);
This function is similar to the command-line command "start" in Win9x, not only to manipulate files, but also to operate on HTTP, mailto, and so on. So we can design a hyperlink-style program.
Then place the two labels in the form and set their properties to:
Name Caption
Lbhomepage http://www.ccrun.com
Lbemail mailto:ccrun.com@163.com
Change the font to blue and underline, then add the following code to the two label Click event:
ShellExecute (Handle, "", Lbhomepage.caption, "", "", SW_SHOWNORMAL);
ShellExecute (Handle, "", Lbemail.caption, "", "", SW_SHOWNORMAL);
Ok! Try it, how about it? Cool enough!
As you can see from the above, ShellExecute is much more powerful and can completely replace winexec (), in fact, WinExec () is also reserved for compatibility with previous versions, so we should try to use ShellExecute (). In fact, in WINAPI, there is also a function ShellExecuteEx (), but its use is complex, and does not support Windows NT, so this is not introduced here, interested can query WINAPI Help.