Click here to Skip to main content
15,887,027 members
Articles / Programming Languages / Visual Basic

ByPass difficult Automation and add applications "as is" in your .NET application

Rate me:
Please Sign up or sign in to vote.
3.40/5 (29 votes)
28 Dec 2004CPOL4 min read 153.9K   3K   74  
Fed up with automation? Need to add components as is to your application? Try this out.
Imports mshtml
Imports System.Text
Public Class Calculator
    Inherits System.Windows.Forms.Form
    Dim win32 As New Win32Functions
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Public TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Browser As System.Windows.Forms.Label
    Friend WithEvents Source As System.Windows.Forms.TextBox
    Friend WithEvents Title As System.Windows.Forms.Label
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Browser = New System.Windows.Forms.Label
        Me.Source = New System.Windows.Forms.TextBox
        Me.Title = New System.Windows.Forms.Label
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(24, 56)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "0"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(128, 56)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Open Calc"
        '
        'Browser
        '
        Me.Browser.Location = New System.Drawing.Point(312, 32)
        Me.Browser.Name = "Browser"
        Me.Browser.Size = New System.Drawing.Size(360, 384)
        Me.Browser.TabIndex = 2
        Me.Browser.Text = "Get Running Browser Window"
        '
        'Source
        '
        Me.Source.Location = New System.Drawing.Point(0, 88)
        Me.Source.Multiline = True
        Me.Source.Name = "Source"
        Me.Source.Size = New System.Drawing.Size(296, 328)
        Me.Source.TabIndex = 3
        Me.Source.Text = "Get Window Text"
        '
        'Title
        '
        Me.Title.Location = New System.Drawing.Point(304, 0)
        Me.Title.Name = "Title"
        Me.Title.Size = New System.Drawing.Size(232, 23)
        Me.Title.TabIndex = 4
        Me.Title.Text = "Title "
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(544, 0)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(136, 23)
        Me.Button2.TabIndex = 5
        Me.Button2.Text = "Get Running IE Window"
        '
        'Calculator
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(688, 422)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Title)
        Me.Controls.Add(Me.Source)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Browser)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Calculator"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ex As ExternalCalc = New ExternalCalc
        ex.formParent = Me
        ex.Show()

    End Sub

    Private Sub Calculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim px As Process() = System.Diagnostics.Process.GetProcessesByName("iexplore")
        Dim p As Process = px(0)
        Try

            win32.removeTitleBarAndRescale(p.MainWindowHandle)
            win32.SetParent(p.MainWindowHandle, Me.Browser.Handle)
            win32.SetWindowPos(p.MainWindowHandle, Me.Browser.Handle, 0, 0, Me.Browser.Width, Me.Browser.Height, win32.SWP_FRAMECHANGED Or win32.SWP_NOZORDER)
            Dim ieDom As New IEDom
            Dim i As HTMLDocument = ieDom.IEDOMFromhWnd(p.MainWindowHandle)
            Title.Text = i.title
            Source.Text = i.documentElement.innerHTML
            RemoveMenuBar(p.MainWindowHandle)
        Catch ex As Exception
            MessageBox.Show("No IE window open")
        End Try
    End Sub
    Public Delegate Function EnumChildProc(ByVal hWnd As IntPtr, ByRef lParam As IntPtr) As Int32

    Private Function RemoveMenuBar(ByVal hWnd As IntPtr) As Int32
        win32.EnumChildWindows(hWnd, AddressOf IsMenuBar, hWnd)
    End Function
    Private Function IsMenuBar(ByVal hWnd As IntPtr, ByRef lParam As IntPtr) As Int32
        Dim Res As Int32
        Dim ClassName As StringBuilder = New StringBuilder(100)
        Res = win32.GetClassName(hWnd, ClassName, ClassName.MaxCapacity)
        'Console.WriteLine(ClassName.ToString())
        If ClassName.ToString() = "ReBarWindow32" Then
            ' Hide menuBar
            win32.ShowWindow(hWnd, 0)
            Return 0
        Else
            Return 1
        End If
    End Function
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions