Results 1 to 4 of 4

Thread: open outside application in form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Pittsburgh, PA
    Posts
    329
    i need to open an outside application inside of a form.
    this way my user can use both applications at once without having to switch between two windows.
    how do i do this?

    thanks in advance


  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    You need a picture box inside your form, also I can't remember how to shell an app from your own programm but it's pretty easy (I think there's a vb shell command) and you need to get it's hWnd. once youve done that

    Code:
    Private Const WS_MAXIMIZE = &H1000000
    Private Const GWL_STYLE = (-16)
    
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    
    Private Sub WindowToPicturebox(WindowhWnd As Long, PictureboxhWnd As Long)
    
    SetWindowlong WindowhWnd,GWL_STYLE, GetWindowLong(WindowhWnd,GWL_STYLE) Or WS_MAXIMIZE
    
    SetParent WindowhWnd, PictureboxhWnd
    
    End Sub
    hope this helps.

  3. #3
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141
    Sam,

    I am also very interested in figuring out how to do this
    (especially how to run a DOS command prompt window in an app).

    I've been playing with the code you posted, but I'm unable
    to get anything to work. Can you give any more pointers on
    how this works? Specifically, How to get the hWnd value
    from a shell command and How the WindowToPicturebox sub in
    the code you provided is called. It would be greatly
    appreciated.

    By the way, here are some examples of shell commands. So
    you don't have to look them up.


    Code:
    'Start the Calculator Program
    retVal = Shell("C:\Windows\CALC.EXE", vbNormalFocus)
    'Start a command prompt from NT
    retVal = Shell("C:\Winnt\system32\command.com", vbNormalFocus)
    [Edited by jcouture100 on 04-12-2000 at 10:42 PM]
    JC

  4. #4
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Right I've done it, It took a while cos I was looking for a better method than this to get the hWnd of the new shell.

    You need a form with a picture box, a command button and a timer with disabled = False and interval = 100 (ish, try a bit less if it works, a bit more if it doesn't)

    Put this code in the form

    [CODE]

    Option Explicit

    Private Const SW_NORMAL = 1
    Private Const GWL_STYLE = (-16)
    Private Const WS_CAPTION = &HC00000
    Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type
    Private Type WINDOWPLACEMENT
    Length As Long
    flags As Long
    showCmd As Long
    rcMinandMaxPositions As RECT
    rcNormalPosition As RECT
    End Type

    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
    Private Declare Function EnumDesktopWindows Lib "user32" Alias "EnumChildWindows" (ByVal hDesktop As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function SetWindowPlacement Lib "user32" (ByVal hWnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
    Private Declare Function GetWindowPlacement Lib "user32" (ByVal hWnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
    Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
    Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As Long) As Long


    Private Sub Command1_Click()
    Reset
    EnumDesktopWindows GetDesktopWindow, AddressOf enumWindowproc, 1
    Shell "C:\Winnt\system32\command.com", vbMinimizedNoFocus

    Timer1.Enabled = True
    End Sub


    Private Sub Timer1_Timer()
    Dim hWnd As Long
    Dim str As String
    Dim l As Long
    Dim apiWindowPlacement As WINDOWPLACEMENT

    EnumDesktopWindows GetDesktopWindow, AddressOf enumWindowproc, 2

    Timer1.Enabled = False

    SetParent hWndNewShellWindow, Picture1.hWnd

    GetWindowPlacement hWndNewShellWindow, apiWindowPlacement

    apiWindowPlacement.rcNormalPosition = CalculateNewWindowRect(Picture1.hWnd, hWndNewShellWindow)

    SetWindowPlacement hWndNewShellWindow, apiWindowPlacement

    apiWindowPlacement.showCmd = SW_NORMAL
    End Sub

    Private Function CalculateNewWindowRect(ParentWindow As Long, ChildWindow As Long) As RECT
    Dim rcTargetRect As RECT 'Rectangle of window we're going to put it in
    Dim rcLarge As RECT 'Rectangle of window including border
    Dim rcSmall As RECT 'Rectangle of window excluding border

    GetClientRect ParentWindow, rcTargetRect
    GetWindowRect ChildWindow, rcLarge
    GetClientRect ChildWindow, rcSmall

    'translate window rect into client coords
    ScreenToClient ChildWindow, rcLarge.Left
    ScreenToClient ChildWindow, rcLarge.Right

    CalculateNewWindowRect.Left = rcTargetRect.Left + rcLarge.Left - rcSmall.Left
    CalculateNewWindowRect.Top = rcTargetRect.Top + rcLarge.Top - rcSmall.Top
    CalculateNewWindowRect.Bottom = rcTargetRect.Bottom + rcLarge.Bottom - rcSmall.Bottom
    CalculateNewWindowRect.Right = rcTargetRect.Right + rcLarge.Right - rcSmall.Right

    End Function

    [/Code}

    and you need a standard module with this code

    Code:
    Option Explicit
    Dim collWindows As New Collection
    Public hWndNewShellWindow As Long
    Public Sub Reset()
    hWndNewShellWindow = 0
    Set collWindows = New Collection
    
    End Sub
    Public Function enumWindowproc(ByVal hWnd As Long, ByVal lParam As Long) As Long
    
    
    Select Case lParam
    
        Case 1
        
        collWindows.Add False, CStr(hWnd)
        enumWindowproc = True
        Case 2
        'Debug.Print hwnd
        On Error Resume Next
        If collWindows.Item(CStr(hWnd)) Then
        hWndNewShellWindow = hWnd
        enumWindowproc = False
        Else
        enumWindowproc = True
        End If
    End Select
    End Function
    then just hit the command button and it should open a new dos prompt in the picture box.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width