本人正用 C# 开发屏幕录制程序,允许用户指定任意窗口区域为录制范围,这就必须获得当前用户指定的窗口对象及其相对屏幕的位置和大小,固有此问。

解决方案 »

  1.   

    窗口相对屏幕的位置,你有窗口的句柄还是自己的窗体?
    如果不是自己写的窗体,是获得的窗口句柄,可以用GetWindowRect
    如果是自己的窗体。这到容易
    Rectangle rect = new Rectangle(窗体对象.Left,窗体对象.Top,窗体对象.Width,窗体对象.Height);
      

  2.   

    算了。估计你也看不明白如何实现,写个例子给你好了。[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]
    public static extern IntPtr GetForegroundWindow();
    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowRect")]
    public static extern int GetWindowRect(IntPtr hwnd, ref System.Drawing.Rectangle lpRect);
    private static void TestGetForegroundWindowRectangle()
    {
        IntPtr hForeground = GetForegroundWindow();
        System.Drawing.Rectangle rect = new System.Drawing.Rectangle();
        GetWindowRect(hForeground, ref rect);
        //这里的rect就是你你要的
    }
      

  3.   

    this下面有坐标转屏幕坐标的把当前的local转一下就可以了。不用API。
    用API也可的。楼上给出了。
      

  4.   


    你真知道?你知道还问?GetForegroundWindow是获得当前操作系统被激活的窗体句柄
    GetWindowRect是获得窗体的位置这不是你要的?如果不是,那显然你没理解你要的是什么,如果是想说鼠标悬停位置的窗体。你就要遍历所有当前窗体,获得rect,然后看鼠标落在谁的范围内。
      

  5.   

    我们知道,这其实一个跨进程操作的问题。也就是说,如果是在当前应用程序中获得任意窗口的属性都是轻而易举的事儿,何劳 API 登场啊?
      

  6.   

    谢谢 wuyazhe,问题得到了解决。再次谢谢!!