interacting with another application

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.
mjh2Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
amebaConnect With a Mentor Commented:
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
 
amebaCommented:
Do you know the title of the main form of that application?
0
 
amebaCommented:
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
 
mjh2Author Commented:
Adjusted points to 25
0
 
mjh2Author Commented:
Adjusted points to 80
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.