|
-
Mar 26th, 2011, 08:08 PM
#1
Thread Starter
Junior Member
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?
-
Mar 27th, 2011, 01:31 AM
#2
Re: SetParent Question
 Originally Posted by andrecito77
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
-
Mar 27th, 2011, 09:57 AM
#3
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|