Solved

Maximize an application from another aplication

Posted on 1998-11-04
5
225 Views
Last Modified: 2010-08-05
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?
0
Comment
Question by:dpina
  • 4
5 Comments
 
LVL 14

Accepted Solution

by:
waty earned 300 total points
ID: 1443080
Here is the way to avoid two instances :

If App.PrevInstance Then
   End
End If

0
 
LVL 14

Expert Comment

by:waty
ID: 1443081
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
 
LVL 14

Expert Comment

by:waty
ID: 1443082
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
 

Author Comment

by:dpina
ID: 1443083
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
 
LVL 14

Expert Comment

by:waty
ID: 1443084
Try this :

call ShowWindow(Me.hwnd, SW_MAXIMIZE)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now