65.9K
CodeProject 正在变化。 阅读更多。
Home

适用于 Windows Mobile 5 的屏幕截图工具

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.47/5 (10投票s)

2008年6月13日

公共领域

1分钟阅读

viewsIcon

53396

downloadIcon

462

一个适用于 Pocket PC 的简单屏幕截图工具。

引言

我(和许多其他人一样)对 Pocket PC 上缺乏截屏按钮感到沮丧。所以当我的同事的屏幕截图工具试用版过期时,我决定自己创建一个。

在 Google 上搜索后,我找到了一些线索,但没有完整的解决方案,所以我认为一个不错的起点是写一篇 CodeProject 文章。

代码的碎片(紧凑的代码)来源于网络,但它已经被重做、重新打包和重新编码,很难追溯到任何单个网页。所以目前没有感谢其他人的地方…

Using the Code

该应用程序会弹出一个消息框,然后等待按下硬件按钮(在上面的图片中,是标记为绿色的键)。硬件按键取决于设备,所以如果你将其部署到真实的 Pocket PC 上,你必须弄清楚你的设备上的按键是什么。

当用户按下正确的按钮时,一个名为 capture.jpg 的 bmp 文件保存在根目录下,会显示另一个消息框,应用程序结束。就这么简单。

该应用程序使用硬件按钮组件来监听按钮点击。主要的捕获过程发生在以下位置

    public class Capturer
    {
        // Declarations
        [DllImport("coredll.dll")]
        public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest,
            int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
        
        [DllImport("coredll.dll")]
        private static extern IntPtr GetDC(IntPtr hwnd);
        
        const int SRCCOPY = 0x00CC0020;


        public static void Snapshot(string fileName, Rectangle rectangle)
        {
            //Use a zeropointer to get hold of the screen context
            IntPtr deviceContext = GetDC(IntPtr.Zero);
            
            
            using(Bitmap capture = new Bitmap(rectangle.Width, rectangle.Height))
            //Get screen context
            using (Graphics deviceGraphics = Graphics.FromHdc(deviceContext))
            //Get graphics from bitmap
            using (Graphics captureGraphics = Graphics.FromImage(capture))
            {
                // Blit the image data
                BitBlt(captureGraphics.GetHdc(), 0, 0,
                    rectangle.Width, rectangle.Height, deviceGraphics.GetHdc(),
                    rectangle.Left, rectangle.Top, SRCCOPY);

                //Save to disk
                capture.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
            }
        }
    }

该代码通过使用零指针的 GetDC 获取屏幕设备上下文,然后 bitblts 到一个保存在硬编码位置(根目录)的位图中。

历史

2008-06-12:发布第一个版本。

© . All rights reserved.