1

有人可以发布允许我通过名称读取特定窗口的位置和分辨率的代码,例如

private function findposition(byval windowtitle as string)

欢呼马丁

我在用

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

Private Declare Function GetWindowRect Lib "user32" (ByVal _
        hwnd As IntPtr, ByVal lpRect As Rectangle) As Integer

Dim lobbywindow As IntPtr = FindWindow("Appclass", "Appname")
Dim lobbyrectangle As New Rectangle

GetWindowRect(lobbywindow, lobbyrectangle)

请协助解决这个问题,因为我收到错误

调用 PInvoke 函数 'App!App.Form1::GetWindowRect' 使堆栈失衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查 PInvoke 签名的调用约定和参数是否与目标非托管签名匹配。

4

2 回答 2

2

在这种情况下,您需要使用 Windows API,此处描述了类似的问题:http ://www.activevb.de/tipps/vb6tipps/tipp0111.html

你需要实施

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

Private Declare Function GetWindowRect Lib "user32" (ByVal _
        hwnd As Long, lpRect As RECT) As Long
于 2012-06-23T14:08:34.000 回答
2

我认为它适用于任何窗口,即使是外部的?这样的事情可能会有所帮助:

    [DllImport("user32.dll")]
    private static extern IntPtr FindWindow(string className, string windowName);

    [DllImport("user32.dll")]
    private static extern int GetWindowRect(IntPtr hwnd, out Rectangle rect);



    private void button2_Click(object sender, EventArgs e)
    {
        string className = "yourClassName";
        string windowName = "yourWindowName";

        Rectangle rect;
        IntPtr hwnd = FindWindow(className, windowName);
        GetWindowRect(hwnd, out rect);
    }
于 2012-06-23T14:08:57.783 回答