日期:2014-05-18  浏览次数:20741 次

菜鸟的疑惑,求注释
小弟刚接触GDI。大神帮我注释下下面的代码(通俗点,详细点,毕竟菜鸟嘛。0.0)。
C# code
public static Bitmap GetDesktop()
        {
            int screenX;
            int screenY;
            IntPtr hBmp;
            IntPtr hdcScreen = GetDC(GetDesktopWindow());
            IntPtr hdcCompatible = CreateCompatibleDC(hdcScreen);

            screenX = GetSystemMetrics(0);
            screenY = GetSystemMetrics(1);
            hBmp = CreateCompatibleBitmap(hdcScreen, screenX, screenY);

            if (hBmp != IntPtr.Zero)
            {
                IntPtr hOldBmp = (IntPtr)SelectObject(hdcCompatible, hBmp);
                BitBlt(hdcCompatible, 0, 0, screenX, screenY, hdcScreen, 0, 0, 13369376);

                SelectObject(hdcCompatible, hOldBmp);
                DeleteDC(hdcCompatible);
                ReleaseDC(GetDesktopWindow(), hdcScreen);

                Bitmap bmp = System.Drawing.Image.FromHbitmap(hBmp);

                DeleteObject(hBmp);
                GC.Collect();

                return bmp;
            }


------解决方案--------------------
对于这段代码,你需要理解一下DC的概念,和windows api DC操作。

大致意思:
1、取得屏幕的DC
2、创建一个兼容的DC
3、创建一个屏幕大小的兼容Bitmap
4、将此位图算入兼容DC
5、将屏幕DC拷贝到兼容的DC的位图中
6、回复兼容DC的位图,上一步已经返回的结果
7、删除兼容的DC
8、释放桌面DC
9、从Win32位图句柄转换为.Net的Bitmap类对象
10、删除位图句柄。