Results 1 to 3 of 3

Thread: SetParent Question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    21

    Question SetParent Question

    I am placing an external program into my application. The current code is as follows:
    Code:
    Const SW_HIDE As Integer = 0
        Const SW_SHOWNORMAL As Integer = 1
        Const SW_NORMAL As Integer = 1
        Const SW_SHOWMINIMIZED As Integer = 2
        Const SW_SHOWMAXIMIZED As Integer = 3
        Const SW_MAXIMIZE As Integer = 3
        Const SW_SHOWNOACTIVATE As Integer = 4
        Const SW_SHOW As Integer = 5
        Const SW_MINIMIZE As Integer = 6
        Const SW_SHOWMINNOACTIVE As Integer = 7
        Const SW_SHOWNA As Integer = 8
        Const SW_RESTORE As Integer = 9
        Const SW_SHOWDEFAULT As Integer = 10
        Const SW_FORCEMINIMIZE As Integer = 11
        Const SW_MAX As Integer = 11
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Shared Function FindWindow( _
         ByVal lpClassName As String, _
         ByVal lpWindowName As String) As IntPtr
        End Function
        Declare Function SetParent Lib "user32" (ByVal hWndChild As System.IntPtr,ByVal hWndNewParent As System.IntPtr) As System.IntPtr
    
        Private Sub Emulator_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim MyPSI As New ProcessStartInfo("notepad.exe")
            Dim MyProcess As Process = Process.Start(MyPSI)
            MyProcess.WaitForInputIdle()
            Dim hWnd As IntPtr
            hWnd = MyProcess.MainWindowHandle
            SetParent(hWnd, Me.Handle)
            Me.Text = MyProcess.MainWindowTitle
        End Sub
    Btw: I know I have lots of unused functions

    What I want is for "notepad" to open up maximized and without a border. I already have it maximized, but I want the border gone.

    How can I do this?
    Also how can I make my current form the same size as the process?

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: SetParent Question

    Quote Originally Posted by andrecito77 View Post
    What I want is for "notepad" to open up maximized and without a border. I already have it maximized, but I want the border gone.

    How can I do this?
    Also how can I make my current form the same size as the process?
    Use the GetWindowRect API to get the size of a window.
    As far as removing notepads border you might want to look at the Get/SetWindowLong APIs and their flags, then to make notepad fit inside the form (maximized ) I just used MoveWindow.

    I tried it like this in WinXP-32 and Win7-64 and seemed to work OK when compiled for x86...

    Code:
    Public Class Form1
    
        Dim notepad_hWnd As IntPtr = IntPtr.Zero
    
        Private Const GWL_STYLE As Integer = (-16)
        Private Const WS_BORDER As Integer = &H800000
        Private Declare Function IsWindow Lib "user32" Alias "IsWindow" (ByVal hwnd As IntPtr) As Integer
        Private Declare Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
        Private Declare Function MoveWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal X As Int32, ByVal Y As Int32, ByVal nWidth As Int32, ByVal nHeight As Int32, ByVal bRepaint As Boolean) As Boolean
        Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
        Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer) As Integer
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
            Dim MyPSI As New ProcessStartInfo("notepad.exe")
            Try
                Dim MyProcess As Process = Process.Start(MyPSI)
                MyProcess.WaitForInputIdle()
                notepad_hWnd = MyProcess.MainWindowHandle
    
                If notepad_hWnd <> IntPtr.Zero Then
                    Me.Text = MyProcess.MainWindowTitle
                    ' set parent
                    SetParent(notepad_hWnd, Me.Handle)
                    ' remove notepad border
                    SetWindowLong(notepad_hWnd, GWL_STYLE, GetWindowLong(notepad_hWnd, GWL_STYLE) + WS_BORDER)
                    ' size notepad to forms client size
                    MoveWindow(notepad_hWnd, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height, True)
                End If
            Catch ex As Exception
                MessageBox.Show("Unable to launch notepad.exe", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    
            ' if notepad still running put it on the desktop or it will be running in background!
            If notepad_hWnd <> IntPtr.Zero Then
                If IsWindow(notepad_hWnd) <> 0 Then
                    ' re-enable notepad border?
                    SetWindowLong(notepad_hWnd, GWL_STYLE, GetWindowLong(notepad_hWnd, GWL_STYLE) + WS_BORDER)
                    ' move notepad to desktop
                    SetParent(notepad_hWnd, IntPtr.Zero)
                End If
            End If
    
        End Sub
    
    End Class

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    21

    Re: SetParent Question

    Nice Thanks a bunch

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width