WIN32窗口没有重新绘制

时间:2015-10-05 17:13:38

标签: c++ winapi

我正在编写win32应用程序,按下打印屏幕键后打印屏幕。它显示了打印屏幕,然后在两次鼠标点击之后切割它们之间的矩形区域并将其复制到剪贴板。我重新绘制窗口有问题。当我点击时,程序调用WM_PAINT消息中的代码,但屏幕上没有任何变化。我可以在调试器中看到,该程序输入if语句。这是我的第一个win32计划,所以我可能会误解它是如何工作的。请原谅;)

我正在使用Win 8.1和VS 2015。

以下是代码:

// MyPrintScreen2.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "MyPrintScreen2.h"
#include <windowsx.h>

#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst;                                // current instance
WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
//HWND hWnd;
HBITMAP hBitmap=NULL, outBitmap=NULL;

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
ATOM hotkey;
bool clickedOnce=false;
bool clickedTwice=false;
int x1, y1, x2, y2,xa,ya;
bool Crop(RECT cropArea);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
int resx = GetSystemMetrics(SM_CXSCREEN);
int resy = GetSystemMetrics(SM_CYSCREEN);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_MYPRINTSCREEN2, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MYPRINTSCREEN2));

    MSG msg;

    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style = 0;//CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYPRINTSCREEN2));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = NULL;// MAKEINTRESOURCEW(IDC_MYPRINTSCREEN2);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}


BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable


  HWND  hWnd = CreateWindowW(szWindowClass, szTitle, WS_MAXIMIZE | WS_POPUP,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }
   //ShowWindow(hWnd, SW_MAXIMIZE);//nCmdShow);
   //UpdateWindow(hWnd);
   hotkey = GlobalAddAtom((LPWSTR)"PrtScr");
   if (hotkey) RegisterHotKey(hWnd, hotkey, NULL, VK_SNAPSHOT);
   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {


    case WM_CREATE:
    {


    }

    case WM_PAINT:
        {


            PAINTSTRUCT     ps;
            HDC             hdc;
            hdc = BeginPaint(hWnd, &ps);

            HDC hMemDC = CreateCompatibleDC(hdc);
            ::SelectObject(hMemDC, hBitmap);
            if(!clickedOnce) BitBlt(hdc, 0, 0, resx, resy, hMemDC, 0, 0, SRCCOPY);

            Rectangle(hdc,200, 200, 300, 300);  //rectangles only for testing with the easiest drawing
            if (clickedOnce)

            {
                Rectangle(hdc, 300, 300, 400, 400); 

            }

            ::DeleteDC(hMemDC);
            EndPaint(hWnd, &ps);

        }
        break;
    case WM_DESTROY:
        DeleteObject(hBitmap);
        PostQuitMessage(0);
        break;


    case WM_HOTKEY:
        if (wParam == hotkey)
        {
            int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
            int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
            HWND hDesktopWnd = GetDesktopWindow();
            HDC hDesktopDC = GetDC(hDesktopWnd);
            HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
            hBitmap = CreateCompatibleBitmap(hDesktopDC,
                nScreenWidth, nScreenHeight);
            SelectObject(hCaptureDC, hBitmap);
            BitBlt(hCaptureDC, 0, 0, nScreenWidth, nScreenHeight,
                hDesktopDC, 0, 0, SRCCOPY | CAPTUREBLT);

            ReleaseDC(hDesktopWnd, hDesktopDC);
            DeleteDC(hCaptureDC);
            ShowWindow(hWnd, SW_MAXIMIZE);
            UpdateWindow(hWnd);
        }
        break;
    case WM_LBUTTONUP:
        if (!clickedOnce)
        {
            x1= GET_X_LPARAM(lParam);
            y1= GET_Y_LPARAM(lParam);
            clickedOnce = true;
        }
        else
        {
            x2 = GET_X_LPARAM(lParam);
            y2 = GET_Y_LPARAM(lParam);
            clickedTwice = true;
            RECT cropArea;
            cropArea.top = y1;
            cropArea.left = x1;
            cropArea.bottom = y2;
            cropArea.right = x2;
            bool a = Crop(cropArea);
            if (OpenClipboard(0))
            {

                EmptyClipboard();

                HANDLE bitmapH = outBitmap;
                SetClipboardData(CF_BITMAP, bitmapH);
                CloseClipboard();

            }

        }

        RECT rect;
        GetClientRect(hWnd, &rect);
        //UpdateWindow(hWnd);
        RedrawWindow(hWnd,NULL,NULL, RDW_INTERNALPAINT);

        break;
    case WM_MOUSEMOVE:
        if (clickedOnce)
        {
            xa = GET_X_LPARAM(lParam);
            ya = GET_Y_LPARAM(lParam);
            //RedrawWindow(hWnd, NULL, NULL, RDW_INTERNALPAINT);
        }
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}


bool Crop(RECT cropArea)
{
    HDC hSrc = CreateCompatibleDC(NULL);
    SelectObject(hSrc, hBitmap);

    HDC hNew = CreateCompatibleDC(hSrc);
    HBITMAP hBmp = CreateCompatibleBitmap(hSrc, cropArea.right - cropArea.left, cropArea.bottom - cropArea.top);
    HBITMAP hOld = (HBITMAP)SelectObject(hNew, hBmp);

    bool retVal = (BitBlt(hNew, 0, 0, cropArea.right - cropArea.left, cropArea.bottom - cropArea.top, hSrc, cropArea.left, cropArea.top, SRCCOPY)) ? true : false;

    SelectObject(hNew, hOld);

    DeleteDC(hSrc);
    DeleteDC(hNew);

    DeleteObject(outBitmap);

    outBitmap = hBmp;

    return retVal;
}

0 个答案:

没有答案