저장버튼 눌렀을 때 태우면 된다.
창에 따라서 다르게 파라미터를 넘겨줌
이건 글로벌이나 헤더에 선언
enum {
ParamSetting = 0,
AdminSetting,
MasterSetting,
MotionSetting
};
이건 세팅할 때 태우면 됨
void CDlgSetting::SaveDisplayCaptureSetting(int nType)
{
// 캡쳐후 저장하는 함수
// 1. 화면의 DC(디바이스 컨텍스트)를 가져옴
HDC h_screen_dc = ::GetDC(NULL);
if (!h_screen_dc) return;
// 2. 화면 해상도 정보를 얻음
int width = ::GetDeviceCaps(h_screen_dc, HORZRES);
int height = ::GetDeviceCaps(h_screen_dc, VERTRES);
// 3. DIB (Device-Independent Bitmap) 정보를 설정
BITMAPINFO dib_define = {};
dib_define.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
dib_define.bmiHeader.biWidth = width;
dib_define.bmiHeader.biHeight = -height; // 좌표계를 반전하여 올바른 방향으로 저장
dib_define.bmiHeader.biPlanes = 1;
dib_define.bmiHeader.biBitCount = 24;
dib_define.bmiHeader.biCompression = BI_RGB;
dib_define.bmiHeader.biSizeImage = ((width * 24 + 31) / 32) * 4 * height;
BYTE* p_image_data = NULL;
// 4. DIB 섹션 생성 (화면 이미지를 저장할 메모리 공간 확보)
HBITMAP h_bitmap = ::CreateDIBSection(h_screen_dc, &dib_define, DIB_RGB_COLORS, (void**)&p_image_data, NULL, 0);
if (!h_bitmap) {
::ReleaseDC(NULL, h_screen_dc);
return;
}
// 5. 가상 DC를 생성하여 화면을 캡처할 준비
HDC h_memory_dc = ::CreateCompatibleDC(h_screen_dc);
if (!h_memory_dc) {
DeleteObject(h_bitmap);
::ReleaseDC(NULL, h_screen_dc);
return;
}
// 6. 가상 DC에 비트맵 연결
HBITMAP h_old_bitmap = (HBITMAP)::SelectObject(h_memory_dc, h_bitmap);
// 7. 화면 내용을 캡처 (BitBlt를 이용해 가상 DC에 복사)
if (!::BitBlt(h_memory_dc, 0, 0, width, height, h_screen_dc, 0, 0, SRCCOPY)) {
::SelectObject(h_memory_dc, h_old_bitmap);
DeleteDC(h_memory_dc);
DeleteObject(h_bitmap);
::ReleaseDC(NULL, h_screen_dc);
return;
}
// 8. 원래 비트맵으로 되돌린 후, 가상 DC 해제
::SelectObject(h_memory_dc, h_old_bitmap);
DeleteDC(h_memory_dc);
::ReleaseDC(NULL, h_screen_dc);
// 9. BMP 파일의 헤더 정보 설정
BITMAPFILEHEADER dib_format_layout = {};
dib_format_layout.bfType = *(WORD*)"BM";
dib_format_layout.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dib_define.bmiHeader.biSizeImage;
dib_format_layout.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// 10. 설정 유형에 따라 폴더 및 파일명 설정
CString strDetailSetting;
switch (nType) {
case ParamSetting: strDetailSetting = _T("Parameter Setting"); break;
case AdminSetting: strDetailSetting = _T("Admin Setting"); break;
case MasterSetting: strDetailSetting = _T("Master Setting"); break;
case MotionSetting: strDetailSetting = _T("Motion Setting"); break;
default: strDetailSetting = _T("None"); break;
}
// 11. 현재 날짜 및 시간 가져오기
CTime time = CTime::GetCurrentTime();
CString strYMD = time.Format(_T("%Y%m%d"));
CString strDTime = time.Format(_T("%Y%m%d_%H%M%S"));
// 12. 캡처 이미지 저장 폴더 생성
CString strPath;
strPath.Format(_T("C:\\glim\\Setting Change Capture\\%s\\%s\\"), strDetailSetting, strYMD);
SHCreateDirectoryEx(NULL, strPath, NULL); // 폴더가 없으면 자동 생성
// 13. 파일명 생성
CString strFilePath;
strFilePath.Format(_T("%s%s_%s_Display Capture.bmp"), strPath, strDTime, strDetailSetting);
// 14. BMP 파일 저장
FILE* p_file = _wfopen(strFilePath, _T("wb"));
if (!p_file) {
//AfxMessageBox(_T("파일을 저장할 수 없습니다."));
DeleteObject(h_bitmap);
return;
}
// 15. 파일에 헤더 및 이미지 데이터 기록
fwrite(&dib_format_layout, 1, sizeof(BITMAPFILEHEADER), p_file);
fwrite(&dib_define, 1, sizeof(BITMAPINFOHEADER), p_file);
fwrite(p_image_data, 1, dib_define.bmiHeader.biSizeImage, p_file);
fclose(p_file);
// 16. 사용한 비트맵 해제
DeleteObject(h_bitmap);
}
저장할 때 이렇게 호출
'개발공부 > C++' 카테고리의 다른 글
[C++ MFC] 로그 남길 때 동시 접근 문제, CCriticalSection 사용 (0) | 2025.03.13 |
---|---|
test dword ptr [eax],eax ; probe page. 오류 해결 (0) | 2025.03.10 |
[C++ MFC] 문자열 _T("")와 L"" (0) | 2024.12.26 |
[C++] 폴더 없을 때 폴더 생성 (0) | 2024.12.17 |
[C++ ] Systemtime : 시스템 시간 날짜 얻어오기 (0) | 2024.12.17 |