📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm1::CaptureWinImage(TRect rect)
{
pBitmap->Width = rect.Right - rect.Left;
pBitmap->Height = rect.Bottom - rect.Top;
// 从截获的桌面图像中获取指定区域的图像
HDC ScreenDC= GetDC(0);
try
{
StretchBlt(pBitmap->Canvas->Handle, 0, 0,
pBitmap->Width, pBitmap->Height, ScreenDC,
rect.Left, rect.Top,
pBitmap->Width,
pBitmap->Height,
SRCCOPY);
}
__finally{
ReleaseDC(0, ScreenDC);
}
}
//---------------------------------------------------------------------------
void TForm1::CaptureDesktop(HDC ImageHandle)
{
HDC hdc = GetDC(0);
BitBlt(ImageHandle,0,0,Screen->Width,
Screen->Height,hdc,0,0,SRCCOPY);
ReleaseDC(0, hdc);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CaptureScreen1Click(TObject *Sender)
{
Form1->Visible=false;
//调整Image对象的长宽
Image1->Width=Screen->Width;
Image1->Height=Screen->Height;
//等待屏幕刷新
Sleep(1000);
//拷贝截获的图像
CaptureDesktop(Image1->Canvas->Handle);
//重新显示窗体
Form1->Visible=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CaptureWindow1Click(TObject *Sender)
{
TRect rect;
Form1->Visible=false;
Sleep(1000);
// 获取当前活动的窗体句柄
HWND WinHandle =GetTopWin();
// 获取此窗口的尺寸
GetWindowRect(WinHandle, &rect);
// 截获窗体图像
CaptureWinImage(rect);
// 将工作变量pBitmao中的图像拷贝到Image1中
Image1->Width = pBitmap->Width;
Image1->Height = pBitmap->Height;
Image1->Picture->Bitmap = pBitmap;
Form1->Visible=true;
}
//---------------------------------------------------------------------------
HWND TForm1::GetTopWin(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 NULL;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
pBitmap = new Graphics::TBitmap();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
pBitmap->Free();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Capture1Click(TObject *Sender)
{
Form1->Visible=false;
// 初始化
Form2->IsRegionSelected=false;
Image1->Width=Screen->Width;
Image1->Height=Screen->Height;
Form2->OriginPt.x = Form2->MovePt.x =0;
Form2->OriginPt.y = Form2->MovePt.y =0;
Sleep(500);
// 在Form2的Image组件中填充截获到的桌面图像
CaptureDesktop(Form2->Image1->Canvas->Handle);
Form2->Visible=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SaveAs1Click(TObject *Sender)
{
SaveDialog1->FileName = "NewMap1.bmp";
if(SaveDialog1->Execute())
{
Image1->Picture->SaveToFile(SaveDialog1->FileName);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Exit1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -