Click here to Skip to main content
15,891,375 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 154.2K   3K   74  
Fed up with automation? Need to add components as is to your application? Try this out.
Imports System.Reflection
Imports System.Text
Imports System.Runtime.InteropServices
Public Class ExternalCalc
    Inherits System.Windows.Forms.Form
    ' Constants used to remove the borders of Media Player
    Private Const LEFT_CONST As Integer = 0
    Private Const RIGHT_CONST As Integer = 0
    Private Const TOP_CONST As Integer = 20
    Private Const BOTTOM_CONST As Integer = 0
#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.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'ExternalCalc
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(256, 206)
        Me.Name = "ExternalCalc"
        Me.Text = "ExternalCalc"

    End Sub

#End Region
    Dim p As Process
    Dim win32 As New Win32Functions
    Private Sub ExternalCalc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim pinfo As New ProcessStartInfo("calc")
        p = System.Diagnostics.Process.Start(pinfo)
        p.WaitForInputIdle()
        win32.SetParent(p.MainWindowHandle, Me.Handle)
        win32.removeTitleBarAndRescale(p.MainWindowHandle)
        win32.SetWindowPos(p.MainWindowHandle, Me.Handle, -LEFT_CONST, -TOP_CONST, Me.Width + LEFT_CONST + RIGHT_CONST, Me.Height + TOP_CONST + BOTTOM_CONST, win32.SWP_FRAMECHANGED Or win32.SWP_NOZORDER)
    End Sub

    Private Sub ExternalCalc_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        win32.SetForegroundWindow(p.MainWindowHandle)
        SendKeys.SendWait("^{c}")
        If Not (formParent1 Is Nothing) Then
            formParent1.TextBox1.Clear()
            formParent1.TextBox1.Paste()
            p.CloseMainWindow()
            p.Close()

        End If
    End Sub
    Dim formParent1 As Calculator
    Public Property formParent()
        Get
            formParent = formParent1
        End Get
        Set(ByVal Value)
            formParent1 = Value
        End Set
    End Property
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