Go The magical application of ShellExecute in Delphi

Source: Internet
Author: User

The function of ShellExecute in Delphi ShellExecute is to run an external program (either open a registered file, open a directory, print a file, etc.) and have some control over the external program. One, ShellExecute common methods there are several API functions can implement these functions, but in most cases shellexecute is more used, and it is not too complex. Here is an example of how to use it. 1. Start a new application
ShellExecute (Handle, ' Open ', PChar (' C:\test\app.exe '), nil, nil, sw_show); 2. Open Notepad and open a file (the system recognizes the path to the Notepad application, so we do not have to use absolute paths)
ShellExecute (Handle, ' Open ', PChar (' Notepad '), PChar (' C:\test\readme.txt '), nil, sw_show); 3. Print a document
ShellExecute (Handle, ' print ', PChar (' C:\test\test.doc '), nil, nil, sw_show); Note: You may see word temporarily open, but it will automatically close. 4. Open an HTML page
ShellExecute (Handle, ' Open ', PChar (' http://www.festra.com/'), nil, nil, sw_show); 5. You can open the application via a registered file type
ShellExecute (Handle, ' Open ', PChar (' C:\test\readme.txt '), nil, nil, sw_show); 6. Open a directory with Windows Explorer
ShellExecute (Handle, ' Explore ', PChar (' C:\Windows) ', nil, nil, sw_show); 7. Call the Resource Manager ShellExecute (0, ' explore ', 0,filename,0,1);   The filename here is a drive, such as C \ 8. Run a DOS command and return immediately
ShellExecute (Handle, ' Open ', PChar (' Command.com '), PChar ('/C copy file1.txt File2.txt '), nil, sw_show); 9. Run a DOS command and keep the DOS window present
ShellExecute (Handle, ' Open ', PChar (' Command.com '), PChar ('/k dir '), Nil, sw_show); 10. Send email v_mailto:= ' mailto: ' +filename; FileName is an email address
v_mailto:=v_mailto+ '? subject= ' +docname; DocName as Title
ShellExecute (0, ' open ', v_mailto,0,0,1);
Ii. ShellExecute Detailed ShellExecute functions are used to open or print a specified file. The specified file must be an executable file or a document file.
HInstance ShellExecute (
HWND hwnd,//Handle to parent window
LPCTSTR lpoperation,//Pointer to string the specifies operation to perform
LPCTSTR lpfile,//pointer to filename or folder name string
LPCTSTR lpparameters,//pointer to string that specifies Executable-file parameters
LPCTSTR lpdirectory,//Pointer to string, specifies default directory
INT nShowCmd//Whether file is shown when opened
) * Params (parametric interpretation)
Hwnd
Specifies the parent window handle. This window receives all the messages generated by the project. For example, a project can report an error through a message box.
Lpoperation
Describes the execution of an operation with a null-terminated string. The following action string is valid: line number parameter meaning
The 1 "open" function opens the file specified by Lpfile. This file must be an executable file or a document file. The folder must be a folder that can be opened.
The 2 "print" function prints the file specified by Lpfile. This file should be a document file. If the specified file is an executable file, the function opens the file in open mode.
The 3 "Explore" function will search for the folder specified by Lpfile. If the lpoperation parameter is NULL, the function opens the file specified by Lpfile.
Lpfile
Opens or prints the specified file with a null-terminated string description, or opens or searches for the specified
Folder. function to open a specified file or folder, or to print a document file.
Lpparameters
If lpfile specifies an executable file, Lpparameters assigns the execution file to the application's parameters as a null-terminated string.
Line number parameter meaning
1 sw_hide hides this form and activates other forms.
2 Sw_maximize maximizes the specified form.
3 sw_minimize minimizes the specified form and activates the topmost form sequentially.
4 Sw_restore activates and displays the form. If the form is minimized or maximized, the form reverts to its original size and position. The application specifies the markup when a minimized form is restored.
5 Sw_show activates and displays the form at its current size and position.
6 Sw_showdefault
7 sw_showmaximized activates and maximizes the display of the form.
8 sw_showminimized activates and minimizes the reality form.
9 sw_showminnoactive Minimize the form and keep it active.
Sw_showna displays the form in its current state, keeping it active.
Sw_shownoactivate displays the form in its current size and position and retains its active status.
Sw_shownormal activates and displays a form. If the form is maximized or minimized, the form reverts to its original size and position. When the form is first displayed, the application records the markup.
If Lpfield specifies a document file, lpparameters should be null.
Lpdirectory
Specifies the default directory at which the file begins to run, with a null-terminated string.
nShowCmd
If Lpfield specifies an executable file, nShowCmd specifies how the file is displayed when it is opened. This parameter can be any of the following values: If Lpfield specifies a document file, nShowCmd should be 0. * Return values (return value)
If the function executes successfully, the return value is the handle to the instance of the project that is already running, or a handle to the Dynamic Data Exchange Server application.
(Please refer to the Delphi online Help for a list of return values withheld)
* Remarks (Note)
The file specified by the Lpfile parameter must be an executable file or a document file. If the file is a document file, the ShellExecute function will open or print the file as determined by the lpoperation parameter. If it is an executable file, the ShellExecute function will open it even if the lpoperation parameter is specified as printing.
You can use ShellExecute to open a live search for a folder. You can open a folder in any of the following ways:
ShellExecute (handle, NULL, path_to_folder, NULL, NULL, SW_SHOWNORMAL);
Or
ShellExecute (Handle, "open", path_to_folder, NULL, NULL, SW_SHOWNORMAL);
Use the following call to search for a folder:
ShellExecute (handle, "explore", path_to_folder, NULL, NULL, SW_SHOWNORMAL)
If the lpoperation parameter is NULL, the function opens the file specified by Lpfile. If the lpoperation parameter is "open" or "explore", the function forces the form or explorer to open.
Note: The way to open a webpage in a new window
ShellExecute (self.     Handle, PChar (' open '), PChar (' Explorer '), PChar (URL), nil, sw_maximize); Three. Frequently Asked Questions
Q: How do I open an app?
ShellExecute (This->m_hwnd, "open", "calc.exe", "", "", sw_show);
Or
ShellExecute (This->m_hwnd, "open", "notepad.exe", "C:\MyLog.log", "", sw_show);
Q: How do I open a document associated with a system program?
ShellExecute (This->m_hwnd, "open", "C:\abc.txt", "", "", sw_show);
Q: How do I open a webpage?
ShellExecute (This->m_hwnd, "open", "http://www.google.com";, "", "", sw_show);
Q: How to activate the relevant program, send email?
ShellExecute (This->m_hwnd, "open", "mailto:[email protected]"," "," ", sw_show);
Q: How do I print a document using a system printer?
ShellExecute (This->m_hwnd, "print", "C:\abc.txt", "", "", sw_hide);
Q: How can I use the system lookup function to find the specified file?
ShellExecute (M_hwnd, "find", "D:\nish", null,null,sw_show);
Q: How do I start a program until it runs to the end?
Shellexecuteinfo Shexecinfo = {0};
shexecinfo.cbsize = sizeof (SHELLEXECUTEINFO);
Shexecinfo.fmask = see_mask_nocloseprocess;
Shexecinfo.hwnd = NULL;
Shexecinfo.lpverb = NULL;
Shexecinfo.lpfile = "C:\MyProgram.exe";
Shexecinfo.lpparameters = "";
Shexecinfo.lpdirectory = NULL;
Shexecinfo.nshow = Sw_show;
Shexecinfo.hinstapp = NULL;
ShellExecuteEx (&shexecinfo);
WaitForSingleObject (Shexecinfo.hprocess,infinite);
Or:
Process_information ProcessInfo;
Startupinfo Startupinfo; Entry parameters
ZeroMemory (&startupinfo, sizeof (STARTUPINFO));
STARTUPINFO.CB = sizeof Startupinfo; Allocation size
if (CreateProcess ("C:\Winnt\Notepad.exe", NULL,
Null,null,false,0,null,
Null,&startupinfo,&processinfo))
{
WaitForSingleObject (Processinfo.hprocess,infinite);
CloseHandle (Processinfo.hthread);
CloseHandle (processinfo.hprocess);
}
Else
{
MessageBox ("The process could not being started ...");
}
Q: How do I display the properties of a file or folder?
Shellexecuteinfo Shexecinfo ={0};
shexecinfo.cbsize = sizeof
SHELLEXECUTEINFO);
Shexecinfo.fmask = see_mask_invokeidlist;
Shexecinfo.hwnd = NULL;
Shexecinfo.lpverb = "Properties";
Shexecinfo.lpfile = "C: \"; It can also be a file
Shexecinfo.lpparameters = "";
Shexecinfo.lpdirectory = NULL;
Shexecinfo.nshow = Sw_show;
Shexecinfo.hinstapp = NULL Q: The file that needs to be used uses SHELLAPI; Four. Common methods 2 1. This function can open any file, even desktop and URL shortcuts (. ink or. url). ShellExecute parses all the contents of the system registry HKEY_CLASSES_ROOT, determines which one executes the program, launches a new instance, or uses DDE to connect the file name to an open instance. Then, ShellExecute returns an instance handle of the app that opened the file.
Example: Open a bitmap file called Iloveu.bmp.   ShellExecute (Handle, ' open ', ' c:\iloveu.bmp ', nil,nil,sw_shownormal); 2. Open the Web page. When ShellExecute encounters the "http:" in front of the file name, you can tell that the file you want to open is a Web file that starts with Internet Explorer or Netscape Navigator or any other browser you use to open the file.
Example: Open Web: http://hi.baidu.com/yueyun889 ShellExecute (Handle, ' open ', ' http://hi.baidu.com/yueyun889 ', nil,nil,sw_   Shownormal); 3.ShellExecute can also recognize other agreements, such as FTP, GOPHER. Even the knowledge "mailto, if the file name points to" Mailto:[email protected] ", it will activate the email program (Windows Outlook, etc.) and open a new pending edit email.
Example: Open a new email, the recipient of the new mail: [email protected] ShellExecute (Handle, ' open ', ' mailto:[email protected] ', nil,nil,sw_shownormal)     ; Of course, you can also specify multiple recipients: ShellExecute (Handle, ' open ', ' Mailto:[email protected];[   Email protected] ', nil,nil,sw_shownormal);   4. Open a new application: ShellExecute (Handle, ' Open ', PChar (' C:\test\app.exe '), nil, nil, sw_show); 5. Print the notebook and open a file (the system can remember the path of the program, so you do not need to use the absolute path) ShellExecute (Handle, ' print ', PChar (' C:\test\test.doc '), nil, nil, sw_ SHOW);
Note: You may see word temporarily being opened, but it will automatically turn off 6. Open a ShellExecute with Windows Explorer (Handle, ' Explore ', PChar (' C:\Windows '), Nil, nil,   Sw_show); 7. Run a new DOS command and immediately return to ShellExecute (Handle, ' Open ', PChar (' Command.com '), PChar ('/C copy file1.txt File2.txt '), nil, sw_show)   ; 8. Run a DOS command and keep the DOS window present ShellExecute (Handle, ' Open ', PChar (' Command.com '), '/k dir ', Nil, sw_show);

Go The magical application of ShellExecute in Delphi

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.