Maximize an application from another aplication

We want avoid that users had two open instances at the same application. Therefore, we have a Windows API to know if an application is launched yet, but we dont know what API function using to Maximize a external application, Can anybody give me a tip about this?
dpinaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
watyConnect With a Mentor Commented:
Here is the way to avoid two instances :

If App.PrevInstance Then
   End
End If

0
 
watyCommented:
Here is a more complete function :

Function AnotherInstance() As Boolean
   ' *** This routine determines if the currently running program is already
   ' *** running (running twice).  It shifts the focus to the previous app
   ' *** and returns true.  It does NOT close the duplicated instance.

   Dim AppTitle        As String

   If App.PrevInstance Then
      AppTitle = App.Title
      App.Title = "No longer want this app running..."
      AppActivate AppTitle   ' Activate the previous instance
      AnotherInstance = True

   Else
      AnotherInstance = False
     
   End If

End Function

0
 
watyCommented:
You could also use the FindWindowLike (you should modify it a little) and then use the ShowWindow with the needed parameters.

Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

' *** Show window styles
Private Const SW_SHOWNORMAL = 1
Private Const SW_ERASE = &H4
Private Const SW_HIDE = 0
Private Const SW_INVALIDATE = &H2
Private Const SW_MAX = 10
Private Const SW_MAXIMIZE = 3
Private Const SW_MINIMIZE = 6
Private Const SW_NORMAL = 1
Private Const SW_OTHERUNZOOM = 4
Private Const SW_OTHERZOOM = 2
Private Const SW_PARENTCLOSING = 1
Private Const SW_RESTORE = 9
Private Const SW_PARENTOPENING = 3
Private Const SW_SHOW = 5
Private Const SW_SCROLLCHILDREN = &H1
Private Const SW_SHOWDEFAULT = 10
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMINNOACTIVE = 7
Private Const SW_SHOWNA = 8
Private Const SW_SHOWNOACTIVATE = 4


Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) 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 SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDLAST = 1
Private Const GW_HWNDNEXT = 2
Private Const GW_HWNDPREV = 3
Private Const GW_OWNER = 4
Private Const GW_CHILD = 5

Private Function FindWindowLike(ByVal hWndStart As Long, WindowText As String, ClassName As String) As Integer
   ' #VBIDEUtils#************************************************************
   ' * Programmer Name  : http://www.mvps.org/vbnet/index.html
   ' * Web Site         : http://www.mvps.org/vbnet/code/system/findwindowlikesimple.htm
   ' * Date             : 28/10/98
   ' * Time             : 10:12
   ' * Module Name      : Lib_Module
   ' * Module Filename  : Lib.bas
   ' * Procedure Name   : FindWindowLike
   ' * Parameters       :
   ' *                    ByVal hWndStart As Long
   ' *                    WindowText As String
   ' *                    Classname As String
   ' **********************************************************************
   ' * Comments         : How to Find Applications Matching a Specific Class or Window Title
   ' *
   ' *
   ' **********************************************************************


   Dim hwnd          As Long
   Dim sWindowText   As String
   Dim sClassName    As String
   Dim r             As Long

   'Hold the level of recursion and
   'hold the number of matching windows
   Static Level As Integer

   'Initialize if necessary. This is only executed when level = 0
   'and hWndStart = 0, normally only on the first call to the routine.
   If Level = 0 Then
      If hWndStart = 0 Then hWndStart = GetDesktopWindow()
   End If

   'Increase recursion counter
   Level = Level + 1

   'Get first child window
   hwnd = GetWindow(hWndStart, GW_CHILD)

   Do Until hwnd = 0

      'Search children by recursion
      r = FindWindowLike(hwnd, WindowText, ClassName)

      'Get the window text and class name
      sWindowText = Space$(255)
      r = GetWindowText(hwnd, sWindowText, 255)
      sWindowText = Left(sWindowText, r)

      sClassName = Space$(255)
      r = GetClassName(hwnd, sClassName, 255)
      sClassName = Left(sClassName, r)

      'Check if window found matches the search parameters
      If (sWindowText Like WindowText) And (sClassName Like ClassName) Then

         Debug.Print hwnd & vbTab & sClassName & vbTab & sWindowText
         FindWindowLike = hwnd

         'uncommenting the next line causes the routine to
         'only return the first matching window.
         ' Exit Do

      End If

      'Get next child window
      hwnd = GetWindow(hwnd, GW_HWNDNEXT)

   Loop

   'Reduce the recursion counter
   Level = Level - 1

End Function


0
 
dpinaAuthor Commented:
thanks waty, I think that I'll choose your option but would you mind giving me an little example of how is used ShowWindow?

thank you
0
 
watyCommented:
Try this :

call ShowWindow(Me.hwnd, SW_MAXIMIZE)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.