Click here to Skip to main content
11,702,168 members (66,230 online)
Click here to Skip to main content
Articles » Languages » VB.NET » HowTo » Downloads
Add your own
alternative version

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

, 28 Dec 2004 CPOL 120.9K 2.5K 73
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()

        'This call is required by the Windows Form Designer.

        '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
            End If
        End If
    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.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.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"
        Me.cmdMenu.Index = 0
        Me.cmdMenu.Text = "Command Prompt- Open as Normal and Add as Min"
        Me.webMenu.Index = 1
        Me.webMenu.Text = "Get Running Browser and Make it full screen"
        Me.mediaMenu.Index = 2
        Me.mediaMenu.Text = "Media Player - in a Button - Max"
        Me.wordMenu.Index = 3
        Me.wordMenu.Text = "WinWord - In a Label"
        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"
        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"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(688, 266)
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.WindowState = System.Windows.Forms.FormWindowState.Maximized

    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
    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)
        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")
            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")
        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)
        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.


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


About the Author

Armoghan Asif
Pakistan Pakistan
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 29 Dec 2004
Article Copyright 2004 by Armoghan Asif
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid