简体   繁体   中英

Getting A Window's Region

I am trying to a get a window's height and width using this :

    [DllImport("User32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetForegroundWindow();

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

    private void timer1_Tick(object sender, EventArgs e)
    {
        Rectangle bonds = new Rectangle();
        GetWindowRect(GetForegroundWindow(), out bonds);
        Bitmap bmp = new Bitmap(bonds.Width, bonds.Height);
        Graphics memoryGraphics = Graphics.FromImage(bmp);
        IntPtr dc = memoryGraphics.GetHdc();
        PrintWindow(GetForegroundWindow(), dc, 0x1);
        memoryGraphics.ReleaseHdc(dc);
        bmp.Save("C:\\Test.gif", ImageFormat.Gif);
    }

but bonds.width and height are more than the real window.

Example : My window is 100x150, bond.height is like 400 and bonds.width is like 500. I get a picture with the size of 400x500 that consist of the window in the corner of the picture (which is good) and the rest is black since the picture is way bigger than the window (which is bad)

NOTE : I don't care that the window is aeroless, it's good for me.

So any suggestion, or maybe a better way of getting a region?

You need to use ScreenToClient and translate the coordinates.

GetWindowRect returns the window area relative to the top-left of the screen ( 0, 0 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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