Solved

interacting with another application

Posted on 1998-11-11
5
131 Views
Last Modified: 2010-04-30
I used a shell function to open a application.. I want to set a form's width to the same width of the application that I had openned. I have tried using the simple
     frmMain.width= screen.activeform.width, and this is no working.
0
Comment
Question by:mjh2
  • 3
  • 2
5 Comments
 
LVL 15

Expert Comment

by:ameba
ID: 1444384
Do you know the title of the main form of that application?
0
 
LVL 15

Expert Comment

by:ameba
ID: 1444385
In fact, you don't need Title. Can you add more points, and I will post nice code - "your form follows the application you shelled".
0
 

Author Comment

by:mjh2
ID: 1444386
Adjusted points to 25
0
 

Author Comment

by:mjh2
ID: 1444387
Adjusted points to 80
0
 
LVL 15

Accepted Solution

by:
ameba earned 80 total points
ID: 1444388
Start new project in vb, add command button and timer to your form and paste this code:

' Form which Shells and follows shelled window
' Created by Bruno Paris, email ameba@zg.tel.hr
Option Explicit
' API declarations
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40
Private Const SWP_NOMOVE = &H2
Private Const SWP_HIDEWINDOW = &H80
Private Const SWP_NOZORDER = &H4
Private Const GW_HWNDNEXT = 2
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Private 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
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
' private variables
Private m_useHwnd As Long ' window to follow

Private Sub Command1_Click()
    Dim ProcessID As Long, PID As Long
    On Error Resume Next
    ProcessID = Shell("Notepad", vbNormalFocus)
    If Err Then Exit Sub
    On Error GoTo 0
    m_useHwnd = FindWindow(vbNullString, vbNullString)
    Do Until m_useHwnd = 0
        If GetParent(m_useHwnd) = 0 Then
            Call GetWindowThreadProcessId(m_useHwnd, PID)
            If PID = ProcessID Then Exit Do
        End If
        m_useHwnd = GetWindow(m_useHwnd, GW_HWNDNEXT)
    Loop
    Timer1.Interval = 500
    Timer1.Enabled = True
End Sub

Private Sub Form_Load()
    Command1.Top = 30
    Height = 70 * Screen.TwipsPerPixelY
End Sub

Private Sub Timer1_Timer()
    On Error Resume Next
    If IsWindow(m_useHwnd) = 0 Then
        Timer1.Enabled = False
        Move 100, 100
        Beep
        AppActivate Me
        Exit Sub
    End If
    If IsIconic(m_useHwnd) Then
        WindowState = vbMinimized
    Else
        WindowState = vbNormal
    End If
    ' Get the rectangle describing the window
    Dim WindowRect As RECT
    GetWindowRect m_useHwnd, WindowRect
    ' position form
    Dim tmpLeft As Single, tmpTop As Single, tmpWidth As Single
    tmpLeft = WindowRect.Left * Screen.TwipsPerPixelX
    tmpTop = (WindowRect.Bottom - 6) * Screen.TwipsPerPixelY
    tmpWidth = (WindowRect.Right - WindowRect.Left) * Screen.TwipsPerPixelX
    If Left <> tmpLeft Or Top <> tmpTop Or Width <> tmpWidth Then
        Move tmpLeft, tmpTop, tmpWidth
        SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOSIZE Or SWP_NOMOVE
    End If
End Sub

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

11 Experts available now in Live!

Get 1:1 Help Now