Return to an already open application when a user tries to open a new instance in vb6

后端 未结 2 1869
南笙
南笙 2021-01-15 04:35

Suppose a user minimize my visual basic application to the taskbar notification icon. Now I want when user open a new instance, the old one should restore.

2条回答
  •  抹茶落季
    2021-01-15 04:51

    You can often do this fairly simply using DDE in a degenerate way:

    Form1.frm

    Option Explicit
    'This is Form1.  To use as DDE source at design time we set:
    '   Form1.LinkMode = 1 (Source, i.e. vbLinkSource).
    '   Form1.LinkTopic = "Form1" (default).
    '
    'Note we use (hidden) Label1 on this Form as a DDE destination.
    
    Private PrevState As Integer
    
    Private Sub Form_LinkExecute(CmdStr As String, Cancel As Integer)
        'Got a "command" so restore Form1 and accept the command.
        WindowState = PrevState
        Caption = "I am awake!"
        Cancel = False
    End Sub
    
    Private Sub Form_Load()
        PrevState = WindowState
    End Sub
    
    Private Sub Form_Resize()
        If WindowState <> vbMinimized Then PrevState = WindowState
    End Sub
    

    Module1.bas

    Option Explicit
    
    Private Sub Main()
        Load Form1
        'After Form1 is loaded (hidden), try DDE link to possible prior copy.
        With Form1.Label1
            .LinkTopic = App.EXEName & "|Form1"
            On Error Resume Next
            .LinkMode = vbLinkManual
            If Err.Number = 0 Then
                On Error GoTo 0
                'Link succeeded.  Wake up prior copy via pushback to
                'the DDE source, then unload Form1 and terminate.
                .LinkExecute "Wake up!"
                Unload Form1
            Else
                On Error GoTo 0
                'Link failed, so we're 1st.  Show Form1.
                Form1.Show vbModal
            End If
        End With
    End Sub
    

提交回复
热议问题