copy then screen

General FreeBASIC programming questions.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: copy then screen

Post by MrSwiss »

MSDN wrote:Note: GetDeviceCaps reports info that the display driver provides. If the display driver declines to report any info, GetDeviceCaps calculates the info based on fixed calculations. If the display driver reports invalid info, GetDeviceCaps returns the invalid info. Also, if the display driver declines to report info, GetDeviceCaps might calculate incorrect info because it assumes either fixed DPI (96 DPI) or a fixed size (depending on the info that the display driver did and didn’t provide). Unfortunately, a display driver that is implemented to the Windows Display Driver Model (WDDM) (introduced in Windows Vista) causes GDI to not get the info, so GetDeviceCaps must always calculate the info.
This may explain, why the obtained information may be useless, wrong, or whatever ... Depending on whether the result is returned (properly) or calculated with fixed (probably wrong) sizes (e.g. 96 DPI).
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: copy then screen

Post by Tourist Trap »

grindstone wrote:For saving a screenshot of the desktop to a file you have to change some details
grindstone, I've used some size retrieval for better generality as said before. So using your version, I have now one first part using the clipboard image and your part doing so. I'm just wondering if there is no some double work, but I don't really catch all what your part does so I'm leaving it as it, and overall this is working very decently in many parts.

Code: Select all

#Include "fbgfx.bi"
#Include Once "windows.bi"

Declare Function get_clip() As HBITMAP
'- - - - - - - - - - - - - - - - - - - - - - - - 
''0 - Check if anything in the clipboard
''..should use get_clip(CF_BITMAP) ??
If 0=get_clip() Then 
    ? "Please copy an image to clipboard first"
    Sleep : End 0
End If

''1 - Retrieve clipboard bitmap size information
Dim As BITMAP bm  
GetObject (get_clip(), 24, cptr(BITMAP ptr, @bm))
Dim As Integer _w,_h
_w = bm.bmWidth
_h = bm.bmHeight

''2 - Display clipboard bitmap at screen
Dim As BITMAPINFO BitmapInfo
Dim As HDC display_dc    
         display_dc  = GetDC( NULL )
Dim As HDC bitmap_dc      
         bitmap_dc = CreateCompatibleDC( display_dc )
Dim As HBITMAP bitmap     
         bitmap = CreateCompatibleBitmap( display_dc, _w, _h )
Dim As HGDIOBJ null_bitmap 
         null_bitmap = SelectObject( bitmap_dc, bitmap )
bitmap = get_clip()

ScreenRes _w, _h, 32
Dim As FB.image Ptr optr = ImageCreate( _w, _h )

optr->pitch = optr->Width * 4
With BitmapInfo.bmiHeader
    .biSize     = SizeOf(BITMAPINFOHEADER)
    .biWidth    = _w
    .biHeight   = -_h
    .biPlanes   = 1
    .biBitCount = 32
End With 'BitmapInfo.bmiHeader

getdibits( bitmap_dc, _ 
           bitmap, _ 
           0, _ 
           _h, _ 
           optr + 1, _ 
           @BitmapInfo, _ 
           DIB_RGB_COLORS )
SelectObject( bitmap_dc, null_bitmap )

Put (0,0), optr
Draw string (_w\2-8*5, _h-40), "CLIPBOARD IMAGE", rgb(25,50,75)

DeleteDC( display_dc )
DeleteDC( bitmap_dc )
DeleteObject( bitmap )

''3 - Save clipboard bitmap at disk file
If 0=BSave("D:Temp/savedclipboard.bmp", optr) Then
    Draw string (_w\2-7*5, _h-20), "SAVED TO DISK", rgb(25,50,75)
Else
    Draw string (_w\2-7*5, _h-20), "BSAVE FAILURE", rgb(25,50,75)
    Sleep : End 0
End If

ImageDestroy(optr)

Sleep : End -1
'- - - - - - - - - - - - - - - - - - - - - - - - 
Function get_clip() As HBITMAP
    Dim hBmp As HANDLE
    
    If IsClipboardFormatAvailable(CF_BITMAP) = 0 Then
        Return NULL
    EndIf
    If OpenClipboard(0) = 0 Then
        Return NULL
    EndIf
    
    Function = GetClipboardData(CF_BITMAP)
    CloseClipboard()
End Function 'HBITMAP:=get_clip()
 
3622
Posts: 24
Joined: Mar 14, 2015 23:53

Re: copy then screen

Post by 3622 »

I would be interested to know how to do this, and to be able to use the clipboard in general, from Linux. Can anybody point me in the right direction? Many thanks in anticipation.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: copy then screen

Post by MrSwiss »

3622 wrote:I would be interested to know how to do this, and to be able to use the clipboard in general, from Linux. Can anybody point me in the right direction? Many thanks in anticipation.
Well, this above code is using Windows API Functions/Data Structures etc. You'd have to consult documentation on similar stuff in Linux.
This might be depending on the Linux Distribution you're using ... (maybe better, to ask this question in the LINUX section of the Forum).

This thread should actually be moved to the Windows section (likewise).
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: copy then screen

Post by dodicat »

Text is a bit easier to put on the clipboard.
(Windaes)

Code: Select all

#include "Windows.bi"

Sub SaveToClipBoard(Text As String)       'write Text to the clipboard
   Var Num=GlobalAlloc(0,Len(Text)+1)
   Var Chars=GlobalLock(Num)
    Chars= @text[0]
    GlobalUnlock(Num)
    OpenClipboard(0)
    EmptyClipboard()
    SetClipboardData(CF_TEXT,Chars)
    CloseClipboard()
End Sub
Function factorial(num As Long) As String
    Dim  As Ubyte _Mod(0 To 99),_Div(0 To 99)
    For z As Long=0 To 99:_Mod(z)=(z Mod 10+48):_Div(z)=z\10:Next
    Var fact="1",a="",b="",c=""
    Dim As Ubyte n,carry,ai
    For z As Long=1 To num
        a=fact:b=Str(z):Var la =Len(a),lb =Len(b)
        c =String(la+lb,"0")
        For i As Long =la-1 To 0 Step -1
            carry=0:ai=a[i]-48
            For j As Long =lb-1 To 0 Step -1
                n =ai*(b[j]-48)+(c[i+j+1]-48)+carry
                carry =_Div(n):c[i+j+1]=_Mod(n)
            Next j
            c[i]+=carry
        Next i
        fact=Ltrim(c,"0")
    Next z
    Return fact
End Function


SaveToClipboard("Factorial of 5000 = " &factorial(5000))
Print "done"
Sleep


 
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: copy then screen

Post by Tourist Trap »

3622 wrote:I would be interested to know how to do this, and to be able to use the clipboard in general, from Linux. Can anybody point me in the right direction? Many thanks in anticipation.
I have always been a very casual linux user, but as far as I can understand the philosophy some shell command should do the job. Have you tried to launch a shell so, fed with some copy/paste shell instructions?
Ok how to copy/paste via a shell instruction is a good question. I know absolutly not.

Looking around I've found a link that reviews some possible command line tools : http://tips.webdesign10.com/how-to-take ... untu-linux. I don't know if it can help as a first approach.

Please considere also this link http://unix.stackexchange.com/questions ... rd-to-file where usage of xclip allows grabbing clipboard image, then saved to a png file in a rather straighforward way.
Jawade
Posts: 228
Joined: Apr 25, 2008 19:13

Re: copy then screen

Post by Jawade »

Thanks to all. I have a working program, or better, programs. One startprogran, snap.bas and one main program, 14081157.bas, The forst starts the second. The result is good, every snap is saved. Start snap.exe and hear the beep. With the richt-Alt you make a snap, and with the right-Control the program is closed. It worke also if another program is focushed.

Here snap.bas:

Code: Select all


#include "windows.bi"

Beep
Do
  Do
    Sleep 3
  Loop Until GetKeyState(VK_RMENU) < 0 Or GetKeyState(VK_RCONTROL) < 0
  Do: Loop Until GetKeyState(VK_RMENU) > -1
  If GetKeyState(VK_RCONTROL) > -1 Then
    keybd_event 44, 0, 0, 0
    Exec "C:\Users\Jawade\Sources\ClipBTest\14081157.exe", ""
  End If
Loop Until GetKeyState(VK_RCONTROL) < 0: Beep

And here 14081157.bas:

Code: Select all


    #INCLUDE "fbgfx.bi"
    #INCLUDE ONCE "windows.bi"

    DECLARE FUNCTION get_clip() AS HBITMAP
    DECLARE FUNCTION set_clip(bitmap AS STRING) AS HBITMAP

    Dim As String bb
    Dim As Integer w
    Dim As Integer _h
    ScreenInfo w,_h
    SCREENRES w, _h, 32

    DIM AS BITMAPINFO BitmapInfo
    DIM AS HDC display_dc      = GetDC( NULL )
    DIM AS HDC bitmap_dc       = CreateCompatibleDC( display_dc )
    DIM AS HBITMAP bitmap      = CreateCompatibleBitmap( display_dc, w, _h )
    DIM AS HGDIOBJ null_bitmap = SelectObject( bitmap_dc, bitmap )

    bitmap = get_clip()

    DIM AS fb.image PTR optr = IMAGECREATE( w, _h )
    optr->pitch = optr->WIDTH * 4
    WITH BitmapInfo.bmiHeader
       .biSize     = SIZEOF(BITMAPINFOHEADER)
       .biWidth    = w
       .biHeight   = -_h
       .biPlanes   = 1
       .biBitCount = 32
    END With

    getdibits( bitmap_dc, bitmap, 0, _h, optr + 1, @BitmapInfo, DIB_RGB_COLORS )

    SelectObject( bitmap_dc, null_bitmap )
    DeleteDC( display_dc )
    DeleteDC( bitmap_dc )
    DeleteObject( bitmap )

    PUT(0, 0), optr
    IMAGEDESTROY(optr)

    bb = Mid$(Date$, 7, 4) + Left$(Date$, 2)
    bb = bb + Mid$(Date$, 4, 2)
    bb = bb + Left$(Time$, 2)
    bb = bb + Mid$(Time$, 4, 2)
    bb = bb + Mid$(Time$, 7, 2)
    bb = bb + Mid$(Str$(Timer), InStr(Str$(Timer), ".") + 1, 2)
    bb = bb + ".bmp"
    BSave bb, 0

    FUNCTION set_clip(bitmap AS STRING) AS HBITMAP
            DIM hBmp AS HANDLE
            hBmp = LoadImage(0, bitmap, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)
            OpenClipboard(0)
            EmptyClipboard()
            SetClipboardData(CF_BITMAP, CopyImage(hBmp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG))
            CloseClipboard()
            RETURN NULL
    END FUNCTION

    FUNCTION get_clip() AS HBITMAP
            DIM hBmp AS HANDLE
            IF IsClipboardFormatAvailable(CF_BITMAP) = 0 THEN
                RETURN NULL
            ENDIF
            IF OpenClipboard(0) = 0 THEN
                RETURN NULL
            ENDIF
            FUNCTION = GetClipboardData(CF_BITMAP)
            CloseClipboard()
    END Function 

Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: copy then screen

Post by Tourist Trap »

Jawade wrote: I have a working program
Well done. Thanks for the topis question, I will also be able to use clipboard now.
3622 wrote:I would be interested to know how to do this, and to be able to use the clipboard in general, from Linux.
I have made no test but GTK+ is providing clipboard functions that would probably fit best on linux : https://developer.gnome.org/gtk2/stable ... kClipboard.
Post Reply