More and more programmers like to add a link to a website or an electronic mailbox in the program written. Users only need to click the mouse to automatically open the browser and link to the specified URL address, or open the "New Window" of the Mail Customer, even the recipient, mail theme, and mail text can be filled with users.
If you want users to download upgrades, patches from the website, or introduce the latest information on the product, then add the link to the website, this is undoubtedly better than letting the user open the browser, and then enter the URL to make more convenience It is also more interested in users. If you add a link to the electronic mailbox in the program, then the user can send the program to the program to provide better technical support and after-sales service, but also facilitate the activities such as user surveys.
This article
Take Delphi as an example, this paper describes how to link directly from the application to the specified site or to send an email to the working principle and implementation method.
working principle
Many programmers know
Windows API Functions SHELLEXECUTE can be used to execute programs, open files, or folders, but browse URL addresses with this function or send new emails, I am afraid that the understanding is not much. This use is not mentioned in Windows Help Document Win32.hlp.
Standard usage
SHELLEXECUTE function prototype and parameter meanings as follows:
Function shellexecute (HWND: HWnd; Operation, FileName, Parameters, Directory: Pchar; showcmd: integer): hinst; stdcall;
● hWnd: Used to specify the parent window handle. When a function calling process occurs, it will be the parent window of the Windows message window. For example, it can be set to the application main window handle, that is, Application.Handle, or it can be set to the desktop window handle (obtained with the getDesktopWindow function).
● Operation: Used to specify the operation to be performed. The "Open" operation represents the program specified by the filename parameter, or opens the file or folder specified by the filename parameter; "Print" means that the file specified by the FileName parameter; "Explore" operation indicates that the browsing is specified by the filename parameter. folder. When the parameter is set to NIL, the execution default operation "Open" is executed.
● FILENAME: Used to specify the file name to be opened, the program file name to be executed, or the folder name to be browsed.
● Parameters: If the filename parameter is an executable, this parameter specifies the command line parameter, otherwise this parameter should be NIL or PCHAR (0).
● Directory: Used to specify the default directory.
● 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 call is successful, the return value is an instance handle of the executed program. If the return value is less than 32, an error occurs.
The above is merely the standard usage of the shellexecute function, and the special usage of it will be described below.
2. Special usage
If they will
The filename parameter is set to
"Http:" protocol format, then this function will open the default browser and link to the specified URL address. If multiple browsers are installed in the user machine, the function will determine which browser is activated according to the settings of the HTTP Protocol Handler in the Windows 9x / NT Registry.
Format 1: http: // Website domain. Such as:
Shellexecute (handle, 'open', 'http://www.neu.edu.cn', nil, nil, sw_shownormal);
Format 2: http: // Website Domain Name / Web file name. Such as:
Shellexecute (Handle, 'Open', 'http://www.neu.edu.cn/default.htm' ,nil, nil, 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, or Netscape Messanger. If multiple mail clients are installed in the user machine, the function will determine which mail customer program will be activated according to the setting of the Mailto protocol handler in the Windows 9x / NT registry.
Format 1: Mailto:
Shellexecute (Handle, 'Open', 'Mailto:', NIL, NIL, SW_SHOWNORMAL);
Effect: Open the new mail window.
Format 2: Mailto: User Account @ Mail Server Address
Shellexecute (Handle, 'Open', 'Mailto: WHO@mail.neu.edu.cn', NIL, NIL, SW_SHOWNORMAL);
Effect: Open the new mail window and automatically fill in the recipient address. If you specify multiple recipient addresses, the recipient's address must be
Separate (below) with a semicolon or comma.
Format three: mailto: User account @ mail server address? Subject = mail topic & body = mail text,
Shellexecute (Handle, 'Open', '
Mailto: Who@mail.neu.edu.cn? Subject = Hello & Body = this is a test ', nil, nil, sw_shownormal);
Effect: Open the new mail window, and
Automatically fill in the recipient address, email topics, and mail text.
If the message body includes multi-line text, a wrap escape character% 0A must be added between each line of text.
Implementation
Create a Delphi project and add two tag controls in the main form HttPlabel and MailTolabel. Set the font.color property of the label control to CLBLUE, the font.style property is set to blank, the CURSOR property is set to CRHANDPOINT, and the CAPTION property is set to "Welcome to our website" and "give us a letter".
To the analog hyperlink effect, first, set the font.color property of the tag control to CLRED in the MouseMove event of the label control, and the Style is set to FsunderLine.
Procedure TFORM1.HTTPLABELMOUSEMOVE (Sender: Tobject; Shift: TshiftState; x, y: integer);
Begin
HTTPLABEL.FONT.COLOR: = CLRED;
HTTPLABEL.FONT.Style: = [fsunderline];
END;
Procedure TFORM1.MAILTOLABELMOMOVE (Sender: Tobject; Shift: TshiftState; x, y: integer);
Begin
Mailtolabel.font.color: = CLRED;
Mailtolabel.Font.Style: = [fsunderline];
END;
Then, set the font.color property of the label control to CLBLUE in the mousemove event of the main form, and the font.style property is set to blank so that the label control can restore the original display status when the mouse off the label control. Procedure TFORM1.FORMMMOMOVE (Sender: Tobject; Shift: TshiftState; x, y: integer);
Begin
HTTPLABEL.FONT.COLOR: = CLBLUE;
HttPlabel.Font.Style: = [];
Mailtolabel.font.color: = CLBLUE;
Mailtolabel.Font.Style: = [];
END;
Finally, add the website link or send the program code to the online link or send the mail.
Procedure TFORM1.HTTPLABELCLICK (Sender: TOBJECT);
Begin
IF shellexecute (handle,
'Open',
'Http://www.neu.edu.cn',
NIL,
NIL,
SW_SHOWNORMAL) <= 32 THEN
ShowMessage ('happens network error!');
END;
Procedure TFORM1.MAILTOLABELCLICK (Sender: TOBJECT);
Begin
IF shellexecute (handle,
'Open',
'Mailto: who@mail.neu.edu.cn ',
NIL,
NIL,
SW_SHOWNORMAL) <= 32 THEN
SHOWMESSAGE ('The error occurred in the error!');
END;
Note: Since the shellexecute function is declared in the shellapi.Pas unit, you must join the reference to the shellapi.pas unit:
Uses shellapi;
Of course, the reader can also try to make it into hyperlink control for use.