windows截图代码

注意点:
1.在大屏的笔记本:要去显示设置里面把缩放调到100%
2.获取屏幕坐标的注意点https://blog.csdn.net/zhangweishuang/article/details/5966105?utm_source=jiancool
// ICONDEMO.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "ICONDEMO.h"
// 此代码模块中包含的函数的前向声明:
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
 _In_opt_ HINSTANCE hPrevInstance,
 _In_ LPWSTR    lpCmdLine,
 _In_ int       nCmdShow)
{
 static TCHAR szAppName[] = TEXT("Blowup");
 HACCEL hAccel;
 HWND hwnd;
 MSG msg;
 WNDCLASS wndclass;
 wndclass.style = CS_HREDRAW | CS_VREDRAW;
 wndclass.lpfnWndProc = WndProc;
 wndclass.cbClsExtra = 0;
 wndclass.cbWndExtra = 0;
 wndclass.hInstance = hInstance;
 wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
 wndclass.hCursor = LoadCursor(nullptr, IDC_ARROW);
 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 wndclass.lpszMenuName = szAppName;
 wndclass.lpszClassName = szAppName;
 if (!RegisterClass(&wndclass))
 {
  MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
  return 0;
 }
 hwnd = CreateWindow(szAppName, TEXT("Blow-Up Mouse Demo"), WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, CW_USEDEFAULT,
  CW_USEDEFAULT, CW_USEDEFAULT,
  NULL, NULL, hInstance, NULL);
 ShowWindow(hwnd, nCmdShow);
 UpdateWindow(hwnd);
 hAccel = LoadAccelerators(hInstance, szAppName);
    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
  if (!TranslateAccelerator(hwnd, hAccel, &msg))
  {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
  
    }
 return  msg.wParam;
 
}
void InvertBlock(HWND hwndScr, HWND hwnd, POINT ptBeg, POINT ptEnd)
{
 HDC hdc;
 hdc = GetDCEx(hwndScr, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE);
 ClientToScreen(hwnd, &ptBeg);
 ClientToScreen(hwnd, &ptEnd);
 PatBlt(hdc, ptBeg.x, ptBeg.y, ptEnd.x - ptBeg.x, ptEnd.y - ptBeg.y, DSTINVERT);
 ReleaseDC(hwndScr, hdc);
}
HBITMAP CopyBitmap(HBITMAP hBitmapSrc)
{
 BITMAP bitmap;
 HBITMAP hBitmapDst;
 HDC hdcSrc, hdcDst;
 
 GetObject(hBitmapSrc, sizeof(BITMAP), &bitmap);
 hBitmapDst = CreateBitmapIndirect(&bitmap);
 hdcSrc = CreateCompatibleDC(NULL);
 hdcDst = CreateCompatibleDC(NULL);
 SelectObject(hdcSrc, hBitmapSrc);
 SelectObject(hdcDst, hBitmapDst);
 BitBlt(hdcDst, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcSrc, 0, 0, SRCCOPY);
 DeleteDC(hdcSrc);
 DeleteDC(hdcDst);
 return hBitmapDst;
}
//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的:    处理主窗口的消息。
//
//  WM_COMMAND  - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY  - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 static BOOL bCapturing, bBlocking;
 static HBITMAP hBitmap;
 static HWND hwndScr;
 static POINT ptBeg, ptEnd;
 BITMAP bm;
 HBITMAP hBitmapClip;
 HDC hdc, hdcMem;
 int iEnable;
 PAINTSTRUCT ps;
 RECT rect;
 POINT point;
 switch (message)
 {
 case WM_LBUTTONDOWN:
  if (!bCapturing)
  {
   if (LockWindowUpdate(hwndScr = GetDesktopWindow()))
   {
    bCapturing = TRUE;
    SetCapture(hwnd);
    SetCursor(LoadCursor(NULL, IDC_CROSS));
   }
   else
   {
    MessageBeep(0);
   }
  }
  return 0;
 case WM_RBUTTONDOWN:
  if (bCapturing)
  {
   bBlocking = TRUE;
  /* ptBeg.x = LOWORD(lParam);
   ptBeg.y = HIWORD(lParam);*/
   ptBeg.x = MAKEPOINTS(lParam).x;
   ptBeg.y = MAKEPOINTS(lParam).y;
   ptEnd = ptBeg;
   InvertBlock(hwndScr, hwnd, ptBeg, ptEnd);
  }
  return 0;
 case WM_MOUSEMOVE:
  if (bBlocking)
  {
   InvertBlock(hwndScr, hwnd, ptBeg, ptEnd);
   ptEnd.x = MAKEPOINTS(lParam).x;
   ptEnd.y = MAKEPOINTS(lParam).y;
   InvertBlock(hwndScr, hwnd, ptBeg, ptEnd);
  }
  return 0;
 case WM_LBUTTONUP:
 case WM_RBUTTONUP:
  if (bBlocking)
  {
   InvertBlock(hwndScr, hwnd, ptBeg, ptEnd);
   ptEnd.x = MAKEPOINTS(lParam).x;
   ptEnd.y = MAKEPOINTS(lParam).y;
   if (hBitmap)
   {
    DeleteObject(hBitmap);
    hBitmap = NULL;
   }
   //hdc = GetDC(hwndScr);
   hdc = GetDCEx(hwndScr, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE);
   hdcMem = CreateCompatibleDC(hdc);
   hBitmap = CreateCompatibleBitmap(hdc, abs(ptEnd.x - ptBeg.x), abs(ptEnd.y - ptBeg.y));
   SelectObject(hdcMem, hBitmap);
   point = ptBeg;
   ClientToScreen(hwnd,&point);
   StretchBlt(hdcMem, 0, 0, abs(ptEnd.x - ptBeg.x), abs(ptEnd.y - ptBeg.y), hdc, point.x, point.y, ptEnd.x - ptBeg.x, ptEnd.y - ptBeg.y, SRCCOPY);
   DeleteDC(hdcMem);
   ReleaseDC(hwnd, hdc);
   InvalidateRect(hwnd, NULL, TRUE);
  }
  if (bBlocking || bCapturing)
  {
   bBlocking = bCapturing = FALSE;
   SetCursor(LoadCursor(NULL, IDC_ARROW));
   ReleaseCapture();
   LockWindowUpdate(NULL);
  }
  return 0;
 case WM_INITMENUPOPUP:
  iEnable = IsClipboardFormatAvailable(CF_BITMAP) ? MF_ENABLED : MF_GRAYED;
  EnableMenuItem((HMENU)wParam, IDM_EDIT_PASTE, iEnable);
  iEnable = hBitmap ? MF_ENABLED : MF_GRAYED;
  EnableMenuItem((HMENU)wParam, IDM_EDIT_CUT, iEnable);
  EnableMenuItem((HMENU)wParam, IDM_EDIT_COPY, iEnable);
  EnableMenuItem((HMENU)wParam, IDM_EDIT_DELETE, iEnable);
  return 0;
 case WM_COMMAND:
  switch (LOWORD(wParam))
  {
  case IDM_EDIT_CUT:
  case IDM_EDIT_COPY:
   if (hBitmap)
   {
    hBitmapClip = CopyBitmap(hBitmap);
    OpenClipboard(hwnd);
    EmptyClipboard();
    SetClipboardData(CF_BITMAP, hBitmapClip);
   }
   if (LOWORD(wParam) == IDM_EDIT_COPY)
   {
    return 0;
   }
  case IDM_EDIT_DELETE:
   if (hBitmap)
   {
    DeleteObject(hBitmap);
    hBitmap = NULL;
   }
   InvalidateRect(hwnd, NULL, TRUE);
   return 0;
  case IDM_EDIT_PASTE:
   if (hBitmap)
   {
    DeleteObject(hBitmap);
    hBitmap = NULL;
   }
   OpenClipboard(hwnd);
   hBitmapClip = (HBITMAP)GetClipboardData(CF_BITMAP);
   if (hBitmapClip)
   {
    hBitmap = CopyBitmap(hBitmapClip);
   }
   CloseClipboard();
   InvalidateRect(hwnd, NULL, TRUE);
   return 0;
  }
  break;
 case WM_PAINT:
  hdc = BeginPaint(hwnd, &ps);
  if (hBitmap)
  {
   GetClientRect(hwnd, &rect);
   hdcMem = CreateCompatibleDC(hdc);
   SelectObject(hdcMem, hBitmap);
   GetObject(hBitmap, sizeof(BITMAP), (PSTR)&bm);
   SetStretchBltMode(hdc, COLORONCOLOR);
   StretchBlt(hdc, 0, 0, rect.right, rect.bottom, hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
   DeleteDC(hdcMem);
  }
  EndPaint(hwnd, &ps);
  return 0;
 case WM_DESTROY:
  if (hBitmap)
  {
   DeleteObject(hBitmap);
  }
  PostQuitMessage(0);
  return 0;
  
 }
 return DefWindowProc(hwnd, message, wParam, lParam);
}
 
 
/////////////////////////////////////////////////////////////////////////////
//
// Menu
//
BLOWUP MENU
BEGIN
    POPUP "&Edit"
    BEGIN
        MENUITEM "Cu&t Ctrl+X",                IDM_EDIT_CUT
        MENUITEM "&Copy Ctrl+C",               IDM_EDIT_COPY
        MENUITEM "&Paste Ctrl+V",              IDM_EDIT_PASTE
        MENUITEM "De&lete Delete",             IDM_EDIT_DELETE
    END
END
 
/////////////////////////////////////////////////////////////////////////////
//
// Accelerator
//
BLOWUP ACCELERATORS
BEGIN
    "C",            IDM_EDIT_COPY,          VIRTKEY, CONTROL, NOINVERT
    "V",            IDM_EDIT_PASTE,         VIRTKEY, CONTROL, NOINVERT
    VK_DELETE,      IDM_EDIT_DELETE,        VIRTKEY, NOINVERT
    "X",            IDM_EDIT_CUT,           VIRTKEY, CONTROL, NOINVERT
END
 
#define IDM_EDIT_CUT                    40001
#define IDM_EDIT_COPY                   40002
#define IDM_EDIT_PASTE                  40003
#define IDM_EDIT_DELETE                 40004
原文地址:https://www.cnblogs.com/xiaomi-daming/p/12993108.html