我最近用了好几个抓屏软件,其中他们都有一个capture control的功能,也就是能够抓取屏幕中某窗口中的按键,菜单,我只知道通过下面:
GetCursorPos()
WindowFromPoint()
GetWindowRect()
这三个函数可以知道控件的位置,然后将全屏幕抓下来,最后用BitBlt函数将控件所在全屏幕图形中位置的控件图形提取出来完成了。但是在这些抓屏软件中这个功能你可以选择要抓取的控件,在被选中的控件周围就会有一个表示被选中的框,你就知道这个框被选中了,我不明白这个被选中的功能是如何做的,希望知道的高手能讲一讲。谢谢。

解决方案 »

  1.   

    to cg1120:
      希望能讲详细点。
      

  2.   

    我用BCB写了一个,你看看吧://---------------------------------------------------------------------------#include <vcl.h>
    #pragma hdrstop#include "untShow.h"
    #include "untCapture.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm2 *Form2;
    //---------------------------------------------------------------------------
    __fastcall TForm2::TForm2(TComponent* Owner)
        : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm2::CaptureRegion(void)
    {
        if((MovePt.y-Origin.y)!=0)
        {
          int KAWidth=abs(MovePt.x-Origin.x);
          int KAHeight=abs(MovePt.y-Origin.y);
          int Orgx=Origin.x,Orgy=Origin.y;
          if (MovePt.y < Origin.y) Orgy = MovePt.y;
          if (MovePt.x < Origin.x) Orgx = MovePt.x;
    // Copy the selected part of the Desktop to Form1 Image
          StretchBlt(Form1->Image1->Canvas->Handle,
            0,0,
            KAWidth,
            KAHeight,
            Image1->Canvas->Handle,
            Orgx, Orgy,
            KAWidth,
            KAHeight,
            SRCCOPY);
    // Adjust Form1 Image size
          Form1->Image1->Width=abs(MovePt.x-Origin.x);
          Form1->Image1->Height=abs(MovePt.y-Origin.y);
        }
        else
        {
          Form1->Image1->Picture->Bitmap->Canvas->Brush->Color=clWhite;
          Form1->Image1->Picture->Bitmap->Canvas->FillRect(Canvas->ClipRect) ;
        }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm2::DrawRect(TPoint TopLeft, TPoint BottomRight, TPenMode KAMode)
    {
      Canvas->Pen->Mode = KAMode;
      Canvas->Pen->Width = 2;
      Canvas->Rectangle(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y);
    }
    void __fastcall TForm2::Image1MouseDown(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y)
    {
        IsRegionSelected = true;
        Origin = Point(X,Y);
        MovePt = Origin;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm2::FormCreate(TObject *Sender)
    {
        SetBounds( 0, 0, Screen->Width, Screen->Height );    
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm2::Image1MouseMove(TObject *Sender, TShiftState Shift,
          int X, int Y)
    {
        if(IsRegionSelected)
        {
            DrawRect(Origin, MovePt, pmNotXor);
            MovePt = Point(X, Y);
            MovePt.x=MovePt.x+Image1->Left;
            MovePt.y=MovePt.y+Image1->Top;
            DrawRect(Origin, MovePt, pmNotXor);
        }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm2::Image1MouseUp(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y)
    {
        if(IsRegionSelected)
        {
            MovePt = Point(X, Y);
            IsRegionSelected=false;
            CaptureRegion();
        }
        Form1->Visible=true;
        Form2->Visible=false;
    }
    //---------------------------------------------------------------------------
      

  3.   

    {***************************************************************
     * 方 法 名  : g_fun_SnatchScreen
     * 编写目的   :
     * 作    者  : 黄仁光
     * 参    数  : HWnd:THandle
     * 结    果  : None
     * 编写日期   :2002年12月31日
     ****************************************************************}
    procedure g_fun_SnatchScreen(HWnd:THandle);
    var
      TheRECT: TRect;
      TheWidth: integer;
      TheHeight: integer;
      SourceDC: integer;
      DestDC: integer;
      BHandle: integer;
      Wnd: integer;
    begin
      GetWindowRect(HWnd, therect);
      thewidth := therect.Right - therect.left;
      TheHeight := therect.Bottom - therect.Top;
      sourcedc := CreateDC('Display', 0, 0, 0);
      DestDC := CreateCompatibleDC(sourcedc);
      BHandle := CreateCompatibleBitmap(sourcedc, thewidth, TheHeight);
      SelectObject(DestDC, BHandle);
      BitBlt(DestDC, 0, 0, TheWidth, TheHeight, SourceDC, TheRECT.Left, TheRECT.Top, MERGECOPY);
      wnd := HWnd;
      OpenClipboard(Wnd);
      EmptyClipboard;
      SetClipboardData(2, BHandle);
      CloseClipboard;
      DeleteDC(DestDC);
      ReleaseDC(BHandle, SourceDC);
    end;
      

  4.   

    还有一部分,前面那个用来现实,这个用来抓:
    #include <vcl.h>
    #include <math.h>
    #pragma hdrstop#include "untCapture.h"
    #include "untShow.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    Graphics::TBitmap *KABitmap;
    //---------------------------------------------------------------------------
    //捕获整个窗口
    void __fastcall TForm1::CaptureDesktop(HDC DesImage)
    {
        HDC hdc = GetDC(0);
        BitBlt(DesImage,0,0,Screen->Width,Screen->Height,hdc,0,0,SRCCOPY);
        ReleaseDC(0,hdc);
    }
    //---------------------------------------------------------------------------
    //捕获活动窗口
    HWND __fastcall TForm1::GetTopWndHandle(void)
    {
        HWND hwnd = GetTopWindow(0);
        while(hwnd != 0)
        {
            if(IsWindowVisible(hwnd) && (GetWindowLong(hwnd,GWL_EXSTYLE) & WS_EX_TOPMOST) == 0)
            {
                if(IsIconic(hwnd))
                    hwnd = GetDesktopWindow();
                return hwnd;
            }
            hwnd = GetNextWindow(hwnd,GW_HWNDNEXT);
        }
        return 0;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::CaptureWndImage(TRect rect)
    {
        // First set the size of the Bitmap.
        KABitmap->Width = rect.Right - rect.Left;
        KABitmap->Height = rect.Bottom - rect.Top;
        // Get the seleted part of the Desktop Image
        //    ScreenDC = GetDC( 0 );
        HDC  ScreenDC= GetDC(0);
        try
        {
            StretchBlt(KABitmap->Canvas->Handle, 0, 0,
                     KABitmap->Width, KABitmap->Height, ScreenDC,
                     rect.Left, rect.Top,
                     KABitmap->Width,
                     KABitmap->Height,
                     SRCCOPY);
        }
        __finally
        {
            ReleaseDC(0, ScreenDC);
        }
    }
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
        KABitmap = new Graphics::TBitmap();
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormDestroy(TObject *Sender)
    {
        KABitmap->Free();
    }
    //---------------------------------------------------------------------------void __fastcall TForm1::Save1Click(TObject *Sender)
    {
        int i=1;
        SaveDialog1->FileName = "Captured1.bmp";
        while (FileExists(SaveDialog1->FileName))
        {
          i++;
          SaveDialog1->FileName = "Captured"+IntToStr(i)+".bmp";
        }
        if(SaveDialog1->Execute())
        {
          Image1->Picture->SaveToFile(SaveDialog1->FileName);
        }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::N1Click(TObject *Sender)
    {
        Form1->Visible=false;
        // Initializations
        Image1->Width=Screen->Width;
        Image1->Height=Screen->Height;    // Give the Display time to refresh
        Sleep(2000);    // Copy the Desktop to Image1
        CaptureDesktop(Image1->Canvas->Handle);    // Show Form1
        Form1->Visible=true;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::N2Click(TObject *Sender)
    {
        TRect rect;
        Application->Minimize();
        // Give some time to refresh and to use to select a window
        Sleep(3000);
        // Get the handle of the top active window
        HWND WinHandle =GetTopWndHandle();
        // Get the size of this window
        GetWindowRect(WinHandle, &rect);
        // Put the Image of the top active window in the KABitmap
        CaptureWndImage(rect);
        // Now Copy the KABitmao to Image1
        Image1->Width = KABitmap->Width;
        Image1->Height = KABitmap->Height;
        Image1->Picture->Bitmap = KABitmap;
        Application->Restore();
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::N3Click(TObject *Sender)
    {
        Form1->Visible=false;
        // Initializations
        Image1->Width=Screen->Width;
        Image1->Height=Screen->Height;
        Form2->Origin.x = Form2->MovePt.x =0;
        Form2->Origin.y = Form2->MovePt.y =0;
        Form2->IsRegionSelected=false;    // Give the Display time to refresh
        Sleep(500);    // Copy the Desktop Image to Form2 Image
        CaptureDesktop(Form2->Image1->Canvas->Handle);
        // Show Form2
        Form2->Visible=true;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Exit1Click(TObject *Sender)
    {
        Application->Terminate();
    }
    //---------------------------------------------------------------------------