From 8a9b395a431f333ea59215c549f39ca0db2aabad Mon Sep 17 00:00:00 2001 From: xland <412588801@qq.com> Date: Wed, 10 Jul 2024 08:40:06 +0800 Subject: [PATCH] fix: pixInof on top of cutMask --- res/res.rc | 8 +- src/PixelInfo.cpp | 26 +- src/PixelInfo.h | 39 +-- src/WindowMain.cpp | 708 ++++++++++++++++++++++----------------------- src/WindowMain.h | 62 ++-- 5 files changed, 427 insertions(+), 416 deletions(-) diff --git a/res/res.rc b/res/res.rc index 70a82ed0..15f5ec4c 100644 --- a/res/res.rc +++ b/res/res.rc @@ -8,8 +8,8 @@ IDI_LOGO ICON "logo.ico" IDR_ICON_FONT ICON_FONT "icon.ttf" VS_VERSION_INFO VERSIONINFO -FILEVERSION 2,1,9,0 -PRODUCTVERSION 2,1,9,0 +FILEVERSION 2,1,10,0 +PRODUCTVERSION 2,1,10,0 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x1L @@ -26,12 +26,12 @@ BEGIN BEGIN VALUE "CompanyName", "GitHub xland" VALUE "FileDescription", "ScreenCapture" - VALUE "FileVersion", "2.1.9.0" + VALUE "FileVersion", "2.1.10.0" VALUE "InternalName", "ScreenCapture.exe" VALUE "LegalCopyright", "Copyright (C) LiuXiaolun 2023-2024" VALUE "OriginalFilename", "ScreenCapture.exe" VALUE "ProductName", "ScreenCapture" - VALUE "ProductVersion", "2.1.9.0" + VALUE "ProductVersion", "2.1.10.0" END END BLOCK "VarFileInfo" diff --git a/src/PixelInfo.cpp b/src/PixelInfo.cpp index 91f4858d..ce2b191f 100644 --- a/src/PixelInfo.cpp +++ b/src/PixelInfo.cpp @@ -1,4 +1,4 @@ -#include "PixelInfo.h" +#include "PixelInfo.h" #include "App.h" #include "WindowBase.h" #include "include/core/SkRegion.h" @@ -28,15 +28,29 @@ PixelInfo* PixelInfo::Get() return pixelInfo; } -bool PixelInfo::OnMouseMove(const int& x, const int& y) +bool PixelInfo::OnMouseMove(const int& x, const int& y) { + auto win = App::GetWin(); + if (win->IsMouseDown || win->state >= State::mask) + { + return false; + } + win->Refresh(); +} + +bool PixelInfo::OnPaint(SkCanvas* canvas) { auto win = App::GetWin(); if (win->IsMouseDown || win->state >= State::mask) { return false; } - float width = 200.0f; - float height = 200.0f; + float width{ 200.0f },height{ 200.0f },x,y; + { + POINT mousePos; + GetCursorPos(&mousePos); + x = (float)mousePos.x; + y = (float)mousePos.y; + } rect.setXYWH(x + 10, y + 10, width, height); if (rect.fLeft + width > win->w) { rect.offset(0 - width - 20, 0); @@ -44,8 +58,6 @@ bool PixelInfo::OnMouseMove(const int& x, const int& y) if (rect.fTop + height > win->h) { rect.offset(0, 0 - height - 20); } - auto canvas = win->surfaceFront->getCanvas(); - canvas->clear(SK_ColorTRANSPARENT); SkPaint paint; paint.setColor(SkColorSetARGB(168, 0, 0, 0)); canvas->drawRect(rect, paint); @@ -109,8 +121,6 @@ bool PixelInfo::OnMouseMove(const int& x, const int& y) data = str.data(); canvas->drawSimpleText(data, str.size()*2, SkTextEncoding::kUTF16, rect.fLeft + 8, rect.fTop + 186, *font, paint); - - win->Refresh(); return false; } diff --git a/src/PixelInfo.h b/src/PixelInfo.h index 1203bf5e..3640bcc4 100644 --- a/src/PixelInfo.h +++ b/src/PixelInfo.h @@ -1,20 +1,21 @@ -#pragma once -#include "include/core/SkSurface.h" -#include "include/core/SkCanvas.h" -#include "include/core/SkRect.h" -#include "include/core/SkPoint.h" -#include "include/core/SkPath.h" -#include - -class PixelInfo -{ -public: - ~PixelInfo(); - static void Init(); - static PixelInfo *Get(); - bool OnMouseMove(const int &x, const int &y); - -private: - PixelInfo(); - SkRect rect; +#pragma once +#include "include/core/SkSurface.h" +#include "include/core/SkCanvas.h" +#include "include/core/SkRect.h" +#include "include/core/SkPoint.h" +#include "include/core/SkPath.h" +#include + +class PixelInfo +{ +public: + ~PixelInfo(); + static void Init(); + static PixelInfo *Get(); + bool OnPaint(SkCanvas* canvas); + bool OnMouseMove(const int& x, const int& y); + +private: + PixelInfo(); + SkRect rect; }; \ No newline at end of file diff --git a/src/WindowMain.cpp b/src/WindowMain.cpp index ac81c3ce..41f87e9d 100644 --- a/src/WindowMain.cpp +++ b/src/WindowMain.cpp @@ -1,354 +1,354 @@ -#include "WindowMain.h" -#include -#include -#include -#include "include/core/SkBitmap.h" -#include "include/core/SkCanvas.h" -#include "include/core/SkStream.h" -#include "include/encode/SkPngEncoder.h" - -#include "App.h" -#include "CutMask.h" -#include "ToolMain.h" -#include "ToolSub.h" -#include "Recorder.h" -#include "PixelInfo.h" -#include "ColorBlender.h" - -WindowMain::WindowMain() -{ - initSize(); - CutMask::Init(w,h); - PixelInfo::Init(); - shotScreen(); - initWindow(); - state = State::start; -} - -WindowMain::~WindowMain() -{ - delete PixelInfo::Get(); - delete CutMask::Get(); - ColorBlender::Reset(); -} - -void WindowMain::Save(const std::string& filePath) -{ - Recorder::Get()->FinishPaint(); - paintToDie(); - auto rect = CutMask::GetCutRect(); - auto img = surfaceBase->makeImageSnapshot(SkIRect::MakeLTRB(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom)); - SkPixmap pixmap; - img->peekPixels(&pixmap); - SkPngEncoder::Options option; - SkFILEWStream stream(filePath.data()); - SkPngEncoder::Encode(&stream, pixmap, option); - stream.flush(); - App::Quit(6); -} - -void WindowMain::SaveToClipboard() -{ - Recorder::Get()->FinishPaint(); - paintToDie(); - auto rect = CutMask::GetCutRect(); - auto img = surfaceBase->makeImageSnapshot(SkIRect::MakeLTRB(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom)); - int width{ (int)rect.width() }, height{ (int)rect.height() }; - SkPixmap pixmap; - img->peekPixels(&pixmap); - HDC screenDC = GetDC(nullptr); - HDC memoryDC = CreateCompatibleDC(screenDC); - HBITMAP hBitmap = CreateCompatibleBitmap(screenDC, width, height); - DeleteObject(SelectObject(memoryDC, hBitmap)); - BITMAPINFO bitmapInfo = { 0 }; - bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); - bitmapInfo.bmiHeader.biWidth = width; - bitmapInfo.bmiHeader.biHeight = -height; - bitmapInfo.bmiHeader.biPlanes = 1; - bitmapInfo.bmiHeader.biBitCount = 32; - //pixmap.addr() 像素数据的地址 - SetDIBitsToDevice(memoryDC, 0, 0, width, height, 0, 0, 0, height, pixmap.addr(), &bitmapInfo, DIB_RGB_COLORS); - if (!OpenClipboard(nullptr)) { - MessageBox(NULL, L"Failed to open clipboard when save to clipboard.", L"Error", MB_OK | MB_ICONERROR); - return; - } - EmptyClipboard(); - SetClipboardData(CF_BITMAP, hBitmap); - CloseClipboard(); - ReleaseDC(nullptr, screenDC); - DeleteDC(memoryDC); - DeleteObject(hBitmap); - App::Quit(7); -} - -void WindowMain::paintToDie() -{ - surfaceBase->writePixels(*pixSrc, 0, 0); - auto canvas = surfaceBase->getCanvas(); - auto img = surfaceBack->makeImageSnapshot(); - canvas->drawImage(img, 0.f, 0.f); - img = surfaceFront->makeImageSnapshot(); - canvas->drawImage(img, 0.f, 0.f); -} - -void WindowMain::initCanvas() -{ - SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); - surfaceBase = SkSurfaces::Raster(info); - surfaceBack = SkSurfaces::Raster(info); - surfaceFront = SkSurfaces::Raster(info); - pixBase = new SkPixmap(); - surfaceBase->peekPixels(pixBase); - pixBack = new SkPixmap(); - surfaceBack->peekPixels(pixBack); -} - -LRESULT WindowMain::wndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -{ - switch (msg) - { - case WM_LBUTTONDOWN: - { - IsMouseDown = true; - auto x = GET_X_LPARAM(lparam); - auto y = GET_Y_LPARAM(lparam); - return onMouseDown(x, y); - } - case WM_LBUTTONDBLCLK: - { - auto x = GET_X_LPARAM(lparam); - auto y = GET_Y_LPARAM(lparam); - return onDoubleClick(x,y); - } - case WM_LBUTTONUP: - { - IsMouseDown = false; - auto x = GET_X_LPARAM(lparam); - auto y = GET_Y_LPARAM(lparam); - return onMouseUp(x, y); - } - case WM_MOUSEMOVE: - { - auto x = GET_X_LPARAM(lparam); - auto y = GET_Y_LPARAM(lparam); - if (IsMouseDown) { - return onMouseDrag(x, y); - } - else { - return onMouseMove(x, y); - } - } - case WM_RBUTTONDOWN: - { - auto x = GET_X_LPARAM(lparam); - auto y = GET_Y_LPARAM(lparam); - onMouseDownRight(x, y); - return true; - } - case WM_KEYDOWN: - { - onKeyDown(wparam); - return false; - } - case WM_CHAR: { - return onChar(wparam); - } - case WM_MOUSEWHEEL: { - int delta = GET_WHEEL_DELTA_WPARAM(wparam); - onMouseWheel(delta); - return false; - } - case WM_TIMER: { - return onTimeout(wparam); - } - default: - break; - } - return DefWindowProc(hwnd, msg, wparam, lparam); -} - -void WindowMain::paintCanvas() -{ - surfaceBase->writePixels(*pixSrc, 0, 0); - auto canvas = surfaceBase->getCanvas(); - auto img = surfaceBack->makeImageSnapshot(); - canvas->drawImage(img, 0.f, 0.f); - img = surfaceFront->makeImageSnapshot(); - canvas->drawImage(img, 0.f, 0.f); - - CutMask::Get()->OnPaint(canvas); - ToolMain::Get()->OnPaint(canvas); - ToolSub::Get()->OnPaint(canvas); -} - -bool WindowMain::onMouseDown(const int& x, const int& y) -{ - if (ToolMain::Get()->OnMouseDown(x, y)) { - return true; - } - if (ToolSub::Get()->OnMouseDown(x, y)) { - return true; - } - if (CutMask::Get()->OnMouseDown(x, y)) { - return true; - } - if (Recorder::Get()->OnMouseDown(x, y)) { - return true; - } - return false; -} -bool WindowMain::onMouseDownRight(const int& x, const int& y) -{ - bool flag = Recorder::Get()->OnMouseDownRight(x, y); - if (!flag) { - App::Quit(2); - } - return false; -} -bool WindowMain::onMouseUp(const int& x, const int& y) -{ - if (ToolMain::Get()->OnMouseUp(x, y)) { - return true; - } - if (ToolSub::Get()->OnMouseUp(x, y)) { - return true; - } - Recorder::Get()->OnMouseUp(x, y); - CutMask::Get()->OnMouseUp(x, y); - return false; -} -bool WindowMain::onMouseMove(const int& x, const int& y) -{ - auto tm = ToolMain::Get()->OnMouseMove(x, y); - auto ts = ToolSub::Get()->OnMouseMove(x, y); - if (tm || ts) { - return false; - } - auto cm = CutMask::Get()->OnMouseMove(x, y); - if (cm) { - return false; - } - PixelInfo::Get()->OnMouseMove(x, y); - Recorder::Get()->OnMouseMove(x, y); - return false; -} -bool WindowMain::onMouseDrag(const int& x, const int& y) -{ - auto tm = ToolMain::Get()->OnMouseMove(x, y); - auto ts = ToolSub::Get()->OnMouseMove(x, y); - if (tm || ts) { - return false; - } - if (CutMask::Get()->OnMouseDrag(x, y)) { - return false; - } - Recorder::Get()->OnMouseDrag(x, y); - return false; -} -bool WindowMain::onChar(const unsigned int& val) -{ - Recorder::Get()->OnChar(val); - return false; -} -bool WindowMain::onKeyDown(const unsigned int& val) -{ - bool flag = Recorder::Get()->OnKeyDown(val); - if (flag) { - return true; - } - flag = CutMask::Get()->OnKeyDown(val); - if (flag) { - return true; - } - if (val == VK_UP) { - POINT pos; - GetCursorPos(&pos); - SetCursorPos(pos.x, --pos.y); - } - else if (val == VK_DOWN) { - POINT pos; - GetCursorPos(&pos); - SetCursorPos(pos.x, ++pos.y); - } - else if (val == VK_LEFT) { - POINT pos; - GetCursorPos(&pos); - SetCursorPos(--pos.x, pos.y); - } - else if (val == VK_RIGHT) { - POINT pos; - GetCursorPos(&pos); - SetCursorPos(++pos.x, pos.y); - } - else if (val == VK_ESCAPE) - { - App::Quit(3); - } - else if (state > State::mask && GetKeyState(VK_CONTROL) < 0) { - if (val == 'S') {//Ctrl+S - App::SaveFile(); - } - else if (val == 'C') {//Ctrl+C - SaveToClipboard(); - } - else if (val == 'P') { //Ctrl+P - App::Pin(); - } - else if (val == 'Y') { //Ctrl+Y - Recorder::Get()->Redo(); - } - else if (val == 'Z') { //Ctrl+Z - Recorder::Get()->Undo(); - } - } - return true; -} -bool WindowMain::onMouseWheel(const int& delta) -{ - Recorder::Get()->OnMouseWheel(delta); - return false; -} -bool WindowMain::onDoubleClick(const int& x, const int& y) -{ - if (state >= State::tool && CutMask::GetCutRect().contains(x,y)) { - SaveToClipboard(); - return false; - } - auto tm = ToolMain::Get(); - if (tm->ToolRect.contains(x, y)) { - tm->OnMouseDown(x, y); - return false; - } - return false; -} -bool WindowMain::onTimeout(const unsigned int& id) -{ - Recorder::Get()->OnTimeout(id); - return false; -} -void WindowMain::shotScreen() -{ - HDC hScreen = GetDC(NULL); - HDC hDC = CreateCompatibleDC(hScreen); - HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, w, h); - DeleteObject(SelectObject(hDC, hBitmap)); - BOOL bRet = BitBlt(hDC, 0, 0, w, h, hScreen, x, y, SRCCOPY); - long long rowBytes = w * 4; - long long dataSize = rowBytes * h; - pixSrcData.resize(dataSize); - BITMAPINFO info = {sizeof(BITMAPINFOHEADER), (long)w, 0 - (long)h, 1, 32, BI_RGB, (DWORD)dataSize, 0, 0, 0, 0}; - GetDIBits(hDC, hBitmap, 0, h, &pixSrcData.front(), &info, DIB_RGB_COLORS); - DeleteDC(hDC); - DeleteObject(hBitmap); - ReleaseDC(NULL, hScreen); - SkImageInfo imgInfo = SkImageInfo::MakeN32Premul(w, h); - pixSrc = new SkPixmap(imgInfo, &pixSrcData.front(), rowBytes); -} - -void WindowMain::initSize() -{ - x = GetSystemMetrics(SM_XVIRTUALSCREEN); - y = GetSystemMetrics(SM_YVIRTUALSCREEN); - w = GetSystemMetrics(SM_CXVIRTUALSCREEN); - h = GetSystemMetrics(SM_CYVIRTUALSCREEN); -} +#include "WindowMain.h" +#include +#include +#include +#include "include/core/SkBitmap.h" +#include "include/core/SkCanvas.h" +#include "include/core/SkStream.h" +#include "include/encode/SkPngEncoder.h" + +#include "App.h" +#include "CutMask.h" +#include "ToolMain.h" +#include "ToolSub.h" +#include "Recorder.h" +#include "PixelInfo.h" +#include "ColorBlender.h" + +WindowMain::WindowMain() +{ + initSize(); + CutMask::Init(w,h); + PixelInfo::Init(); + shotScreen(); + initWindow(); + state = State::start; +} + +WindowMain::~WindowMain() +{ + delete PixelInfo::Get(); + delete CutMask::Get(); + ColorBlender::Reset(); +} + +void WindowMain::Save(const std::string& filePath) +{ + Recorder::Get()->FinishPaint(); + paintToDie(); + auto rect = CutMask::GetCutRect(); + auto img = surfaceBase->makeImageSnapshot(SkIRect::MakeLTRB(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom)); + SkPixmap pixmap; + img->peekPixels(&pixmap); + SkPngEncoder::Options option; + SkFILEWStream stream(filePath.data()); + SkPngEncoder::Encode(&stream, pixmap, option); + stream.flush(); + App::Quit(6); +} + +void WindowMain::SaveToClipboard() +{ + Recorder::Get()->FinishPaint(); + paintToDie(); + auto rect = CutMask::GetCutRect(); + auto img = surfaceBase->makeImageSnapshot(SkIRect::MakeLTRB(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom)); + int width{ (int)rect.width() }, height{ (int)rect.height() }; + SkPixmap pixmap; + img->peekPixels(&pixmap); + HDC screenDC = GetDC(nullptr); + HDC memoryDC = CreateCompatibleDC(screenDC); + HBITMAP hBitmap = CreateCompatibleBitmap(screenDC, width, height); + DeleteObject(SelectObject(memoryDC, hBitmap)); + BITMAPINFO bitmapInfo = { 0 }; + bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bitmapInfo.bmiHeader.biWidth = width; + bitmapInfo.bmiHeader.biHeight = -height; + bitmapInfo.bmiHeader.biPlanes = 1; + bitmapInfo.bmiHeader.biBitCount = 32; + //pixmap.addr() 像素数据的地址 + SetDIBitsToDevice(memoryDC, 0, 0, width, height, 0, 0, 0, height, pixmap.addr(), &bitmapInfo, DIB_RGB_COLORS); + if (!OpenClipboard(nullptr)) { + MessageBox(NULL, L"Failed to open clipboard when save to clipboard.", L"Error", MB_OK | MB_ICONERROR); + return; + } + EmptyClipboard(); + SetClipboardData(CF_BITMAP, hBitmap); + CloseClipboard(); + ReleaseDC(nullptr, screenDC); + DeleteDC(memoryDC); + DeleteObject(hBitmap); + App::Quit(7); +} + +void WindowMain::paintToDie() +{ + surfaceBase->writePixels(*pixSrc, 0, 0); + auto canvas = surfaceBase->getCanvas(); + auto img = surfaceBack->makeImageSnapshot(); + canvas->drawImage(img, 0.f, 0.f); + img = surfaceFront->makeImageSnapshot(); + canvas->drawImage(img, 0.f, 0.f); +} + +void WindowMain::initCanvas() +{ + SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); + surfaceBase = SkSurfaces::Raster(info); + surfaceBack = SkSurfaces::Raster(info); + surfaceFront = SkSurfaces::Raster(info); + pixBase = new SkPixmap(); + surfaceBase->peekPixels(pixBase); + pixBack = new SkPixmap(); + surfaceBack->peekPixels(pixBack); +} + +LRESULT WindowMain::wndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) +{ + switch (msg) + { + case WM_LBUTTONDOWN: + { + IsMouseDown = true; + auto x = GET_X_LPARAM(lparam); + auto y = GET_Y_LPARAM(lparam); + return onMouseDown(x, y); + } + case WM_LBUTTONDBLCLK: + { + auto x = GET_X_LPARAM(lparam); + auto y = GET_Y_LPARAM(lparam); + return onDoubleClick(x,y); + } + case WM_LBUTTONUP: + { + IsMouseDown = false; + auto x = GET_X_LPARAM(lparam); + auto y = GET_Y_LPARAM(lparam); + return onMouseUp(x, y); + } + case WM_MOUSEMOVE: + { + auto x = GET_X_LPARAM(lparam); + auto y = GET_Y_LPARAM(lparam); + if (IsMouseDown) { + return onMouseDrag(x, y); + } + else { + return onMouseMove(x, y); + } + } + case WM_RBUTTONDOWN: + { + auto x = GET_X_LPARAM(lparam); + auto y = GET_Y_LPARAM(lparam); + onMouseDownRight(x, y); + return true; + } + case WM_KEYDOWN: + { + onKeyDown(wparam); + return false; + } + case WM_CHAR: { + return onChar(wparam); + } + case WM_MOUSEWHEEL: { + int delta = GET_WHEEL_DELTA_WPARAM(wparam); + onMouseWheel(delta); + return false; + } + case WM_TIMER: { + return onTimeout(wparam); + } + default: + break; + } + return DefWindowProc(hwnd, msg, wparam, lparam); +} + +void WindowMain::paintCanvas() +{ + surfaceBase->writePixels(*pixSrc, 0, 0); + auto canvas = surfaceBase->getCanvas(); + auto img = surfaceBack->makeImageSnapshot(); + canvas->drawImage(img, 0.f, 0.f); + img = surfaceFront->makeImageSnapshot(); + canvas->drawImage(img, 0.f, 0.f); + CutMask::Get()->OnPaint(canvas); + PixelInfo::Get()->OnPaint(canvas); + ToolMain::Get()->OnPaint(canvas); + ToolSub::Get()->OnPaint(canvas); +} + +bool WindowMain::onMouseDown(const int& x, const int& y) +{ + if (ToolMain::Get()->OnMouseDown(x, y)) { + return true; + } + if (ToolSub::Get()->OnMouseDown(x, y)) { + return true; + } + if (CutMask::Get()->OnMouseDown(x, y)) { + return true; + } + if (Recorder::Get()->OnMouseDown(x, y)) { + return true; + } + return false; +} +bool WindowMain::onMouseDownRight(const int& x, const int& y) +{ + bool flag = Recorder::Get()->OnMouseDownRight(x, y); + if (!flag) { + App::Quit(2); + } + return false; +} +bool WindowMain::onMouseUp(const int& x, const int& y) +{ + if (ToolMain::Get()->OnMouseUp(x, y)) { + return true; + } + if (ToolSub::Get()->OnMouseUp(x, y)) { + return true; + } + Recorder::Get()->OnMouseUp(x, y); + CutMask::Get()->OnMouseUp(x, y); + return false; +} +bool WindowMain::onMouseMove(const int& x, const int& y) +{ + auto tm = ToolMain::Get()->OnMouseMove(x, y); + auto ts = ToolSub::Get()->OnMouseMove(x, y); + if (tm || ts) { + return false; + } + auto cm = CutMask::Get()->OnMouseMove(x, y); + if (cm) { + return false; + } + PixelInfo::Get()->OnMouseMove(x, y); + Recorder::Get()->OnMouseMove(x, y); + return false; +} +bool WindowMain::onMouseDrag(const int& x, const int& y) +{ + auto tm = ToolMain::Get()->OnMouseMove(x, y); + auto ts = ToolSub::Get()->OnMouseMove(x, y); + if (tm || ts) { + return false; + } + if (CutMask::Get()->OnMouseDrag(x, y)) { + return false; + } + Recorder::Get()->OnMouseDrag(x, y); + return false; +} +bool WindowMain::onChar(const unsigned int& val) +{ + Recorder::Get()->OnChar(val); + return false; +} +bool WindowMain::onKeyDown(const unsigned int& val) +{ + bool flag = Recorder::Get()->OnKeyDown(val); + if (flag) { + return true; + } + flag = CutMask::Get()->OnKeyDown(val); + if (flag) { + return true; + } + if (val == VK_UP) { + POINT pos; + GetCursorPos(&pos); + SetCursorPos(pos.x, --pos.y); + } + else if (val == VK_DOWN) { + POINT pos; + GetCursorPos(&pos); + SetCursorPos(pos.x, ++pos.y); + } + else if (val == VK_LEFT) { + POINT pos; + GetCursorPos(&pos); + SetCursorPos(--pos.x, pos.y); + } + else if (val == VK_RIGHT) { + POINT pos; + GetCursorPos(&pos); + SetCursorPos(++pos.x, pos.y); + } + else if (val == VK_ESCAPE) + { + App::Quit(3); + } + else if (state > State::mask && GetKeyState(VK_CONTROL) < 0) { + if (val == 'S') {//Ctrl+S + App::SaveFile(); + } + else if (val == 'C') {//Ctrl+C + SaveToClipboard(); + } + else if (val == 'P') { //Ctrl+P + App::Pin(); + } + else if (val == 'Y') { //Ctrl+Y + Recorder::Get()->Redo(); + } + else if (val == 'Z') { //Ctrl+Z + Recorder::Get()->Undo(); + } + } + return true; +} +bool WindowMain::onMouseWheel(const int& delta) +{ + Recorder::Get()->OnMouseWheel(delta); + return false; +} +bool WindowMain::onDoubleClick(const int& x, const int& y) +{ + if (state >= State::tool && CutMask::GetCutRect().contains(x,y)) { + SaveToClipboard(); + return false; + } + auto tm = ToolMain::Get(); + if (tm->ToolRect.contains(x, y)) { + tm->OnMouseDown(x, y); + return false; + } + return false; +} +bool WindowMain::onTimeout(const unsigned int& id) +{ + Recorder::Get()->OnTimeout(id); + return false; +} +void WindowMain::shotScreen() +{ + HDC hScreen = GetDC(NULL); + HDC hDC = CreateCompatibleDC(hScreen); + HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, w, h); + DeleteObject(SelectObject(hDC, hBitmap)); + BOOL bRet = BitBlt(hDC, 0, 0, w, h, hScreen, x, y, SRCCOPY); + long long rowBytes = w * 4; + long long dataSize = rowBytes * h; + pixSrcData.resize(dataSize); + BITMAPINFO info = {sizeof(BITMAPINFOHEADER), (long)w, 0 - (long)h, 1, 32, BI_RGB, (DWORD)dataSize, 0, 0, 0, 0}; + GetDIBits(hDC, hBitmap, 0, h, &pixSrcData.front(), &info, DIB_RGB_COLORS); + DeleteDC(hDC); + DeleteObject(hBitmap); + ReleaseDC(NULL, hScreen); + SkImageInfo imgInfo = SkImageInfo::MakeN32Premul(w, h); + pixSrc = new SkPixmap(imgInfo, &pixSrcData.front(), rowBytes); +} + +void WindowMain::initSize() +{ + x = GetSystemMetrics(SM_XVIRTUALSCREEN); + y = GetSystemMetrics(SM_YVIRTUALSCREEN); + w = GetSystemMetrics(SM_CXVIRTUALSCREEN); + h = GetSystemMetrics(SM_CYVIRTUALSCREEN); +} diff --git a/src/WindowMain.h b/src/WindowMain.h index 5b85b99c..3f2b42f8 100644 --- a/src/WindowMain.h +++ b/src/WindowMain.h @@ -1,32 +1,32 @@ -#pragma once -#include -#include -#include -#include -#include "WindowBase.h" - -class WindowMain : public WindowBase -{ -public: - WindowMain(); - ~WindowMain(); - void Save(const std::string& filePath) override; - void SaveToClipboard() override; - void paintToDie(); -private: - LRESULT CALLBACK wndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) override; - void initCanvas() override; - void paintCanvas() override; - void initSize(); - void shotScreen(); - bool onMouseDown(const int& x, const int& y); - bool onMouseDownRight(const int& x, const int& y); - bool onMouseUp(const int& x, const int& y); - bool onMouseMove(const int& x, const int& y); - bool onMouseDrag(const int& x, const int& y); - bool onChar(const unsigned int& val); - bool onKeyDown(const unsigned int& val); - bool onMouseWheel(const int& delta); - bool onDoubleClick(const int& x, const int& y); - bool onTimeout(const unsigned int& id); +#pragma once +#include +#include +#include +#include +#include "WindowBase.h" + +class WindowMain : public WindowBase +{ +public: + WindowMain(); + ~WindowMain(); + void Save(const std::string& filePath) override; + void SaveToClipboard() override; + void paintToDie(); +private: + LRESULT CALLBACK wndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) override; + void initCanvas() override; + void paintCanvas() override; + void initSize(); + void shotScreen(); + bool onMouseDown(const int& x, const int& y); + bool onMouseDownRight(const int& x, const int& y); + bool onMouseUp(const int& x, const int& y); + bool onMouseMove(const int& x, const int& y); + bool onMouseDrag(const int& x, const int& y); + bool onChar(const unsigned int& val); + bool onKeyDown(const unsigned int& val); + bool onMouseWheel(const int& delta); + bool onDoubleClick(const int& x, const int& y); + bool onTimeout(const unsigned int& id); }; \ No newline at end of file