by Kent Reisdorph
You can download sample files from our Web site as part of the file dec98.zip. Visit www.zdjournals.com/cpb and click on the Source Code hyperlink.
A Web site is a great way to promote your company. If you have a Web site, you want your users to be able to get to it with the minimum amount of effort. A URL label in your application's About dialog box allows your users to quickly and easily connect to your Web site from your application. In this article, we'll show you how you can create a URL label.
| The label's caption is a URL. | |
| The URL appears in blue text, to indicate that it can be clicked. | |
| Your cursor changes to a pointing hand when it moves over the label. | |
| When you click on the label, it spawns a Web browser. |
Figure A: Users can click on a URL label to quickly go to a Web site.
Most of the steps required to fulfill this feature list are easy to perform--you certainly don't need any help to drop a label on a form, change the Caption property, and change the font's color. The rest, though, requires some explanation.
Using Image Editor, create a resource file (RES) and then create a cursor resource within that resource file. Name the cursor resource something appropriate (WEBPOINTER would be a good name). Save the resource file and close Image Editor. Now, choose C++Builder's Project | Add To Project menu item and add the resource file to your project.
Next, you need to load the cursor resource so that it's available to the URL label. Put this code in your main form's OnCreate event handler:
Screen->Cursors[1] = LoadCursor(HInstance, "WEBPOINTER");The cursor will be loaded when the application starts and will be ready for you to use. Notice that the code loads the cursor into array index 1 of the Cursors array. To assign the new cursor to the URL label, simply change the label's Cursor property to 1. You can do this either at design time or at runtime.
#include <ShellApi.h>Now that you've included the header, you can call ShellExecute. Here's the code:
ShellExecute(0, "open", "http://www.turbopower.com", "", "", SW_SHOWNORMAL);ShellExecute will open a document based on the document's extension. In order for this function to work, the filename extension must be registered with Windows. A browser document is a special case: Windows recognizes the document name as a URL, automatically executes the default Web browser, and loads the document. If ShellExecute fails, it returns a value of 32 or fewer. You should write your code to account for an error. For example:
int result =(int)ShellExecute(0, "open",
"http://www.turbopower.com", "", "",
SW_SHOWNORMAL);
if (result <= 32)
ShowMessage("Unable to start web browser.");
All
you have to do now is add this code to the OnClick event handler for the
URL label. Double-click on the label, and C++Builder will create an event
handler for the OnClick event. Enter code similar to the above code
snippet, and you're all set.
int result =(int)ShellExecute(0, "open",
"mailto:freddy@kreuger.com", "", "",
SW_SHOWNORMAL);
if (result <= 32)
ShowMessage(
"Unable to start mail client.");
Notice
that the document string in this example includes mailto: rather than a
URL. When this code executes, Windows will start the mail client registered on
the user's system and fill in the To field with the E-mail address passed in
ShellExecute.