사용자 콤퓨터가 어떤 상태인지에 맞춰 스크린샷(화면캡쳐) 이미지 저장하기.
//---------------------------------------------------------------------------
bool __fastcall TForm1::isWorkstationLocked()
{
HDESK hDesktop = OpenDesktopW(L"default", 0, FALSE, DESKTOP_SWITCHDESKTOP);
if (hDesktop != 0)
{
bool result = !SwitchDesktop(hDesktop);
CloseDesktop(hDesktop);
return result;
}
return false;
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::screenCapture_myApplication(String stFileName)
{
TImage *ImageCAPTURE = new TImage(NULL) ;
TJPEGImage *jpgImage = new TJPEGImage() ;
bool bl_return ;
bl_return = true ;
// Check if the system locked
bool bl_isLocked = this->isWorkstationLocked() ; // 잠금모드인지 확인.
try {
if (bl_isLocked) { // 잠금 상태면 내 프로그램 이미지만 저장.
HDC hdHandle = GetWindowDC(this->Handle) ;
ImageCAPTURE->Width = this->Width ;
ImageCAPTURE->Height = this->Height ;
ImageCAPTURE->Picture->Bitmap = NULL ;
BitBlt(ImageCAPTURE->Canvas->Handle, 0, 0, this->Width, this->Height, hdHandle, 0, 0, SRCCOPY) ;
} else { // 사용 중인 상태면 전체 스크린 이미지 저장 (만약 화면 잠금상태에서 아래코드가 작동되면 하얀(또는 까만) 이미지만 자장됨.)
ImageCAPTURE->Width = Screen->Width ;
ImageCAPTURE->Height = Screen->Height ;
ImageCAPTURE->Picture->Bitmap = NULL ;
BitBlt(ImageCAPTURE->Canvas->Handle, 0, 0, Screen->Width, Screen->Height, GetDC(0), 0, 0, SRCCOPY) ;
} ;
jpgImage->CompressionQuality = 100 ;
jpgImage->Assign(ImageCAPTURE->Picture->Bitmap) ;
jpgImage->SaveToFile(stFileName) ;
} catch(...) {
bl_return = false ;
} ;
delete ImageCAPTURE ;
delete jpgImage ;
return bl_return ;
}
// 사용 방법
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String stFilename ;
stFilename = "myScreenshot_" + Now().FormatString("yyyy_mm_dd-hhnnss") + ".jpg" ;
this->myScreenCapture( stFilename ) ;
}
//---------------------------------------------------------------------------
|