![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
Listing 9.3 Code in COPYLINK.FRM. Option Explicit Private Sub Command1_Click() Clear the Picture Box. Picture1.picture = LoadPicture() End Sub Private Sub Command2_Click() Quit the program. End End Sub Private Sub mnuEdit_Click() Enable the Copy command only if the Picture Box or the Text Box is active. If TypeOf Screen.ActiveControl Is TextBox Or TypeOf Screen.ActiveControl _ Is PictureBox Then mnuEditCopy.Enabled = True Else mnuEditCopy.Enabled = False End If End Sub Private Sub mnuEditCopy_Click() Dim LinkInfo As String Clear the Clipboard. Clipboard.Clear If the active control is the Text Box copy its text and DDE information to the Clipboard. If TypeOf Screen.ActiveControl Is TextBox Then LinkInfo = App.EXEName & | & LinkTopic LinkInfo = LinkInfo & ! & Screen.ActiveControl.Tag Clipboard.SetText LinkInfo, vbCFLink Clipboard.SetText Screen.ActiveControl.TEXT End If If the active control is a Picture Box copy its picture and DDE information to the Clipboard. If TypeOf Screen.ActiveControl Is PictureBox Then LinkInfo = App.EXEName & | & LinkTopic LinkInfo = LinkInfo & ! & Screen.ActiveControl.Tag Clipboard.SetText LinkInfo, vbCFLink Clipboard.SetData Screen.ActiveControl.picture End If End Sub More DDE MagicDDE offers a great deal more capability than is possible to cover here. However, I would like to finish this chapter with one more use for DDEsomething I find very helpful. Using the power of DDE, we can employ Visual Basic to create extensions to existing programs, circumventing omissions or shortcomings in their design and capabilities. For example, we may like to use the Microsoft Excel spreadsheet program for manipulation and graphing of numerical data. And why not? Excel has a wide range of powerful features, and trying to duplicate even a few of them in a Visual Basic program would be a major programming challenge. In my opinion, however, Excel falls short in one area: data entry. Typing large amounts of data into the spreadsheets row-and-column structure can be an exercise in frustration and error. As you know, however, Visual Basic makes it easy to create attractive, easy-to-use forms for data entry and other purposes, and also to include data validation code to prevent entry of incorrect data. By using DDE, we can create a Visual Basic data-entry program that sends the data to an Excel spreadsheet. We get the best of both worlds: the convenience of a Visual Basic data-entry form and the analytical capabilities of Excel. Using DDE in this way is quite different from the uses we covered earlier in the chapter. DDE is used to transfer data, but no link is established. Once a piece of data is sent to Excel, we have no link back to the Visual Basic programthe end result is exactly the same as if the data had been typed directly into Excel. In addition to sending data to other programs, DDE can be used to send commands, instructing programs to perform various actions. Youll see that several actions are required for this sort of application. The first action we will need is the ability to start a program, if it is not already running. To start another Windows application from within a Visual Basic program, you use the Shell function. The syntax is shown here: result = Shell(CommandString [, WindowStyle]) The CommandString argument specifies the name of the program to execute, including any required arguments or command-line switches. If the program name in CommandString doesnt include a .COM, .EXE, .BAT, or .PIF file extension, .EXE is assumed. The WindowStyle argument specifies the style of the application window when the program starts, such as maximized or minimized. We can omit this argument, and the program is opened, minimized with focus. Table 9.2 shows the possible values for WindowStyle and the defined constants. If the application is successfully initiated, the Shell function returns the applications task identification, a unique number that identifies the running application. If the Shell function is unable to start the named application, an error occurs.
When using the Shell function, remember that it runs other applications asynchronouslythe Visual Basic program does not wait for the Shell command to complete. This means that we cant be sure that a program started with Shell will have completed its start-up procedures before the code following the Shell function in our Visual Basic application is executed. Sending Commands To Another ProgramOnce you have established a DDE link, you can use the link for more than transferring data. You can also use it to send commands to the other application using the LinkExecute method. This method is applied to the control that is maintaining the linkthe active destination control. Thats just a syntax requirement, however, as the control has nothing to do with the command we are sending. The syntax for the LinkExecute method is shown here: ControlName.LinkExecute Command ControlName is the name of a control that is the destination in an active DDE link. The Command argument is a string that contains the commands to be sent to the DDE source application. The legal commands depend on the specific application. In other words, no universal DDE command set exists that contains commands for use with all applications. Youll need to refer to the documentation for each source application for information on the commands it accepts. Excel, for example, accepts any of its macro commands enclosed in brackets. Sending Data To Another ProgramOnce an active DDE link is established, you can send data from the destination program to the source program. Note that this is the reverse of the more typical data flow in a DDE conversation, which is from source to destination. To send data from the destination to the source, use the LinkPoke method. The procedure is as follows:
For example, the following code will poke the text Testing LinkPoke to cell B2 in the Excel spreadsheet TESTDATA.XLS: Text1.Text = Testing LinkPoke Text1.LinkMode = 0 Text1.LinkTopic = EXCEL|TESTDATA Text1.LinkItem = R2C2 Text1.LinkMode = 2 Text1.LinkPoke Note the use of R2C2 to refer to the second row in the second column of the Excel spreadsheet. For DDE commands, Excel uses numbers for both rows and columns rather than using letters for columns, as is the case when you are working directly in Excel.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |