Windows11记事本无法在vb.net中通过FindWindowEx找到

问题描述 投票:0回答:1

我想通过使用vb.net源在Windows 11中找到记事本。
但我总是收到记事本窗口未找到错误。
我认为Windows 11记事本改变风格是主要原因。
它完全适用于 Windows 10。

这是我的代码示例。
请推荐我。

<System.Runtime.InteropServices.DllImport("user32", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Auto)>
Private Shared Function FindWindowEx(
                                    ByVal hwndParent As Integer,
                                    ByVal hwndChild As Integer,
                                    ByVal lpClassName As String,
                                    ByVal lpWindowName As String) As IntPtr


End Function

Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
        Dim VM_SETTEXT As String = &HCS
        Dim source As String = "hello world"
        Dim p As New Process
        p.StartInfo.FileName = "notepad.exe"       
        p.Start()     
       
        Dim hwnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Notepad", "")

        If hwnd <> IntPtr.Zero Then
            MessageBox.Show("Find notepad window")
        End If
End Sub

我想在Windows 11中找到记事本窗口以将数据发送到记事本。

vb.net notepad
1个回答
0
投票

根据我的测试,这个:

Dim hwnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Notepad", "")

应该是这样的:

Dim hwnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Notepad", Nothing)

正如您所拥有的,它只会匹配标题为空字符串的窗口。通过指定

Nothing
,您可以告诉它匹配任何标题。

此外,您已将前两个参数声明为

Integer
类型,但随后传递了
IntPtr
值。如果你有
Option Strict Off
就可以了,但你绝对不应该有
Option Strict Off
,所以不应该编译。将参数声明为
IntPtr
类型或传递
Integer
值。您应该在项目属性和 VS 选项中设置
Option Strict On
,这样以后的所有项目都会默认为
On

© www.soinside.com 2019 - 2024. All rights reserved.