im just started a POC for my remote desk protocol project and get in trouble when runing my code.
the screen capturing is working but only partely, when i display the image im only seeing the top right corner of the screen.
the image attached is what im seeing in the pop up window, has you can see its not full
i tried to move the window around to see if it’s just not displaying it all, playing with the functions paras and all other things for hours but still coudlnt get it to work. im suspecting that when capturing the image it doesnt get the whole screen but thats just a guess.
#include <iostream>
#include <Windows.h>
void InitializeCapture();
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HBITMAP hBitmap = NULL;
HDC hMemoryDC = NULL;
HWND hwndMain = NULL;
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nShowCmd)
{
// Register window class
std::string str = "Sample Window Class";
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create window
hwndMain = CreateWindowEx(
0,
CLASS_NAME,
CLASS_NAME,
WS_OVERLAPPEDWINDOW,
0, 0,
GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
NULL,
NULL,
hInstance,
NULL);
if (hwndMain == NULL)
{
return 0;
}
// Initialize capture and display
InitializeCapture();
ShowWindow(hwndMain, nShowCmd);
UpdateWindow(hwndMain);
// Message loop
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Cleanup
if (hBitmap)
DeleteObject(hBitmap);
if (hMemoryDC)
DeleteDC(hMemoryDC);
return 0;
}
void InitializeCapture()
{
HDC hScreenDC = GetDC(NULL); // Get the screen device context
hMemoryDC = CreateCompatibleDC(hScreenDC); // Create a memory device context
int width = GetSystemMetrics(SM_CXSCREEN); // Screen width
int height = GetSystemMetrics(SM_CYSCREEN); // Screen height
hBitmap = CreateCompatibleBitmap(hScreenDC, width, height); // Create a bitmap compatible with screen DC
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap); // Select the bitmap into memory DC
BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
SelectObject(hMemoryDC, hOldBitmap); // Restore the old bitmap
ReleaseDC(NULL, hScreenDC); // Release the screen device context
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
if (hBitmap)
{
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcMem, hBitmap);
BitBlt(hdc, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hOldBitmap);
DeleteDC(hdcMem);
}
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}