|
|
Title | Use DDE to make an editor where a single instance handles all requests |
Description | This example shows how to use DDE to make a single instance of an editor handle all requests in Visual Basic 6. It uses App.PrevInstance to see if it is the first instance running. If it is not the first instance, the program uses DDE to send any file names it was passed as command-line arguments to the running instance and then exits. |
Keywords | one instance, instance, DDE |
Categories | Software Engineering, Windows |
|
|
When it starts, the program uses App.PrevInstance to see if it is the first instance running. If it is not the first instance, the program uses DDE to send information to the previous instance. It then activates that instance and exits.
The LinkTopic = | where is the name of the project file (.vbp) and is the value set in the form's LinkTopic property.
|
|
' At design time:
' MDIForm.LinkMode = Source
' MDIForm.LinkTopic = DDEFileEditorMdiForm
' Save project as DDEFileEditor.vbp
Private Sub MDIForm_Load()
Dim cap As String
' See if there is a previous instance.
If App.PrevInstance Then
' DDE Destination.
' See if we have command-line arguments.
If Len(Command$) > 0 Then
' Clear any existing DDE link.
Text1.LinkMode = vbLinkNone
' Define link to source.
Text1.LinkTopic = _
"DDEFileEditor|" & _
"DDEFileEditorMdiForm"
' Link to Source.Text1.
Text1.LinkItem = "Text1"
' Establish manual link.
Text1.LinkMode = vbLinkManual
DoEvents
' Push the command-line arguments.
Text1.Text = Command$
Text1.LinkPoke
' Activate the running instance.
cap = Me.Caption
Me.Caption = ""
AppActivate cap
End If
' Exit.
Unload Me
Else
' DDE Source.
dlgFile.Flags = _
cdlOFNFileMustExist Or _
cdlOFNLongNames Or _
cdlOFNHideReadOnly
dlgFile.CancelError = True
dlgFile.Filter = "Text Files (*.txt)|*.txt|All " & _
"Files (*.*)|*.*"
dlgFile.InitDir = App.Path
' Process the command-line arguments.
Text1.Text = Command$
End If
End Sub
|
|
The MDI form's Text1 control's text changes when the first instance placed its command-line arguments in that control and when a later instance sent the data to the control via DDE. When the text changes, the Text1_Change event handler first veifies that this is not a later instance (there's no point loading the files in later instances because they exit) and that the text isn't blank.
The command line parameters are short file names separated by spaces. The code uses the Split statement to separate the file names. For each file name, it makes a new frmEditor form, sets the form's caption to the file's long name, displays the file's contents in the form, and shows the form.
|
|
Private Sub Text1_Change()
Dim files() As String
Dim i As Integer
Dim frm As frmEditor
If App.PrevInstance Then Exit Sub
If Len(Text1.Text) = 0 Then Exit Sub
files = Split(Text1.Text)
Text1.Text = ""
For i = LBound(files) To UBound(files)
Set frm = New frmEditor
frm.Caption = "[" & LongFileName(files(i)) & "]"
frm.Text1 = GrabFile(files(i))
frm.Show
Next i
End Sub
|
|
|
|
|
|