简体   繁体   English

如何在多个潜艇中使用句柄

[英]How to use handle in multiple subs

I'm certainly no expert on VB, so hopefully someone is willing to help me out here.我当然不是VB专家,所以希望有人愿意在这里帮助我。 When I use the code down below, the msgbox in mybase.shown properly shows the handle number, but the one in button1.click throws the exception "No process is associated with this object".当我使用下面的代码时,mybase.shown 中的 msgbox 正确显示了句柄编号,但 button1.click 中的那个会引发异常“没有进程与此对象关联”。 So apparently the handle is only available during the mybase.shown sub?所以显然这个句柄只在 mybase.shown 子期间可用? How do I make it available for other subs too?我如何使它也可用于其他潜艇? Thanks for any help in advance, Kind regards, Eric感谢您提前提供的任何帮助,亲切的问候,埃里克

Option Strict On
    Public Class Form1
        Private WithEvents proc As New Process
        Private Const WM_SYSCOMMAND As Integer = &H112
        Private Const SC_MAXIMIZE As Integer = &HF030
        Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
        Declare Auto Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndParent As IntPtr) As Integer
        Declare Auto Function SendMessage Lib "user32" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
            Dim proc As Process = Process.Start("notepad")
            Threading.Thread.Sleep(200)
            SetParent(proc.MainWindowHandle, Panel1.Handle)
            SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
            MsgBox(proc.MainWindowHandle)
        End Sub
        Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            MsgBox(proc.MainWindowHandle)
            SetParent(proc.MainWindowHandle, IntPtr.Zero)
        End Sub
    End Class

Thank you very much everybody, With your help.非常感谢大家,在您的帮助下。 I finally managed to make it work, I really appreciate your efforts!我终于成功了,我真的很感谢你的努力! Kind regards, Eric亲切的问候,埃里克

Options Strict On
Public Class Form1
    Private WithEvents proc As New Process
    Private Const WM_SYSCOMMAND As Integer = &H112
    Private Const SC_MAXIMIZE As Integer = &HF030
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Declare Auto Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndParent As IntPtr) As Integer
    Declare Auto Function SendMessage Lib "user32" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Dim testje As Long
    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        proc = Process.Start("notepad")
        Threading.Thread.Sleep(200)
        SetParent(proc.MainWindowHandle, Panel1.Handle)
        SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    End Sub
    Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        SetParent(proc.MainWindowHandle, IntPtr.Zero)
    End Sub
End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM