在Haskell中捕获屏幕? [英] Screen capture in Haskell?

查看:146
本文介绍了在Haskell中捕获屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在Windows环境中使用Haskell捕获屏幕(或窗口)? (即每几分钟左右截图)。如果是这样,一个人会怎么做(再次,在哈斯克尔,对于Windows环境)?

更多信息:
我是一个初学者哈斯克尔。一位朋友希望通过让我为他的会计公司开发一些项目来削减开发成本,但他坚持认为我使用了Haskell。他希望有一款工具可以让他监控不同Windows XP工作站的桌面。它可能必须是客户端/服务器类型的应用程序。他只需要监控桌面活动,因此他不需要任何已经上市的昂贵管理软件。我已经筛选了大量文档,并且只找到了wxHaskell,但是在捕获屏幕时找不到太多内容,尤其是在Windows环境中。

解决方案

Tikhon提到的方法是正确的。只需在上面给出的答案中添加一些代码即可。

  import Graphics.Win32.Window 
import Graphics.Win32。 GDI.Bitmap
import Graphics.Win32.GDI.HDC
import Graphics.Win32.GDI.Graphics2D

main =做桌面< - getDesktopWindow - 抓住Hwnd桌面,GetDC 0,GetDC NULL等都可以工作
hdc< - getWindowDC(只是桌面) - 获取桌面的dc句柄
(x,y,r,b)< - getWindowRect桌面 - 查找桌面的大小,以便我们可以知道目标位图的大小应为
- (左,上,右,下)
newDC< - createCompatibleDC(Just hdc) - 创建一个新的DC以容纳复制的图像。它应该与源代码兼容
let width = r - x - 计算宽度
let height = b - y - 计算高度
newBmp< - createCompatibleBitmap hdc width height - 创建一个与新创建的DC
兼容的新位图selBmp< - selectBitmap newDC newBmp - 将位图选入DC,现在在DC上绘制位图以及
bitBlt newDC 0 0宽度高度hdc 0 0 sRCCOPY - 使用SRCCOPY将桌面DC复制到newDC
createBMPFileFoo.bmpnewBmp newDC - 将新的位图文件写出到Foo.bmp
putStrLn 复制位图图像 - 某些调试消息
deleteBitmap selBmp - 清理选定的位图
deleteBitmap newBmp - 清理新位图
deleteDC newDC - 清理我们创建的DC。

这个快速放在一起,但它保存了一个名为Foo.bmp的文件截图。
Ps。对于任何写Win32库的人来说,做得很好:)

Is it possible to capture the screen (or a window) using Haskell in a Windows environment? (ie, taking a screenshot every few minutes or so). If so, how would one go about doing this (again, in Haskell, for a Windows environment)?

More info: I'm a beginner to Haskell. A friend wants to cut development costs by having me whip together some programs for his accounting firm, but he insists that I use Haskell. He wants a tool that will allow him to monitor the desktops of different Windows XP workstations. It would likely have to be a client/server type application. He only needs to monitor desktop activity, hence why he doesn't want any of the expensive management software that is already on the market. I have sifted through lots of documentation, and only got as far as finding wxHaskell, but I couldn't find much on capturing the screen, especially for Windows environments.

解决方案

The Approach Tikhon mentioned is correct. Just to add some code to the answer he gave above

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.

This was just quickly put together, but it saves a screenshot of to a file named Foo.bmp. Ps. To whomever wrote the Win32 Library, nicely done :)

这篇关于在Haskell中捕获屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆