C# getwindowRect的问题

napolengogo napolengogo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我在C#中通过调用getWindowRect来获得其他一个程序窗口的位置,在远程服务器上执行,当我打开远程桌面时,远程该程序,能够正确的得到窗口位置,但是当我关闭远程桌面后,getwindowRect得到的位置居然是负值,请求高手给予解答啊。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

using SimulationClick;

namespace test
{
class Program
{

[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("User32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hwnd, ref Rect lpRect);





public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;


public override String ToString()
{
return base.ToString() + "Left=" + this.Left + " Top=" + this.Top + " Right=" + this.Right + " Bottom=" + this.Bottom;
}
}



static void Main(string[] args)
{

while (true)
{
IntPtr avd = FindWindow(null, "5554:Weixin");
if (avd == IntPtr.Zero)
{
Console.WriteLine("no window");
}
else
{
Console.WriteLine(avd.ToString());
Rect rect = new Rect();
GetWindowRect(avd, ref rect);
Console.WriteLine(rect.ToString());
}
Thread.Sleep(5000);

}

}
}
}


运行结果:
test.Program+RectLeft=283 Top=0 Right=930 Bottom=752
1573276
test.Program+RectLeft=283 Top=0 Right=930 Bottom=752
1573276
test.Program+RectLeft=-32000 Top=-32000 Right=-31840 Bottom=-31969
1573276
test.Program+RectLeft=-32000 Top=-32000 Right=-31840 Bottom=-31969
其中上面两条结果是我打开着远程桌面的时候的输出结果
而当我将远程桌面关闭后,就是下面的结果了,求大牛们解释,不甚感激。
...全文
评论 3
收藏 已收藏
分享
返回顶部
napolengogo
C# 111,130 成员 642,540 内容