110,538
社区成员
发帖
与我相关
我的任务
分享
public static Bitmap GetWindowCapture(IntPtr hWnd)
{
IntPtr hscrdc = GetWindowDC(hWnd); //返回hWnd参数所指定的窗口的设备环境。
var windowRect = Rectangle.Empty;
GetWindowRect(hWnd, ref windowRect);
int width = windowRect.Width;
int height = windowRect.Height;
IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, width, height);//该函数创建与指定的设备环境相关的设备兼容的位图
IntPtr hmemdc = CreateCompatibleDC(hscrdc);//该函数创建一个与指定设备兼容的内存设备上下文环境(DC)
SelectObject(hmemdc, hbitmap);//该函数选择一对象到指定的设备上下文环境中,该新对象替换先前的相同类型的对象
PrintWindow(hWnd, hmemdc,0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
//Clipboard.SetImage(bmp);
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
return bmp;
}
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
public static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; //最左坐标
public int Top; //最上坐标
public int Right; //最右坐标
public int Bottom; //最下坐标
}
IntPtr a = FindWindow(null, "战网");
private void button1_Click(object sender, EventArgs e)
{
StringBuilder title = new StringBuilder(256);
//RECT rect = new RECT();
//GetWindowRect(a, ref rect);
//int width = rect.Right - rect.Left; //窗口的宽度
//int height = rect.Bottom - rect.Top; //窗口的高度
//GetWindowText(a, title, title.Capacity);
this.textBox1.Text = title.ToString();
this.textBox2.Text = a.ToString();
//MoveWindow(a, 800, 800, width, height, true);//移动窗口到屏幕外
Image img = GetWindowCapture(a);
pictureBox1.Image = img;
//img.Save("D:\\Test.jpg");
// img.Dispose();
}
}
}
}