Window Screen Capture with API Functions


It is easy to do a screen capture of the entire window, including titlebar, menubar and frames. This method uses the API call GetDCEx from user32.dll. API functions from gdi32.dll are used to complete the task.

'nomainwin
savefile$=""    'save file name
WindowWidth=600:WindowHeight=400
menu #1, "&File", "&Save As...",[saveAs],"E&xit",[quit]
button #1, "Save Capture", [saveAs],UL,10,10,100,30
graphicbox #1.g, 10, 50, 500, 270
open "Window Screen Capture" for window as #1
    #1 "trapclose [quit]"
    #1.g "down; backcolor yellow; place 10 10; boxfilled 100 100;flush"

    hWindow=hwnd(#1)

    '_DCX_PARENTCLIP - Uses the visible region of the parent window.
    'The parent's WS_CLIPCHILDREN and CS_PARENTDC style bits are ignored.
    'The origin is set to the upper-left corner of the window identified by hWnd.

    '_DCX_WINDOW - Returns a DC that corresponds to the window rectangle
    'rather than the client rectangle.

    flags=_DCX_PARENTCLIP or _DCX_WINDOW
    calldll #user32, "GetDCEx",hWindow as ulong, 0 as ulong, flags as long, hDC as ulong
    CallDLL #gdi32, "CreateCompatibleDC",hDC as ulong, hDst as ulong

    CallDLL #gdi32, "CreateCompatibleBitmap", _
        hDC as ulong, WindowWidth as long, WindowHeight as long,_
        hBitmap as ulong

    CallDLL #gdi32, "SelectObject", _
        hDst as ulong, hBitmap as ulong, oldObj as ulong

    CallDll #gdi32, "BitBlt", hDst as ulong,0 as long, 0 as long,_
    DisplayWidth as long, DisplayHeight as long, hDC as long, 0 as long, 0 as long,_
    _SRCCOPY as long, re as long

    'select original bmp into hDst DC
    CallDLL #gdi32, "SelectObject", hDst as ulong, oldObj as ulong, re as ulong

    'the screen capture is displayed in a graphicbox for demonstration purposes
    loadbmp "capture",hBitmap
    #1.g "horizscrollbar on; vertscrollbar on"
    #1.g "discard; cls; drawbmp capture 0 0;flush"
    wait

[quit]
    if hBitmap<>0 then
        CallDll #gdi32,"DeleteObject",hBitmap as ulong, re as long
        unloadbmp ("capture")
    end if
    CallDLL #gdi32, "DeleteDC", hDst as ulong,result as long
    CallDll #user32, "ReleaseDC",hWindow as ulong, hDC as ulong, r as long
    close #1:end


[saveAs]
    filedialog "Save As... ","*.bmp",savefile$
    if savefile$="" or hBitmap=0 then
        notice "Not able to save."
        wait
    end if
    b$=".bmp"
    if lower$(right$(savefile$,4))<>b$ then savefile$=savefile$+b$
    bmpsave "capture",savefile$
wait


Home

Source Code

Utilities

Internet

Games

Graphics

Media Demos

Snippets

DLL's

API Resources

Freeware

LB 4 Companion

Mastering LB 3

LB Workshop

Game Workshop

Links

Index