Solved

vb.net embedding notepad or any other program to a form

Posted on 2004-08-31
12
1,469 Views
Last Modified: 2009-10-11
I am using vb.net and I want to have the programs embedded into a form.
In vb6 i use to use

     Dim ret As Long
    ret = FindWindow("Opusapp", vbNullString)
    SetParent ret, form1.hwnd
    form1.Show
 

I tried
 Dim myhandle As IntPtr
        Shell("c:\windows\notepad.exe", AppWinStyle.NormalFocus, True)
        myhandle = FindWindow("Untitled - Notepad", vbNullString)
        Dim form1 As New Form1
        SetParent(myhandle, form1.Handle)
        form1.Show()


it opens notepad but not inside form1

what can I do....
0
Comment
Question by:hogan9111
  • 4
  • 4
12 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
0
 

Author Comment

by:hogan9111
Comment Utility
too confussing, don't know what to do in that forum....
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
have you tried the code from mcsd2001 that has been accepted as the answer?
0
 

Author Comment

by:hogan9111
Comment Utility
yes I didn't know where to copy and paste it. And I tried it but got errors.....

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 69

Accepted Solution

by:
Éric Moreau earned 50 total points
Comment Utility
-Create a new project (Windows application template).
-Add a class to your project.
-Delete the code inserted into the new class.
-Copy this code into the class.

Option Strict On

Imports System.Runtime.InteropServices

Public Class ExtProcess
    Inherits System.Diagnostics.Process

    <DllImport("user32.dll")> _
    Private Shared Function GetClassName(ByVal hwnd As Integer, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function IsWindowVisible(ByVal hwnd As Integer) As Boolean
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function SetForegroundWindow(ByVal hwnd As Integer) As Integer
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function GetWindowThreadProcessId(ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function GetWindow(ByVal hwnd As Integer, ByVal wFlag As Integer) As Integer
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function SetParent(ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function GetWindowLong(ByVal hwnd As Integer, ByVal nIndex As Integer) As Integer
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function SetWindowLong(ByVal hwnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function MoveWindow(ByVal hwnd As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Integer) As Integer
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function PostMessage(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function SetWindowText(ByVal hwnd As Integer, ByVal lpString As String) As Integer
    End Function

    Private Const GWL_STYLE As Int32 = (-16)
    Private Const WS_MAXIMIZEBOX As Int32 = &H10000
    Private Const WS_MINIMIZEBOX As Int32 = &H20000
    Private Const WS_DLGFRAME As Int32 = &H400000
    Private Const WS_SYSMENU As Int32 = &H80000
    Private Const WS_CHILD As Int32 = &H40000000
    Private Const WS_CHILDWINDOW As Int32 = (WS_CHILD)
    Private Const WS_VSCROLL As Int32 = &H200000
    Private Const WS_CAPTION As Int32 = &HC00000
    Private Const WS_BORDER As Int32 = &H800000
    Private Const WS_THICKFRAME As Int32 = &H40000
    Private Const WS_SIZEBOX As Int32 = WS_THICKFRAME
    Private Const SW_MAXIMIZE As Int32 = 3
    Private Const GW_HWNDNEXT As Int32 = 2
    Private Const SW_HIDE As Int32 = 0
    Private Const SW_MINIMIZE As Int32 = 6
    Private Const SW_NORMAL As Int32 = 1
    Private Const SW_RESTORE As Int32 = 9
    Private Const WM_CLOSE As Int32 = &H10

    Private m_lngWindowStyle As System.Diagnostics.ProcessWindowStyle
    Private m_lngHandle As Integer
    Private m_blnPromptSave As Boolean
    Private m_strWindowCaption As String
    Private m_blnShowCaption As Boolean
    Private m_blnShowButtons As Boolean
    Private WithEvents m_objParentObject As Control

    Public Property ParentControl() As Control
        Get
            Return m_objParentObject
        End Get
        Set(ByVal Value As Control)
            m_objParentObject = Value
        End Set
    End Property

    Public Property ShowCaption() As Boolean
        Get
            Return m_blnShowCaption
        End Get
        Set(ByVal Value As Boolean)
            m_blnShowCaption = Value
        End Set
    End Property

    Public Property ShowButtons() As Boolean
        Get
            Return m_blnShowButtons
        End Get
        Set(ByVal Value As Boolean)
            m_blnShowButtons = Value
        End Set
    End Property

    Public Property WindowCaption() As String
        Get
            Return m_strWindowCaption
        End Get
        Set(ByVal Value As String)
            m_strWindowCaption = Value
            Call SetWindowText(m_lngHandle, m_strWindowCaption)
        End Set
    End Property

    Public Property SaveOnExit() As Boolean
        Get
            Return m_blnPromptSave
        End Get
        Set(ByVal Value As Boolean)
            m_blnPromptSave = Value
        End Set
    End Property

    Public ReadOnly Property AppHandle() As Integer
        Get
            Return m_lngHandle
        End Get
    End Property

    Public Sub New(ByVal FileName As String)
        Me.StartInfo.FileName = FileName
    End Sub

    Public Sub New(ByVal FileName As String, ByVal Style As System.Diagnostics.ProcessWindowStyle)
        Me.StartInfo.FileName = FileName
        Me.StartInfo.WindowStyle = Style
        Call StartApplication(Style)
    End Sub

    Public Overloads Sub Start(ByVal Style As System.Diagnostics.ProcessWindowStyle)
        Call StartApplication(Style)
    End Sub

    Public Overloads Sub Start(ByVal FileName As String, ByVal Style As System.Diagnostics.ProcessWindowStyle)
        Me.StartInfo.FileName = FileName
        Me.StartInfo.WindowStyle = Style
        Call StartApplication(Style)
    End Sub

    Public Sub ShowApplication(Optional ByVal Style As System.Diagnostics.ProcessWindowStyle = ProcessWindowStyle.Normal)
        If Not IsWindowVisible(Me.AppHandle) Then
            Select Case Style
                Case ProcessWindowStyle.Normal
                    ShowWindow(Me.AppHandle, SW_NORMAL)
                Case ProcessWindowStyle.Maximized
                    ShowWindow(Me.AppHandle, SW_MAXIMIZE)
                Case ProcessWindowStyle.Minimized
                    ShowWindow(Me.AppHandle, SW_MINIMIZE)
                Case ProcessWindowStyle.Hidden
                    ShowWindow(Me.AppHandle, SW_HIDE)
            End Select
        End If

        SetForegroundWindow(Me.AppHandle)
    End Sub

    Public Sub HideApplication()
        If IsWindowVisible(Me.AppHandle) Then
            ShowWindow(Me.AppHandle, SW_HIDE)
        End If
    End Sub

    Private Sub StartApplication(ByVal Style As System.Diagnostics.ProcessWindowStyle)
        Me.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
        Me.StartInfo.CreateNoWindow = True
        Me.StartInfo.UseShellExecute = True
        Me.Start()
        Me.WaitForInputIdle()

        m_lngHandle = GetAppHandle(Me)

        If Style = ProcessWindowStyle.Hidden Then
            Me.HideApplication()
        Else
            Me.HideApplication()
            Me.ShowApplication()
        End If

        If Not m_objParentObject Is Nothing Then
            Call SetParent()
        End If
    End Sub

    Private Function GetAppHandle(ByRef Process As Process) As Integer
        Dim lngHandle As Integer

        lngHandle = -1

        Do Until lngHandle <> -1
            lngHandle = FindWindow(Process)
            If lngHandle <> -1 Then
                Exit Do
            End If
            Application.DoEvents()
        Loop

        GetAppHandle = lngHandle
    End Function

    Private Function FindWindow(ByRef Process As Process, Optional ByVal Loading As Boolean = True) As Integer
        Dim hWndNext As Integer
        Dim hWnd As Integer
        Dim lngAssocProcessID As Integer
        Dim lngReply As Integer
        Dim strClassName As String

        FindWindow = -1

        If Loading Then If Not WaitForProcess(Process) Then Exit Function

        hWndNext = FindWindow(vbNullString, vbNullString)

        Do
            lngReply = GetWindowThreadProcessId(hWndNext, lngAssocProcessID)

            If lngAssocProcessID = Process.Id Then
                strClassName = GetClassName(hWndNext)
                hWnd = FindWindow(strClassName, Process.MainWindowTitle)
                If hWnd <> 0 Then
                    FindWindow = hWnd
                    Exit Function
                End If
            End If

            hWndNext = GetWindow(hWndNext, GW_HWNDNEXT)

        Loop Until hWndNext = 0

    End Function

    Private Function GetClassName(ByVal hWnd As Integer) As String
        Dim strBuffer As New System.Text.StringBuilder(256)
        GetClassName(hWnd, strBuffer, 256)
        GetClassName = strBuffer.ToString
    End Function

    Private Function WaitForProcess(ByRef Process As Process) As Boolean
        Dim lngTimeout As Long

        lngTimeout = CLng(Now.Ticks / System.TimeSpan.TicksPerSecond) + 30

        Do Until Process.MainWindowTitle <> ""
            Application.DoEvents()
            If CLng(Now.Ticks / System.TimeSpan.TicksPerSecond) > lngTimeout Then Exit Function
        Loop

        WaitForProcess = True
    End Function

    Private Sub SetParent()
        Call SetParent(m_objParentObject.Handle.ToInt32)
    End Sub

    Public Sub SetParent(ByVal NewParentHandle As Integer)
        Dim lngStyle As Integer

        SetParent(Me.AppHandle, NewParentHandle)

        lngStyle = GetWindowLong(Me.AppHandle, GWL_STYLE)

        If Not m_blnShowCaption Then
            lngStyle = lngStyle Xor WS_CAPTION
        End If

        If Not m_blnShowButtons Then
            lngStyle = lngStyle Xor WS_SYSMENU
        End If

        lngStyle = lngStyle Xor WS_SIZEBOX

        SetWindowLong(Me.AppHandle, GWL_STYLE, lngStyle)
    End Sub

    Public Sub MoveWindow(ByVal Left As Integer, ByVal Top As Integer, ByVal Width As Integer, ByVal Height As Integer)
        Call MoveWindow(Me.AppHandle, Left, Top, Width, Height, 1)
    End Sub

    Public Sub StopProcess()
        On Error Resume Next
        If m_lngHandle <> 0 And m_blnPromptSave Then
            PostMessage(Me.AppHandle, WM_CLOSE, 1, 0)
        End If
        m_lngHandle = 0
    End Sub

    Protected Overrides Sub Finalize()
        Call StopProcess()
        MyBase.Finalize()
    End Sub

    Private Sub m_objParentObject_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_objParentObject.Resize
        On Error Resume Next
        Call MoveWindow(Me.AppHandle, 0, 0, m_objParentObject.ClientSize.Width, m_objParentObject.ClientSize.Height, 1)
    End Sub
End Class


-Now on the form:
    -Add a picture box control
    -Add a button control
    -Copy this code just before the "end class" line:

    Private m_objProcess As ExtProcess

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        m_objProcess = New ExtProcess("NotePad.exe")
        m_objProcess.ParentControl = Me.PictureBox1
        m_objProcess.SaveOnExit = True
        m_objProcess.ShowCaption = True
        m_objProcess.ShowButtons = False
        m_objProcess.WindowCaption = "New Caption"
        m_objProcess.Start(ProcessWindowStyle.Hidden)
        m_objProcess.ShowApplication(ProcessWindowStyle.Maximized)
    End Sub
0
 

Author Comment

by:hogan9111
Comment Utility
ok, but how do u do that with other application, I tried doing it with acrobat reader and nothing happened, just open the application.
0
 

Author Comment

by:hogan9111
Comment Utility
you there
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
I think I deserve some points on that. The question was first asking about Notepad and I did that!
0
 

Expert Comment

by:Zeus2009
Comment Utility
thats impressive
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
powerN  challenge 3 47
changePi Challenge 15 75
wordmultiple challenge 12 89
A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 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

18 Experts available now in Live!

Get 1:1 Help Now