Sorry voor de lange lap code, maar deze werkt wel met mstsc.exe iig.
Probleem was bij de vorige code dat de MainWindowHandle niet altijd verwijst naar het window wat je ziet (bijvoorbeeld naar een onzichtbaar achtergrondwindow). Onderstaande code pakt gewoon de foregroundwindow, wat in vrijwel alle gevallen goed werkt. Het kan wel netter natuurlijk, maar dat moet met veel meer code dan.
Nu word het child-window ook geresized (als je de anchor-property van de picturebox instelt bijv.) en weer 'losgelaten' zodra je je window sluit.
Nadeel is wel dat een app als mstsc.exe een vaste window grootte heeft, dat kun je eventueel weer oplossen in de resize code.
Visual Basic .NET:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
| Imports System.Runtime.Interopservices
Public Class Form1
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer
Private Declare Function MoveWindow Lib "user32" (ByVal hWnd As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean
Private Declare Auto Function SetWindowLong Lib "User32.Dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
<DllImport("user32.dll", EntryPoint:="GetWindowLong")> _
Private Shared Function GetWindowLong32(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="GetWindowLongPtr")> _
Private Shared Function GetWindowLongPtr64(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function
Private childHandle As Long
Private oldStyle As Long
Private Const SM_CYBORDER = 6
Private Const SM_CYCAPTION = 4
Private Const GWL_STYLE = (-16)
Private Const WS_CAPTION = &HC00000 'WS_BORDER Or WS_DLGFRAME
Private Const WS_DLGFRAME = &H400000
Private Const WS_BORDER = &H800000
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim p As Process = Process.Start("mstsc.exe")
p.WaitForInputIdle()
childHandle = GetForegroundWindow()
SetParent(childHandle, Me.PictureBox1.Handle)
RemoveCaptionBar(childHandle)
resizeChild()
End Sub
Private Sub PictureBox1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Resize
resizeChild()
End Sub
Private Sub resizeChild()
If (childHandle > 0) Then
MoveWindow(childHandle, 0, 0, Me.PictureBox1.Width, Me.PictureBox1.Height, True)
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If (childHandle > 0) Then
AddCaptionBar(childHandle)
SetParent(childHandle, 0)
End If
End Sub
Public Shared Function GetWindowLongPtr(ByVal hWnd As Long, ByVal nIndex As Integer) As IntPtr
If IntPtr.Size = 8 Then
Return GetWindowLongPtr64(hWnd, nIndex)
Else
Return New IntPtr(GetWindowLong32(hWnd, nIndex))
End If
End Function
Private Sub RemoveCaptionBar(ByVal hwnd As Long)
oldStyle = GetWindowLongPtr(hwnd, GWL_STYLE)
Dim NewStyle As Long = oldStyle And Not WS_CAPTION
SetWindowLong(hwnd, GWL_STYLE, NewStyle)
End Sub
Private Sub AddCaptionBar(ByVal hwnd As Long)
SetWindowLong(hwnd, GWL_STYLE, oldStyle)
End Sub
End Class |
[
Voor 12% gewijzigd door
user109731 op 16-05-2006 20:02
. Reden: yeahh, de parser werkt weer :) ]