Capture d’écran dans Haskell?

Est-il possible de capturer l’écran (ou une fenêtre) en utilisant Haskell dans un environnement Windows? (c.-à-d., prendre une capture d’écran à peu près toutes les quelques minutes). Si oui, comment s’y prendrait-il (encore une fois, dans Haskell, pour un environnement Windows)?

Plus d’infos: Je suis débutant chez Haskell. Un ami veut réduire les coûts de développement en me confiant des programmes pour son cabinet d’expert-comptable, mais il insiste pour que j’utilise Haskell. Il souhaite un outil lui permettant de surveiller les postes de travail de différents postes de travail Windows XP. Il devrait probablement s’agir d’une application de type client / serveur. Il lui suffit de surveiller l’activité des postes de travail, c’est pourquoi il ne souhaite aucun des logiciels de gestion coûteux déjà sur le marché. J’ai passé au crible beaucoup de documentation, et je n’ai trouvé que wxHaskell, mais je ne trouvais pas grand-chose à capturer l’écran, en particulier pour les environnements Windows.

L’approche mentionnée par Tikhon est correcte. Juste pour append du code à la réponse qu’il a donnée ci-dessus

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. 

Cela a été rapidement mis en place, mais il enregistre une capture d'écran dans un fichier nommé Foo.bmp. Ps. Pour qui a écrit la bibliothèque Win32, joliment fait 🙂

Vous pouvez également le faire de manière multi-plateforme avec GTK .

Ce ne serait pas très différent de le faire avec C: Prendre une capture d’écran avec C / GTK .

 {-# LANGUAGE OverloadedSsortingngs #-} 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)]) 

Vous devriez pouvoir le faire avec l’API Win32. Basé sur Quelle est la meilleure façon de prendre des captures d’écran d’une fenêtre avec C ++ sous Windows? , vous devez obtenir le contexte de la fenêtre, puis copier l’image à l’aide de GetWindowDC et BitBlt respectivement.

En getWindowDC documentation de l’API Haskell Win32, il existe une fonction getWindowDC dans Graphics.Win32.Window . Cela retourne un IO HDC . Il existe une fonction bitblt dans Graphics.Win32.GDI.Graphics2D . Cette fonction prend un HDC avec un tas de INT qui correspondent vraisemblablement aux arguments qu’il prend en C ++ .

Malheureusement, je n’ai pas de machine Windows à scope de main, donc je ne peux pas écrire le code réel. Vous devrez déterminer comment utiliser les fonctions de l’API Win32 vous-même, ce qui peut être un peu fastidieux.

Si vous le faites, ce serait génial si vous le preniez en compte dans une bibliothèque et que vous le montiez sur Hackage – Windows ne reçoit généralement pas beaucoup d’amour dans Haskell Land (comme je le montre moi-même: P). serait reconnaissant pour un moyen facile de prendre des captures d’écran.