Solved

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

Posted on 2004-08-31
12
1,476 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
ID: 11948173
0
 

Author Comment

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

Expert Comment

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

Author Comment

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

0
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.

 
LVL 69

Accepted Solution

by:
Éric Moreau earned 50 total points
ID: 11959370
-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
ID: 11969326
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
ID: 12005902
you there
0
 
LVL 69

Expert Comment

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

Expert Comment

by:Zeus2009
ID: 25547072
thats impressive
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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

896 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