关于嵌入窗体的编程(vb.net)

前端之家收集整理的这篇文章主要介绍了关于嵌入窗体的编程(vb.net)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

尝试了好几天,终于找出来了一个错误,没有根据,只是猜测.关于Win API函数调用的.先前总是弹出错误"应用程序定义错误或对象定义错误",弄了好几次都不见有什么起效.于是把数据类型好好的检查,把long 改成integer .然后再尝试,用点效果了.窗口标题框去掉了.至于另外一个API函数-SetWindowPos()实在无能为力,还有DestroyWindow()也是,只要是能想到的,基本都尝试过了,结局都是悲剧.只有用例外一个API 函数-MoveWindow()代替一下了(幸好有备用胎),这个函数是百试百灵.谁叫.NET不提供窗口操作类函数,只有求助于API了.

还有一个问题,这个程序对有些窗口不起作用,估计是窗口类不同,要考试了,没时间了,以后再完善.

代码如下:

创建一个WIN32API类,方便调用:

Imports System.Runtime.InteropServices
Public Class Win32API

'//////////////////////////////////////////////////常量声明

Public Const SWP_SHOWWINDOW As Long = &H40
Public Const SWP_HIDEWINDOW As Long = &H80
Public Const WS_VSCROLL As Integer = &H200000
Public Const WS_HSCROLL As Integer = &H100000
Public Const WS_CAPTION As Integer = &HC00000
Public Const HWND_TOPMOST As Integer = -1

'//////////////////////////////////////////////////api函数声明

Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As IntPtr,_
ByVal hWndNewParent As IntPtr) As Integer

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String,_
ByVal lpWindowName As String) As IntPtr

Declare Function MoveWindow Lib "user32" Alias "MoveWindow" (ByVal hwnd As IntPtr,ByVal X As Integer,_
ByVal Y As Integer,ByVal cx As Integer,ByVal cy As Integer,ByVal Flags As Boolean) As Boolean

Declare Function DestroyWindow Lib "user32" Alias "DestroyWindow" (ByVal hwnd As IntPtr) As Integer

Declare Auto Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As IntPtr,_
ByVal nIndex As Integer,ByVal dwNewLong As Integer) As Integer

Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As IntPtr,_
ByVal nIndex As Integer) As Integer

Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As IntPtr,ByVal hWndInsertAfter As Integer,_
ByVal x As Long,ByVal y As Long,ByVal cx As Long,ByVal cy As Long,ByVal wFlags As Long) As Integer
<DllImport("user32")> _
Public Shared Function SetWindowText(ByVal hwnd As Integer,ByVal l As String) As Boolean
End Function
<DllImport("user32")> _
Public Shared Function ShowWindow(ByVal hwnd As Integer,ByVal pa As Integer) As Boolean
End Function
'<DllImport("user32")> _
'Public Shared Function SetWindowLong(ByVal hwnd As Integer,ByVal nIndex As Integer,ByVal dwNewLong As Integer) As Integer
'End Function
End Class

@H_403_89@主窗口代码:

Public Class MainForm

Dim hwnd As IntPtr

Private Sub FormMain_Resize(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Resize
Try
Err.Clear()
Win32API.MoveWindow(hwnd,Me.TabPage2.Width,Me.TabPage2.Height,True) '随父窗体一起缩放
Catch ex As Exception
MessageBox.Show(ErrorToString(Err.LastDllError) & ex.Message)
End Try
End Sub

Private Sub Xuan_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Xuan.Click
Try
Err.Clear()
hwnd = Win32API.FindWindow(vbNullString,xuanze.Text)
Catch ex As Exception
MessageBox.Show(ErrorToString(Err.LastDllError) & ex.Message)
End Try
End Sub

Private Sub Yun_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Yun.Click
Try
System.Diagnostics.Process.Start(yunxing.Text)
System.Threading.Thread.Sleep(18) '过快下面的FindWindow有可能找不到窗体
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub Qian_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Qian.Click
Try
Err.Clear()
Win32API.SetParent(hwnd,Me.TabPage2.Handle) '设置父窗体
System.Threading.Thread.Sleep(100)
Win32API.SetWindowLong(hwnd,-16,Win32API.GetWindowLong(hwnd,-16) And (Not Win32API.WS_CAPTION)) '去掉标题
'MessageBox.Show(ErrorToString(Err.LastDllError))
'Win32API.SetWindowPos(hwnd,Me.TabPage2.Handle,Win32API.SWP_SHOWWINDOW) '直接嵌套到TabPage1内
Win32API.MoveWindow(hwnd,True) '随父窗体一起缩放

Catch ex As Exception
MessageBox.Show(ErrorToString(Err.LastDllError) & ex.Message)
End Try
End Sub

Private Sub Yin_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Yin.Click
Try
Err.Clear()
Win32API.ShowWindow(hwnd,0) '隐藏窗口
Catch ex As Exception
MessageBox.Show(ErrorToString(Err.LastDllError) & ex.Message)
End Try
End Sub

Private Sub Xian_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Xian.Click
Try
Err.Clear()
Win32API.ShowWindow(hwnd,1) '显示窗口
Catch ex As Exception
MessageBox.Show(ErrorToString(Err.LastDllError) & ex.Message)
End Try
End Sub

Private Sub Shan_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Shan.Click
Try
Err.Clear()
Win32API.DestroyWindow(hwnd) '删除窗体
Catch ex As Exception
MessageBox.Show(ErrorToString(Err.LastDllError) & ex.Message)
End Try
End Sub

Private Sub Gai_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Gai.Click
Try
Err.Clear()
Win32API.SetWindowText(hwnd,gaibian.Text) '改变窗口标题
Catch ex As Exception
MessageBox.Show(ErrorToString(Err.LastDllError) & ex.Message)
End Try
End Sub
End Class
@H_403_89@主窗口界面:

运行后例子:

看窗口的标题栏不见了,而且嵌在指定窗口里面,还可以随着窗口放大缩小!估计CF等游戏窗口化外挂就是这样弄的,当然要更高深点.

猜你在找的VB相关文章