Click here to Skip to main content
15,891,837 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.
Public Class Form1
    Inherits System.Windows.Forms.Form
    Declare Function ShowWindow Lib "user32" (ByVal hWnd As System.IntPtr, ByVal nCmdShow As Integer) As Boolean
    Private Const SW_MINIMIZE As Integer = 6
    Private Const SW_MAXIMIZE As Integer = 3
    Private Const SW_RESTORE As Integer = 9

    Declare Function SetParent Lib "user32" (ByVal hWndChild As System.IntPtr, ByVal hWndNewParent As System.IntPtr) As System.IntPtr
#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.
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents cmdMenu As System.Windows.Forms.MenuItem
    Friend WithEvents webMenu As System.Windows.Forms.MenuItem
    Friend WithEvents mediaMenu As System.Windows.Forms.MenuItem
    Friend WithEvents wordMenu As System.Windows.Forms.MenuItem
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.MenuItem1 = New System.Windows.Forms.MenuItem
        Me.cmdMenu = New System.Windows.Forms.MenuItem
        Me.webMenu = New System.Windows.Forms.MenuItem
        Me.mediaMenu = New System.Windows.Forms.MenuItem
        Me.wordMenu = New System.Windows.Forms.MenuItem
        Me.Label1 = New System.Windows.Forms.Label
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.cmdMenu, Me.webMenu, Me.mediaMenu, Me.wordMenu})
        Me.MenuItem1.Text = "Open"
        '
        'cmdMenu
        '
        Me.cmdMenu.Index = 0
        Me.cmdMenu.Text = "Command Prompt- Open as Normal and Add as Min"
        '
        'webMenu
        '
        Me.webMenu.Index = 1
        Me.webMenu.Text = "Get Running Browser and Make it full screen"
        '
        'mediaMenu
        '
        Me.mediaMenu.Index = 2
        Me.mediaMenu.Text = "Media Player - in a Button - Max"
        '
        'wordMenu
        '
        Me.wordMenu.Index = 3
        Me.wordMenu.Text = "WinWord - In a Label"
        '
        'Label1
        '
        Me.Label1.BackColor = System.Drawing.SystemColors.GrayText
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(264, 248)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "This is some label"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(312, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(360, 232)
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(688, 266)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Label1)
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    
    Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        Me.Dispose(True)
    End Sub


    Private Sub cmdMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMenu.Click
        Dim pinfo As New ProcessStartInfo("cmd")
        Dim p As Process = System.Diagnostics.Process.Start(pinfo)
        System.Threading.Thread.CurrentThread.Sleep(1000)
        SetParent(p.MainWindowHandle, Me.Handle)
        ShowWindow(p.MainWindowHandle, SW_MINIMIZE)

    End Sub

    Private Sub webMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles webMenu.Click
        Dim p As Process() = System.Diagnostics.Process.GetProcessesByName("iexplore")
        Try
            SetParent(p(0).MainWindowHandle, Me.Handle)
            ShowWindow(p(0).MainWindowHandle, SW_MAXIMIZE)
        Catch ex As Exception
            MessageBox.Show("No IE window open")
        End Try


    End Sub

    Private Sub mediaMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mediaMenu.Click
        Dim p As Process = System.Diagnostics.Process.Start("mplayer2", Environment.GetEnvironmentVariable("WINDIR") + "\\Media\\Windows XP Startup.wav")
        p.WaitForInputIdle()
        SetParent(p.MainWindowHandle, Me.Button1.Handle)
        ShowWindow(p.MainWindowHandle, SW_MAXIMIZE)
    End Sub

    Private Sub wordMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles wordMenu.Click
        Dim pinfo As New ProcessStartInfo("WINWORD")
        Dim p As Process = System.Diagnostics.Process.Start(pinfo)
        p.WaitForInputIdle()
        SetParent(p.MainWindowHandle, Me.Label1.Handle)

    End Sub
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