我正在尝试创建一个应用程序,其中包含正在运行的应用程序的屏幕截图 我使用了这个答案中的代码https://stackoverflow.com/a/11966931/2064205。 我有一个createBMPFile函数的问题,它保存黑色图像。我试图在C ++中创建类似的应用程序,也有同样的问题。 我是Haskell的新手,不注意代码。
import Graphics.Win32.Window
import Graphics.Win32.GDI.Bitmap
import Graphics.Win32.GDI.HDC
import Graphics.Win32.GDI.Graphics2D
import Graphics.Win32.Window.ForegroundWindow
import Graphics.Win32.GDI.Types (HWND)
import Foreign.Ptr
getValueFromMaybe :: Maybe HWND -> HWND
getValueFromMaybe mx =
case mx of
Just value -> value
Nothing -> nullPtr
main = do
desktop <- findWindowByName "slack" -- Grab the Hwnd of the window with title "slack", GetDC 0, GetDC NULL etc all work too
setForegroundWindow . getValueFromMaybe $ desktop
desktop <- findWindowByName "slack"
hdc <- getWindowDC desktop -- Get the dc handle of the desktop
(x,y,r,b) <- getWindowRect . getValueFromMaybe $ 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.