Announcement

Collapse
No announcement yet.

Easiest way to copy the content of the active window to the clipboard ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Easiest way to copy the content of the active window to the clipboard ?

    Dear PB users,

    For my application I need to add a button in order to copy the content of active window to the clipboard.

    Any idea ?

    Thanks
    Jean-Pierre
    Jean-Pierre LEROY

  • #2
    Key_event(), shift + printscreen?
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      This code will capture a Window by it's handle and save it to disk. You could then read it as a memory bitmap (GRAPHIC BITMAP LOAD) and then copy the bitmap to the clipboard (CLIPBOARD SET BITMAP).
      Code:
      Sub CaptureWindow(ByVal hWin As Dword)
        Local hDC   As Dword
        Local hMDC  As Dword
        Local hMBmp As Dword
        Local rc    As RECT
        Local bm    As Bitmap
        Local bmi   As BITMAPINFO
        Local bmpFH As BITMAPFILEHEADER
        Local wb    As Dword
        Local hf    As Long
      
        GETWINDOWRECT hWin, rc
      
        hDC = CreateDC("DISPLAY", ByVal %Null, ByVal %Null, ByVal %Null)
        hMDC = CreateCompatibleDC(hDC)
        bmi.bmiHeader.biSize = SizeOf(bmi.bmiHeader)
        bmi.bmiHeader.biWidth = (rc.nRight - rc.nLeft)
        bmi.bmiHeader.biHeight = (rc.nBottom - rc.nTop)
        bmi.bmiHeader.biPlanes = 1
        bmi.bmiHeader.biBitCount = 32
        bmi.bmiHeader.biCompression = %BI_RGB
        hMBmp = CreateDIBSection(hMDC, bmi, %DIB_RGB_COLORS, %NULL, 0, 0)
      
        SelectObject hMDC, hMBmp
        GetObject hMBmp, SizeOf(bm), bm
        BitBlt(hMDC, 0, 0, bm.bmWidth, bm.bmHeight, hDC, rc.nLeft, rc.nTop, %SRCCOPY)
      
        wb = bmi.bmiHeader.biWidth + (bmi.bmiHeader.biWidth Mod 4)
        bmpFH.bfType = Cvi("BM")
        bmpFH.bfOffBits = 54
        bmpFH.bfSize = SizeOf(bmpFH) + (wb * bm.bmHeight)
      
        hf = FreeFile
        Open "temp.bmp" For Output As #hf
        Print #hf, bmpFH; bmi.bmiHeader; Peek$(bm.bmBits, bm.bmWidthBytes * bm.bmHeight);
        Close #hf
      
        DeleteDC(hDC)
        DeleteDC(hMDC)
        DeleteObject(hMBmp)
      End Sub
      Sincerely,

      Steve Rossell
      PowerBASIC Staff

      Comment


      • #4
        Capture Dialog to Clipboard

        Short and sweet:

        'Primary Code:
        Code:
        keybd_event(%VK_SnapShot, 1, 0, 0)  'place active dialog on clipboard
        Code:
        'Compilable Example:
        'press the button and then paste the image from the clipboard into
        'another application such as MSPaint.
        #Compile Exe
        #Dim All
        #Include "Win32API.inc"
        Global hDlg as Dword
        Function PBMain() As Long
           Dialog New Pixels, 0, "Dialog Capture",300,300,200,200, %WS_OverlappedWindow To hDlg
           Control Add Button, hDlg, 100,"Push", 50,10,100,20
           Dialog Show Modal hDlg Call DlgProc
        End Function
        CallBack Function DlgProc() As Long
           If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
              keybd_event(%VK_SnapShot, 1, 0, 0)  'place active dialog on clipboard
           End If
        End Function
        Last edited by Gary Beene; 29 Nov 2010, 10:03 AM.

        Comment


        • #5
          Originally posted by Steve Rossell View Post
          This code will capture a Window by it's handle and save it to disk.
          Correct me if I'm wrong but it looks to me that your example could be easily mod'ed and used as a substitute for CC's PCOPY with an effectively unlimited number of pages to save.
          There are no atheists in a fox hole or the morning of a math test.
          If my flag offends you, I'll help you pack.

          Comment


          • #6
            >Correct me if I'm wrong

            Ok, you're wrong.

            PCOPY just copies a block of bytes.. which you can do with any one of about twelve different mechanisms. (You can start with GetConsoleScreenBuffer()).

            Also, PCOPY is proprietary... you store things by a proprietary handle ("page number") and can only use that handle with other PB/CC functions.

            Steve's (very nice) code converts what's on the screen to a standard image format (BMP).

            And when you get to part deux of the problem....

            GRAPHIC BITMAP LOAD returns a proprietary handle, too; but the CLIBOARD SET BITMAP function uses that proprietary handle....of course, you can always save to disk and then load the image using LoadBitmap() or LoadImage() if you need a Windows image handle, with which you could do lots of stuff.

            MCM
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              > Ok, you're wrong.

              I don't think so. Wether or not it's copying byte block(s) or an entire graphics page, the end result will be the same.
              There are no atheists in a fox hole or the morning of a math test.
              If my flag offends you, I'll help you pack.

              Comment


              • #8
                PCOPY provides for saving a "screen" in a format suitable for later display using PB/CC intrinsics, and only PB/CC intrinsics.

                Steve's code provides for saving a "screen" in a standard image format (a bitmap file) from which you can do all sorts of cool stuff.

                Two different animals; two totally different purposes and uses. That is, neither is a tit-for-tat "alternative" to the other.

                Either might serve equally well in a particular application, but there will be times only one will be "right." Application details beyond "storing on clipboard" not shown.

                MCM
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  Well, thank you for finally agreeing with me.
                  There are no atheists in a fox hole or the morning of a math test.
                  If my flag offends you, I'll help you pack.

                  Comment


                  • #10
                    Michael, Gary, thank you for your idea about keybd_event(); it works well except when the end user has already installed a screen capture software on his PC !

                    Steve, I decided to use your code; I just added 4 lines of codes and it works perfectly

                    Code:
                    Local lHandleBMP     As Long
                    Local lTempFileNameA As Asciiz*%MAX_PATH
                    GetTempFileName(gTempPath,$TemporaryFileNamePrefixString,0,lTempFileNameA)
                    CaptureWindow(hWndForm, lTempFileNameA)    
                    Graphic Bitmap Load lTempFileNameA, 0, 0 To lHandleBMP
                    Clipboard Reset
                    Clipboard Set Bitmap lHandleBMP 
                    Graphic Bitmap End
                    In my specific case I don't really need a copy of the screen capture on disk; not sure if there is any other direct solution (without the disk file) ?

                    Thanks,
                    Jean-Pierre
                    Jean-Pierre LEROY

                    Comment


                    • #11
                      Jean-Pierre,

                      I have screen capture software loaded too, but the code still works. Can you say a bit more about what is preventing the code I posted from working, perhaps the error you're getting?

                      I use the code in some of my own apps, so if there's a shortcoming, I'd like to know.

                      Comment


                      • #12
                        my specific case I don't really need a copy of the screen capture on disk; not sure if there is any other direct solution (without the disk file) ?
                        The answer is yes. But that's not complete.

                        The complete answer is, "yes you can get around using the disk file but it is not worth the effort."

                        BTW, you forgot to delete the file on disk from your temp folder when you were done with it. And you wonder why your temp folder has so many files in it?
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          Jean-Pierre,

                          I have screen capture software loaded too, but the code still works. Can you say a bit more about what is preventing the code I posted from working, perhaps the error you're getting?

                          I use the code in some of my own apps, so if there's a shortcoming, I'd like to know
                          In my specific case some end users are using a screen capture software that use the "PrintScreen" key by default; with you 'Compilable Example' when I press the button "Push", the other software is launched automatically and nothing is copied in the clipboard.

                          JP
                          Jean-Pierre LEROY

                          Comment


                          • #14
                            I found two snippets on the forum for that purpose:

                            credit to Adam J. Drake
                            http://www.powerbasic.com/support/pb...ad.php?t=14092

                            credit to Gary Peek
                            http://www.powerbasic.com/support/pb...ad.php?t=10370

                            Thanks,
                            JP
                            Jean-Pierre LEROY

                            Comment

                            Working...
                            X