A Better PrevInstance Example: Can't get it to work 
Author Message
 A Better PrevInstance Example: Can't get it to work

I downloaded the "A Better PrevInstance" example from
http://www.*-*-*.com/

The .exe included works great.  When I compile there example on my
system it either does nothing, meaning the oringal instance stays in
the task bar. Or, I get a runtime error.  I also added their
modprevinst.bas to my current project and their code from their form
and had the same results in my project.  What am I doing wrong?

Code on their form:

Private Sub Form_Load()
'-----------------------------------------------------------
' Adjusted a PlanetSourceCode.com example
' of how not to allow any previnstances or aliases
' to add the ability to bring a prev instance to the front
' and restore if it was minimized
' -Robert Smith 11/98
'-----------------------------------------------------------

Dim temphwnd As Long, x As Long, AppTitle As String
If App.PrevInstance Or FindWindow("ThunderRT5Form", App.Title) > 0
Then
  'get the handle to the window
  'for non-mdi forms use:
  temphwnd = FindWindow("ThunderRT5Form", App.Title)

  'for MDI forms use:
  'temphwnd = FindWindow("ThunderRT5MDIForm", App.Title)

  'Here's the secret!!!
  AppTitle = App.Title
  App.Title = "#$#"

  Caption = "#$#"

  'activate the previous instance at the same
  'place and size it was before
  'could do to non-asyc with ShowWindow but that
  'lead to problems when working outside the current thread
  x = ShowWindowAsync(temphwnd, SW_RESTORE)

  'bring it to the top of the z-order
  SetWindowPos temphwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or
SWP_NOSIZE

  'set focus to it
  AppActivate AppTitle

  'now release it's lock on the z-order
  'or other windows wont be able to show correctly
  'remove this, run the app and watch how the OS tooltips
  'for things like the windows min/max/close buttons
  'appear behind the window, hence the release call
  SetWindowPos temphwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or
SWP_NOSIZE

  End
End If

Caption = App.Title
Show

End Sub

Their .bas

Option Explicit

Public Const SW_NORMAL = 1
Public Const SW_MAXIMIZE = 3
Public Const SW_RESTORE = 9
Public Const HWND_TOP = 0
Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1

Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal
nCmdShow As Long) As Long
Declare Function ShowWindowAsync Lib "user32" (ByVal hwnd As Long,
ByVal nCmdShow As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal
hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As
Long, ByVal cy As Long, ByVal wFlags As Long) As Long



Fri, 06 Aug 2004 08:29:41 GMT  
 A Better PrevInstance Example: Can't get it to work
Yogi

You'll find what you need on my Tips & Tricks | Code Library | VB(A) Code
page

regards

Ian Williams

** invalid reply address, change dk to denmark

Tips & Tricks - http://tips.kingsoft-denmark.com


Quote:
> I downloaded the "A Better PrevInstance" example from
> http://www.vb-world.net/beginning/tip125.html

> The .exe included works great.  When I compile there example on my
> system it either does nothing, meaning the oringal instance stays in
> the task bar. Or, I get a runtime error.  I also added their
> modprevinst.bas to my current project and their code from their form
> and had the same results in my project.  What am I doing wrong?

> Code on their form:

> Private Sub Form_Load()
> '-----------------------------------------------------------
> ' Adjusted a PlanetSourceCode.com example
> ' of how not to allow any previnstances or aliases
> ' to add the ability to bring a prev instance to the front
> ' and restore if it was minimized
> ' -Robert Smith 11/98
> '-----------------------------------------------------------

> Dim temphwnd As Long, x As Long, AppTitle As String
> If App.PrevInstance Or FindWindow("ThunderRT5Form", App.Title) > 0
> Then
>   'get the handle to the window
>   'for non-mdi forms use:
>   temphwnd = FindWindow("ThunderRT5Form", App.Title)

>   'for MDI forms use:
>   'temphwnd = FindWindow("ThunderRT5MDIForm", App.Title)

>   'Here's the secret!!!
>   AppTitle = App.Title
>   App.Title = "#$#"

>   Caption = "#$#"

>   'activate the previous instance at the same
>   'place and size it was before
>   'could do to non-asyc with ShowWindow but that
>   'lead to problems when working outside the current thread
>   x = ShowWindowAsync(temphwnd, SW_RESTORE)

>   'bring it to the top of the z-order
>   SetWindowPos temphwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or
> SWP_NOSIZE

>   'set focus to it
>   AppActivate AppTitle

>   'now release it's lock on the z-order
>   'or other windows wont be able to show correctly
>   'remove this, run the app and watch how the OS tooltips
>   'for things like the windows min/max/close buttons
>   'appear behind the window, hence the release call
>   SetWindowPos temphwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or
> SWP_NOSIZE

>   End
> End If

> Caption = App.Title
> Show

> End Sub

> Their .bas

> Option Explicit

> Public Const SW_NORMAL = 1
> Public Const SW_MAXIMIZE = 3
> Public Const SW_RESTORE = 9
> Public Const HWND_TOP = 0
> Public Const HWND_TOPMOST = -1
> Public Const HWND_NOTOPMOST = -2
> Public Const SWP_NOMOVE = &H2
> Public Const SWP_NOSIZE = &H1

> Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal
> nCmdShow As Long) As Long
> Declare Function ShowWindowAsync Lib "user32" (ByVal hwnd As Long,
> ByVal nCmdShow As Long) As Long
> Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
> lpClassName As String, ByVal lpWindowName As String) As Long
> Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal
> hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As
> Long, ByVal cy As Long, ByVal wFlags As Long) As Long



Fri, 06 Aug 2004 13:37:30 GMT  
 A Better PrevInstance Example: Can't get it to work
The example is in VB5

Quote:
>FindWindow("ThunderRT5Form", App.Title)

My guess is that you are running VB6

"ThunderRT5Form" is the Class Name of a VB5 Form

Try This (snip it and save it as Form1.frm) :-

VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
   Begin VB.TextBox txtSend
      Enabled         =   0   'False
      Height          =   285
      Left            =   1440
      TabIndex        =   2
      Text            =   "Text to Send"
      Top             =   1500
      Width           =   1335
   End
   Begin VB.TextBox txtTarget
      Height          =   285
      Left            =   1440
      TabIndex        =   1
      Text            =   "txtTarget"
      Top             =   1080
      Width           =   1335
   End
   Begin VB.CommandButton cmdSend
      Caption         =   "Send"
      Enabled         =   0   'False
      Height          =   375
      Left            =   1440
      TabIndex        =   0
      Top             =   1860
      Width           =   1335
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

' 29/07/2001  J French ISS Ltd
'
' Demonstration of communication between Apps
'
' Method: Send data from one App to Textbox
'         in another App
'
' Inspired by: Norstrom - developing Chat System
'
' Add 1 Commandbutton : cmdSend
'     1 Textbox       : txtSend
'     1 Textbox       : txtTarget
'
' NOTE:
'    txtTarget - MUST contain 'txtTarget' at design time
'
' Compile and run
'
' THIS DEMO ONLY WORKS WHEN THE APP IS COMPILED
'
' You can modify it to run
'    (slightly differently) in the IDE

Private Declare Function SendMessageStr _
        Lib "user32" _
        Alias "SendMessageA" _
        (ByVal hwnd As Long, _
         ByVal wMsg As Long, _
         ByVal wParam As Long, _
         ByVal S As String) _
        As Long

Private Declare Function FindWindowEx _
        Lib "user32" _
        Alias "FindWindowExA" _
        (ByVal hWnd1 As Long, _
         ByVal hWnd2 As Long, _
         ByVal lpsz1 As String, _
         ByVal lpsz2 As String) _
        As Long

Private Declare Function GetClassName _
        Lib "user32" _
        Alias "GetClassNameA" _
        (ByVal hwnd As Long, _
         ByVal lpClassName As String, _
         ByVal nMaxCount As Long) _
        As Long

Private Declare Function GetWindowText _
        Lib "user32" _
        Alias "GetWindowTextA" _
        (ByVal hwnd As Long, _
         ByVal lpString As String, _
         ByVal cch As Long) As Long

Private Const WM_SETTEXT = &HC

Private hTgt&

Private Sub Form_Load()
    Dim S$

    Me.Top = 0
    Me.AutoRedraw = True
    Me.Caption = "TARGET"
    cmdSend.Enabled = False
    txtSend.Enabled = False

    If App.PrevInstance Then
       Me.Caption = "SOURCE"
       Call LS_FindTarget
       cmdSend.Enabled = True
       txtSend.Enabled = True
       txtTarget.Enabled = False
       Me.Left = Screen.Width - Me.Width
    End If

    If App.PrevInstance = 0 Then
       Me.Left = 0
       S$ = App.Path
       If Right$(S$, 1) <> "\" Then
          S$ = S$ + "\"
       End If
       Shell S$ + App.EXEName, vbNormalFocus
    End If
End Sub

Private Sub cmdSend_Click()
    Static Count&
    Count = Count + 1
    LS_Send "Msg" + Str$(Count) + ":" + txtSend.Text
End Sub

' --- Locate TARGET App and Textbox
Private Sub LS_FindTarget()
    Dim hApp&

    ' --- locate Top Window with Caption "TARGET"
    hApp = FindWindowEx(0, 0, vbNullString, "TARGET")
    Me.Print "hApp:"; hApp
    Me.Print LF_ClassName(hApp); " / "; LF_WindowText(hApp)
    ' --- locate the Textbox containing 'txtTarget' at design time
    hTgt = FindWindowEx(hApp, 0, vbNullString, "txtTarget")
    Me.Print "hTgt:"; hTgt; " / "; LF_WindowText(hTgt)
    Me.Print LF_ClassName(hTgt)
End Sub

' --- actually push data to the TARGET instance
Private Sub LS_Send(S$)
    Call SendMessageStr(hTgt, WM_SETTEXT, 0, S$)
End Sub

' --- prove that we activate the TARGET App
Private Sub txtTarget_Change()
    If Me.CurrentY > Me.ScaleHeight - 250 Then Me.Cls
    Me.Print txtTarget.Text
End Sub

Private Function LF_ClassName(Hnd&) As String
    Dim Q&, S$
    S$ = Space$(100)
    ' ---
    Q = GetClassName(Hnd, S$, Len(S$))
    LF_ClassName = Left$(S$, Q)
End Function

Private Function LF_WindowText(Hnd&) As String
    Dim Q&, S$
    S$ = Space$(100)
    ' ---
    Q = GetWindowText(Hnd, S$, Len(S$))
    LF_WindowText = Left$(S$, Q)
End Function



Fri, 06 Aug 2004 17:10:02 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. PrevInstance Doesn't work !!!

2. PrevInstance Doesn't work !!!

3. 'Canned' data in VB program

4. TSISoon - can't get simple example to work on XP

5. ANOTHER help example doesn't work

6. Microsoft example doesn't work

7. Process.Start - example doesn't work

8. Microsoft's tooltip example does not work?

9. Microsoft example doesn't work...

10. dotnetfx.exe bootstrapper example setup.exe doesn't work

11. DAO Seek Example won't work

12. DragIcon Property Problem /VB Example Doesn't work

 

 
Powered by phpBB® Forum Software