






|
VB Mania |

|
How to Open a link or a Window |
|
Information: In your program, you may need to put a link to website so the user can get access to. |
|
Illustration:
|
|
What You Need?: 1. One form named form1 2. Two Labels: · The first named lblLink1 (caption: any web address, e.g. www.gmail.com) · The second named lblLink2 (caption: any FULL-folder path, e.g. c:\) |
|
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Sub lblLink1_Click() ShellExecute 0&, vbNullString, lblLink1.caption, vbNullString, vbNullString, vbNormalFocus End sub
Private Sub lblLink2_Click() ShellExecute 0&, vbNullString, lblLink2.caption, vbNullString, vbNullString, vbNormalFocus End sub |
|
Code:
|
|
Explanation: Clicking on lblLink1 will evoke shellexecute to open the content of link1.caption as its lpFile parameter. Being a web address as link1.caption, this will direct shellexecute to open the Internet Explorer client into the specific address. Notice the use of vbnullstring as the lpOperation parameter. Whereas in lblLink2, shellexecute will find that lpfile is a directory path, so it will launch a shell's window of the specific path. |
|
Hint: For Other uses of the great shellexecute, replace the following codes under Private Sub lblLink2_Click()
To explore a folder, use: ShellExecute 0&, "explore", lblLink2.caption, vbNullString, vbNullString, vbNormalFocus
To launch the Shell's Find utility for a directory, use: ShellExecute 0&, "find", lblLink2.caption, vbNullString, vbNullString, vbNormalFocus
To Prints the document file specified by lblLink2.caption, use: ShellExecute 0&, "print", lblLink2.caption, vbNullString, vbNullString, vbNormalFocus
(NOTE: If lpFile indicated by lblLink2.caption is not a document file, the function will fail) |
|
VB-Pedia: · Form: a member of VB Library which is a window or dialog box that makes up part of an application's user interface.
· Label: a member of VB Library which displays text that a user can't change directly.
· ShellExecute: a private function of the windows Library "shell32.dll". It performs an operation on a specified file. Parameters ¨ hwnd Handle to the owner window used for displaying a user interface (UI) or error messages. This value can be NULL if the operation is not associated with a window. ¨ lpOperation Pointer to a null-terminated string, referred to in this case as a verb, that specifies the action to be performed. The set of available verbs depends on the particular file or folder. Generally, the actions available from an object's shortcut menu are available verbs. ¨ lpFile Pointer to a null-terminated string that specifies the file or object on which to execute the specified verb. To specify a Shell namespace object, pass the fully qualified parse name. ¨ lpParameters If the lpFile parameter specifies an executable file, lpParameters is a pointer to a null-terminated string that specifies the parameters to be passed to the application. The format of this string is determined by the verb that is to be invoked. ¨ lpDirectory Pointer to a null-terminated string that specifies the default directory. ¨ nShowCmd Flags that specify how an application is to be displayed when it is opened. * SW_SHOW Activates the window and displays it in its current size and position. |

|
Sponsored Links |