実現したいこと
Windows上で動作する簡単な画面キャプチャーソフトを作りたいが
どうしてもディスプレイ設定の拡大縮小とレイアウトで150%とかにしていると
左上から2/3ぐらいしか取れていません。
修正方法をお伺いしたいです。
前提
実現したいことにも書いたように
画面をそのままに保存。
もしくは
(ディスプレイ設定の拡大縮小とレイアウトで150%となっていたら150%で保存???)
発生している問題・エラーメッセージ
下記のソースコードでは
ディスプレイ設定の拡大縮小とレイアウトで150%とかにしていると左上から2/3ぐらいしか取れていません。
コメントアウトしている部分はどうにか画面サイズを大きく取ろうとした名残です……
該当のソースコード
C++
1int CMFCApplication8App::GetWindowDpi(HWND hWnd) 2{ 3 HDC hdc = GetDC(hWnd); 4 int dpi = GetDpiForWindow(hWnd); 5 ReleaseDC(hWnd, hdc); 6 return dpi; 7} 8 9int CMFCApplication8App::GetLogicalCoordinate(int physicalCoordinate, int dpi) 10{ 11 // 物理ピクセル座標を論理座標に変換 12 return MulDiv(physicalCoordinate, 96, dpi); 13} 14 15void CMFCApplication8App::GetScreenAndSaveAsJPGInExeDirectory() 16{ 17 // ウィンドウを非表示にする 18 CWnd* pMainWnd = AfxGetApp()->GetMainWnd(); 19 if (pMainWnd) 20 { 21 pMainWnd->ShowWindow(SW_HIDE); 22 } 23 24 int numDisplays = GetSystemMetrics(SM_CMONITORS); // 接続されているディスプレイの数を取得 25 26 for (int i = 0; i < numDisplays; i++) 27 { 28 // ディスプレイごとにモニターハンドルを取得 29 HMONITOR hMonitor = GetMonitor(i); 30 31 // 各ディスプレイの情報を取得 32 MONITORINFOEX monitorInfo; 33 monitorInfo.cbSize = sizeof(MONITORINFOEX); 34 35 if (GetMonitorInfo(hMonitor, &monitorInfo)) 36 { 37 // モニターの座標とサイズを取得 38 int screenWidth = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left; 39 int screenHeight = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top; 40 int screenLeft = monitorInfo.rcMonitor.left; 41 int screenTop = monitorInfo.rcMonitor.top; 42 /* 43 // デバッグ出力にディスプレイの情報を出力 44 TRACE(_T("Display %d - Width: %d, Height: %d, Left: %d, Top: %d\n"), i, screenWidth, screenHeight, screenLeft, screenTop); 45 / 46 // ディスプレイの物理座標系に変換*/ 47 int dpi = GetWindowDpi(m_pMainWnd->m_hWnd); // メインウィンドウのDPIを取得 48 /*int physicalLeft = MulDiv(monitorInfo.rcMonitor.left, dpi, 96); 49 int physicalTop = MulDiv(monitorInfo.rcMonitor.top, dpi, 96); 50 int physicalWidth = MulDiv(monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left, dpi, 96); 51 int physicalHeight = MulDiv(monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top, dpi, 96); 52 / 53 // 物理ピクセル座標を論理座標に変換 54 int scaledScreenWidth = GetLogicalCoordinate(screenWidth, dpi); 55 int scaledScreenHeight = GetLogicalCoordinate(screenHeight, dpi); 56 int scaledScreenLeft = GetLogicalCoordinate(screenLeft, dpi); 57 int scaledScreenTop = GetLogicalCoordinate(screenTop, dpi);*/ 58 59 60 61 // スクリーンキャプチャ用のデバイスコンテキストを作成 62 HDC screenDC = CreateDC(_T("DISPLAY"), NULL, NULL, NULL); 63 64 // CImage オブジェクトを作成して画像を保存 65 CImage screenshot; 66// if (screenshot.Create(scaledScreenWidth, scaledScreenHeight, 32) != S_OK) 67// if (screenshot.Create(physicalWidth, physicalHeight, 32) != S_OK) 68 if (screenshot.Create(screenWidth, screenHeight, 32) != S_OK) 69 { 70 // 画面全体をビットマップにコピー 71 BitBlt(screenshot.GetDC(), 0, 0, screenWidth, screenHeight, screenDC, screenLeft, screenTop, SRCCOPY);
試したこと
チャットGPTさんに聞いたけど解答がループでした……
補足情報(FW/ツールのバージョンなど)
VisualStudio2019で作成しております。
画面そのままで取る方法でも良いのですが
なんかデバイスコンテキスト使うとうまく取れないのです。
お忙しいところ大変申し訳ないのですが修正方法をお伺いしたいです。
なんとググれば良いのでしょうか?

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/11/20 02:41 編集