Various uses of ShellExecute

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

A, use the mailer sending e-mail system default

Uses

..., ShellAPI;

Var

lpHwnd: HWND;

lpOperation, lpFile, lpParameters, lpDirectory: String;

Begin

lpHwnd:= GetDesktopWindow();

lpOperation:= 'open';

lpFile:= 'mailto:' + '' + '?' //The main email address to the '?' end

+ '&cc=' + '' //CC

+ '&bcc=' + 'lyf.kibosoft.com' //Send the dark

+ '&subject=' + 'Theme'

+ '&body='+ 'The first line '+'%0d' + 'second line' // subject to a'%0d'switch

+ '&File=' + 'D:\Help.rtf'; //The attachment is only valid for Outlook Express

lpParameters:= '';

lpDirectory:= '';

ShellExecute(lpHwnd, pChar(lpOperation),

pChar(lpFile), pChar(lpParameters),

pChar(lpDirectory), SW_SHOWNORMAL);

End;

Two, open a webpage

ShellExecute(0, 'open', 'http://www.google.com', '', '', SW_SHOWNORMAL);

Three, print the document

ShellExecute(0, 'print', 'D:\Help.rtf', '', '', SW_SHOWNORMAL)

Four, use the system default program to open the specified file

ShellExecute(0, 'open', 'D:\Help.rtf', '', '', SW_SHOWNORMAL))

Five, open the specified file with the specified program

ShellExecute(0, 'open', 'notepad.exe', 'D:\Help.rtf', '', SW_SHOWNORMAL)

Six, execution of the shortcut(.LNK)

ShellExecute(handle,'open','c:\windows\desktop\aaa.lnk','', '', sw_hide); 

WinExec run the EXE file. Such as: WinExec('Notepad.exe Readme.txt', SW_SHOW);

ShellExecute not only can run the EXE file, can also run has associated file.

First, must have a reference to the shellapi.pas unit: uses ShellAPI;

1 standard.

The ShellExecute function prototype and parameters:

function ShellExecute(hWnd: HWND; Operation, FileName, Parameters,Directory: PChar; ShowCmd: Integer): HINST; stdcall;

●hWnd: Used to specify the parent window handle. When the error function call, it will serve as the Windows message window's parent window. For example, it can be set to the main application window handle, namely Application.Handle, it can also be set as the desktop window handle (use the GetDesktopWindow function to obtain the).

●Operation: Specifies the action to be taken. The "open" operation that perform specified by the FileName parameter program, or open the specified by the FileName parameter file or folder; "print" said printing operation is specified by the FileName parameter file; "explore" operation that the browser is specified by the FileName parameter folder. When the parameter is set to nil, said the default action"open".

●FileName: Used to specify which file to open, to execute the program file name or browse to the folder name.

●Parameters: If the FileName parameter is an executable program, then this parameter specifies the command line parameters, otherwise, this parameter should be nil or PChar(0).

●Directory: To specify the default directory.

●ShowCmd: If the FileName parameter is an executable program, then this parameter specifies the initial program window display mode, otherwise, this parameter should be set to 0.

If ShellExecute the function succeeds, the return value is the instance handle the program being executed. If the return value is less than 32, that there are errors.

The above is just the standard usage of ShellExecute function, will introduce the following special usage of it.

2 special usage

If the FileName parameter is set to "http:" protocol format, then the function will open the default browser and links to the address specified in URL. If you installed the machine in multiple browsers, the function will be based on the HTTP protocol handler Windows 9x/NT Registry (Protocols Handler) settings determine the start which browser.

Format a: http:// domain name.

Such as: ShellExecute(handle, 'open', http:// ; www.neu.edu.cn', nil, nil, SW_SHOWNORMAL);

Scheme two: http:// website / webpage file name.

Such as: ShellExecute(handle, 'open', http:// ; , SW_SHOWNORMAL);

If the FileName parameter is set to "mailto:" protocol format, then the function will start the default mail client, such as Microsoft Outlook (including Microsoft Outlook Express) or Netscape Messanger. If you install the machine in a mail client, then the function will set the mailto protocol handler Windows 9x/NT registry to determine which one starts the mail client program.

Format a: mailto:

Such as: ShellExecute (handle, 'open', 'mailto:', nil, nil, SW_SHOWNORMAL); a new message window opens.

Scheme two: mailto: user account @ mail server address

Such as: ShellExecute (handle, 'open', ', nil, nil, SW_SHOWNORMAL); a new message window opens, and automatically fill in the recipient address. If you specify multiple recipient address, the recipient address must be separated with semicolons or commas (hereinafter the same).

Scheme three: mailto: user account @ mail server address? Theme Body= subject= mail text

Such as: ShellExecute (handle, 'open', '? Subject=HelloBody=This is a test ', nil, nil, SW_SHOWNORMAL); a new message window opens, and automatically fill in the recipient address, email and message body. If the message body contains multiple lines of text, you must add a newline escape character between each line of text%0a.

Example(delphi):

In an application called c:Project1.exe;

ShellExecute(handle, 'open','c:Project1.exe','String content',nil, SW_SHOWNORMAL);

You can call in Project1.exe:

procedure TForm1.FormCreate(Sender: TObject);

var i:integer;

begin

for i:=1 to paramcount do

if ParamStr(i)〈〉'' then showmessage(ParamStr(i));

end;

The final parameter specifies a command, visibility for the window.

Please use any of the following a constant

SW_HIDE hidden window, active to make a window

SW_MINIMIZE minimized window, active to make a window

SW_RESTORE with the size and the location of the original display a window, at the same time it enters the active state

SW_SHOW with the current size and position display a window, at the same time it enters the active state

SW_SHOWMAXIMIZED maximize the window, and its activation

SW_SHOWMINIMIZED minimized window, and its activation

SW_SHOWMINNOACTIVE minimizes a window, but does not change the active window

SW_SHOWNA with the current size and position display a window, do not change the active window

SW_SHOWNOACTIVATE with the size and location of the nearest display a window, but does not change the active window

SW_SHOWNORMAL and SW_RESTORE are the same

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Ferdinand at February 14, 2014 - 9:12 PM