是否有可能在Windows环境中使用Haskell捕获屏幕(或窗口)? (即每几分钟左右截图)。 如果是这样,那么如何去做这件事(在Haskell中,对于Windows环境)呢?
更多信息:我是Haskell的初学者。 一个朋友希望通过让我为他的会计公司开发一些程序来削减开发成本,但是他坚持认为我使用了Haskell。 他想要一个能让他监控不同Windows XP工作站桌面的工具。 这可能必须是一个客户端/服务器types的应用程序。 他只需要监控桌面活动,为什么他不想要任何已经上市的昂贵的pipe理软件。 我筛选了大量的文档,只是find了wxHaskell,但是在捕获屏幕方面找不到太多东西,尤其是在Windows环境下。
Tikhon提到的方法是正确的。 只要在上面给出的答案上添加一些代码即可
import Graphics.Win32.Window import Graphics.Win32.GDI.Bitmap import Graphics.Win32.GDI.HDC import Graphics.Win32.GDI.Graphics2D main = do desktop <- getDesktopWindow -- Grab the Hwnd of the desktop, GetDC 0, GetDC NULL etc all work too hdc <- getWindowDC (Just desktop) -- Get the dc handle of the desktop (x,y,r,b) <- getWindowRect desktop -- Find the size of the desktop so we can know which size the destination bitmap should be -- (left, top, right, bottom) newDC <- createCompatibleDC (Just hdc) -- Create a new DC to hold the copied image. It should be compatible with the source DC let width = r - x -- Calculate the width let height = b - y -- Calculate the Height newBmp <- createCompatibleBitmap hdc width height -- Create a new Bitmap which is compatible with the newly created DC selBmp <- selectBitmap newDC newBmp -- Select the Bitmap into the DC, drawing on the DC now draws on the bitmap as well bitBlt newDC 0 0 width height hdc 0 0 sRCCOPY -- use SRCCOPY to copy the desktop DC into the newDC createBMPFile "Foo.bmp" newBmp newDC -- Write out the new Bitmap file to Foo.bmp putStrLn "Bitmap image copied" -- Some debug message deleteBitmap selBmp -- Cleanup the selected bitmap deleteBitmap newBmp -- Cleanup the new bitmap deleteDC newDC -- Cleanup the DC we created.
这只是快速放在一起,但它保存了一个名为Foo.bmp文件的截图。 PS。 任何人写的Win32库,很好地完成:)
您也可以使用GTK以跨平台的方式进行。
这与C: 使用C / GTK进行截图没有多大区别。
{-# LANGUAGE OverloadedStrings #-} import Graphics.UI.Gtk import System.Environment import Data.Text as T main :: IO () main = do [fileName] <- getArgs _ <- initGUI Just screen <- screenGetDefault window <- screenGetRootWindow screen size <- drawableGetSize window origin <- drawWindowGetOrigin window Just pxbuf <- pixbufGetFromDrawable window ((uncurry . uncurry Rectangle) origin size) pixbufSave pxbuf fileName "png" ([] :: [(T.Text, T.Text)])
你应该可以用Win32 API来做到这一点。 基于什么是在Windows中使用C ++截取窗口的最佳方式? ,您需要获取窗口的上下文,然后分别使用GetWindowDC
和BitBlt
从中复制图像。
回顾Haskell Win32 API文档, Graphics.Win32.Window
有一个getWindowDC
函数。 这将返回一个IO HDC
。 Graphics.Win32.GDI.Graphics2D
有一个bitblt
函数。 这个函数需要一个HDC
以及一堆INT
,它们大概对应于C ++中的参数。
不幸的是,我没有一台Windows机器,所以我不能写实际的代码。 你将不得不弄清楚如何使用自己的Win32 API函数,这可能有点麻烦。
当你这样做的时候,如果你把它分解成一个库,然后把它放在Hackage上,那就太好了–Windows在Haskell领域通常不会有太多的爱(正如我自己展示的:P),所以我确信其他的Windows程序员会感谢一个简单的方法来截图。