HbmScreen = CreateCompatibleBitmap(hdcWindow, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top) Create a compatible bitmap from the Window DC. MessageBox(hWnd, L"StretchBlt has failed", L"Failed", MB_OK) The source DC is the entire screen, and the destination DC is the current window (HWND). Get the client area for size calculation. MessageBox(hWnd, L"CreateCompatibleDC has failed", L"Failed", MB_OK) HdcMemDC = CreateCompatibleDC(hdcWindow) Create a compatible DC, which is used in a BitBlt from the window DC. Retrieve the handle to a display device context for the client Note: This function attempts to create a file called captureqwsx.bmp PURPOSE: Captures a screenshot into a window ,and then saves it in a. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,ĬW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr) HInst = hInstance // Store instance handle in our global variable create and display the main program window.īOOL InitInstance(HINSTANCE hInstance, int nCmdShow) In this function, we save the instance handle in a global variable and PURPOSE: Saves instance handle and creates main window FUNCTION: InitInstance(HINSTANCE, int) Wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)) Wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_GDICAPTURINGANIMAGE) Wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1) Wcex.hCursor = LoadCursor(nullptr, IDC_ARROW) Wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GDICAPTURINGANIMAGE)) If (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))ĪTOM MyRegisterClass(HINSTANCE hInstance) HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GDICAPTURINGANIMAGE)) LoadStringW(hInstance, IDC_GDICAPTURINGANIMAGE, szWindowClass, MAX_LOADSTRING) LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING) Int APIENTRY wWinMain(_In_ HINSTANCE hInstance, INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM) LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) Forward declarations of functions included in this code module:ĪTOM MyRegisterClass(HINSTANCE hInstance) WCHAR szWindowClass // the main window class name GDI_CapturingAnImage.cpp : Defines the entry point for the application. Each time you resize the window, you'll see the captured screenshot displayed in the client area. Open the GDI_CapturingAnImage.cpp source code file in your new project, and replace its contents with the listing below. It's important to name the new project GDI_CapturingAnImage so that the code listing below will compile (for example, it includes GDI_CapturingAnImage.h, which will exist in your new project if you name it as suggested). ![]() To try out the code example, begin by creating a new project in Visual Studio based on the Windows Desktop Application project template. This section contains a code example that captures an image of the entire desktop, scales it down to the current window size, and then saves it to a file (as well as displaying it in the client area). To redisplay the image, call BitBlt a second time, specifying the compatible DC as the source DC and a window (or printer) DC as the target DC. In this case, the target DC is the compatible DC, so when BitBlt completes the transfer, the image has been stored in memory. Instead, BitBlt receives handles that identify two device contexts and copies the bitmap data from a bitmap selected into the source DC into a bitmap selected into the target DC. However, the two arguments to this function are not bitmap handles. This function performs a bit block transfer that is, it copies data from a source bitmap into a destination bitmap. After you create a compatible DC, you create a bitmap with the appropriate dimensions by calling the CreateCompatibleBitmap function and then select it into this device context by calling the SelectObject function.Īfter the compatible device context is created and the appropriate bitmap has been selected into it, you can capture the image. To store an image temporarily, your application must call CreateCompatibleDC to create a DC that is compatible with the current window DC. Later, when the user selects the normal view, the application must replace the zoomed image with a copy of the normal view that it temporarily saved. For example, when you scale or zoom a picture created in a drawing application, the application must temporarily save the normal view of the image and display the zoomed view. In some cases, you may want your application to capture images and store them only temporarily. You can use a bitmap to capture an image, and you can store the captured image in memory, display it at a different location in your application's window, or display it in another window.
0 Comments
Leave a Reply. |