From eb7fdb0fa322ff49e8f429fb60b79d8ba9d6dfd4 Mon Sep 17 00:00:00 2001 From: huidong <61970628+zouhuidong@users.noreply.github.com> Date: Sat, 16 Jul 2022 15:37:00 +0800 Subject: [PATCH] Ver3.1.1 --- lib/EasyWin32.cpp | 87 ++++++++++++++++++++++++----------------------- lib/EasyWin32.h | 85 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 117 insertions(+), 55 deletions(-) diff --git a/lib/EasyWin32.cpp b/lib/EasyWin32.cpp index 6771ffa..f19f2cd 100644 --- a/lib/EasyWin32.cpp +++ b/lib/EasyWin32.cpp @@ -2,7 +2,7 @@ // // EasyWin32.cpp // -// Ver 3.1.0 +// Ver 3.1.1 // // @@ -723,8 +723,8 @@ int SetWindowExStyle(long lNewExStyle) HICON GetDefaultAppIcon() { IMAGE img = GetDefaultIconImage(); - HBITMAP hBmp = GetImageHBitmap(&img); - HICON hIcon = HICONFromHBitmap(hBmp); + HBITMAP hBmp = Image2Bitmap(&img); + HICON hIcon = Bitmap2Icon(hBmp); DeleteObject(hBmp); return hIcon; } @@ -856,16 +856,17 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) BOOL repeatFlag = (keyFlags & KF_REPEAT) == KF_REPEAT; // previous key-state flag, 1 on autorepeat WORD repeatCount = LOWORD(lParam); // repeat count, > 0 if several keydown messages was combined into one message BOOL upFlag = (keyFlags & KF_UP) == KF_UP; // transition-state flag, 1 on keyup - + + // ���ܼ������������� // if we want to distinguish these keys: - switch (vkCode) - { - case VK_SHIFT: // converts to VK_LSHIFT or VK_RSHIFT - case VK_CONTROL: // converts to VK_LCONTROL or VK_RCONTROL - case VK_MENU: // converts to VK_LMENU or VK_RMENU - vkCode = LOWORD(MapVirtualKeyW(scanCode, MAPVK_VSC_TO_VK_EX)); - break; - } + //switch (vkCode) + //{ + //case VK_SHIFT: // converts to VK_LSHIFT or VK_RSHIFT + //case VK_CONTROL: // converts to VK_LCONTROL or VK_RCONTROL + //case VK_MENU: // converts to VK_LMENU or VK_RMENU + // vkCode = LOWORD(MapVirtualKeyW(scanCode, MAPVK_VSC_TO_VK_EX)); + // break; + //} ExMessage msgKey = {}; msgKey.message = msg; @@ -877,7 +878,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) g_vecWindows[indexWnd].vecMessage.push_back(msgKey); // ������̨��һ�ݣ�֧�� _getch() ϵ�к��� - SendMessage(g_hConsole, msg, wParam, lParam); + PostMessage(g_hConsole, msg, wParam, lParam); } break; @@ -890,7 +891,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) g_vecWindows[indexWnd].vecMessage.push_back(msgChar); // ֪ͨ����̨ - SendMessage(g_hConsole, msg, wParam, lParam); + PostMessage(g_hConsole, msg, wParam, lParam); } break; @@ -1198,23 +1199,29 @@ HWND initgraph_win32(int w, int h, int flag, LPCTSTR strWndTitle, bool(*WindowPr } } - -EASY_WIN32_END - ////////////****** �������� ******//////////// -void HpSleep(int ms) +HBITMAP Image2Bitmap(IMAGE* img) { - static clock_t oldclock = clock(); // ��̬��������¼��һ�� tick + return CreateBitmap(img->getwidth(), img->getheight(), 1, 32, (void*)GetImageBuffer(img)); +} - oldclock += ms * CLOCKS_PER_SEC / 1000; // ���� tick +HICON Bitmap2Icon(HBITMAP hBmp) +{ + BITMAP bmp = {}; + GetObject(hBmp, sizeof(BITMAP), &bmp); - if (clock() > oldclock) // ����Ѿ���ʱ��������ʱ - oldclock = clock(); - else - while (clock() < oldclock) // ��ʱ - Sleep(1); // �ͷ� CPU ����Ȩ������ CPU ռ���� -// Sleep(0); // ���߾��ȡ����� CPU ռ���� + HBITMAP hbmMask = CreateCompatibleBitmap(GetDC(NULL), bmp.bmWidth, bmp.bmHeight); + + ICONINFO ii = { 0 }; + ii.fIcon = TRUE; + ii.hbmColor = hBmp; + ii.hbmMask = hbmMask; + + HICON hIcon = CreateIconIndirect(&ii); + DeleteObject(hbmMask); + + return hIcon; } void outtextxy_format(int x, int y, int _Size, const wchar_t* _Format, ...) @@ -1228,25 +1235,19 @@ void outtextxy_format(int x, int y, int _Size, const wchar_t* _Format, ...) delete buf; } -HBITMAP GetImageHBitmap(IMAGE* img) -{ - return CreateBitmap(img->getwidth(), img->getheight(), 1, 32, (void*)GetImageBuffer(img)); -} +EASY_WIN32_END -HICON HICONFromHBitmap(HBITMAP hBmp) +void HpSleep(int ms) { - BITMAP bmp = {}; - GetObject(hBmp, sizeof(BITMAP), &bmp); - - HBITMAP hbmMask = CreateCompatibleBitmap(GetDC(NULL), bmp.bmWidth, bmp.bmHeight); - - ICONINFO ii = { 0 }; - ii.fIcon = TRUE; - ii.hbmColor = hBmp; - ii.hbmMask = hbmMask; + static clock_t oldclock = clock(); // ��̬��������¼��һ�� tick - HICON hIcon = CreateIconIndirect(&ii); - DeleteObject(hbmMask); + oldclock += ms * CLOCKS_PER_SEC / 1000; // ���� tick - return hIcon; + if (clock() > oldclock) // ����Ѿ���ʱ��������ʱ + oldclock = clock(); + else + while (clock() < oldclock) // ��ʱ + Sleep(1); // �ͷ� CPU ����Ȩ������ CPU ռ���� +// Sleep(0); // ���߾��ȡ����� CPU ռ���� } + diff --git a/lib/EasyWin32.h b/lib/EasyWin32.h index 98f3443..423a11c 100644 --- a/lib/EasyWin32.h +++ b/lib/EasyWin32.h @@ -4,11 +4,11 @@ // ���� EasyX ͼ�ο�� Win32 ��չ�� // // �������ߣ�huidong -// �桡������Ver 3.1.0 +// �桡������Ver 3.1.1 // ���뻷����VisualStudio 2022 | EasyX_20220610 | Windows 10 // ��Ŀ��ַ��https://github.com/zouhuidong/EasyWin32 // �������ڣ�2020.12.06 -// ����޸ģ�2022.07.15 +// ����޸ģ�2022.07.16 // #pragma once @@ -66,6 +66,65 @@ struct EasyWindow int nSkipPixels; // ����ʱ���������ص��������������ٻ棩 }; +// ����϶���Ϣ +// ���÷����� +// ��Ҫ�������Ϣѭ����ÿ�ζ����� UpdateMessage ���������Ϣ +// ���� isLeftDrag��isMiddleDrag��isRightDrag �����ж������϶�����갴�� +// ���� GetDragX��GetDragY ��ȡ����϶�ʱ�������ı仯�� +class MouseDrag +{ +private: + ExMessage old, msg; + int dx, dy; + bool lbtn = false, mbtn = false, rbtn = false; + bool newmsg = false; + + bool UpdateDragInfo(bool& btn, int msgid_down, int msgid_up) + { + if (newmsg) + { + if (btn) + { + dx = msg.x - old.x; + dy = msg.y - old.y; + old = msg; + if (msg.message == msgid_up) btn = false; + if (dx != 0 || dy != 0) return true; + else return false; + } + else + { + if (msg.message == msgid_down) + { + btn = true; + old = msg; + } + return false; + } + newmsg = false; + } + else + { + return false; + } + } + +public: + + void UpdateMessage(ExMessage m) + { + msg = m; + newmsg = true; + } + + bool isLeftDrag() { return UpdateDragInfo(lbtn, WM_LBUTTONDOWN, WM_LBUTTONUP); } + bool isMiddleDrag() { return UpdateDragInfo(mbtn, WM_MBUTTONDOWN, WM_MBUTTONUP); } + bool isRightDrag() { return UpdateDragInfo(rbtn, WM_RBUTTONDOWN, WM_RBUTTONUP); } + + int GetDragX() { return dx; } + int GetDragY() { return dy; } +}; + ////////////****** ������غ��� ******//////////// // ����֧�� win32 �Ļ�ͼ���ڣ�Ĭ��֧�ִ���˫����Ϣ�� @@ -241,6 +300,18 @@ MOUSEMSG To_MouseMsg(ExMessage msgEx); UINT GetExMessageType(ExMessage msg); +////////////****** ͼ������غ��� ******//////////// + +// �õ� IMAGE ����� HBITMAP +HBITMAP Image2Bitmap(IMAGE* img); + +// HBITMAP ת HICON +HICON Bitmap2Icon(HBITMAP hBmp); + +// ����Ļָ��λ�������ʽ���ı� +void outtextxy_format(int x, int y, int _Size, LPCTSTR lpFormat, ...); + + EASY_WIN32_END ////////////****** ����ָ��궨�� ******//////////// @@ -321,20 +392,10 @@ EASY_WIN32_END ////////////****** �������� ******//////////// - // ��ȷ��ʱ����(���Ծ�ȷ�� 1ms������ ��1ms) // by yangw80, 2011-5-4 void HpSleep(int ms); -// ����Ļָ��λ�������ʽ���ı� -void outtextxy_format(int x, int y, int _Size, LPCTSTR lpFormat, ...); - -// �õ� IMAGE ����� HBITMAP -HBITMAP GetImageHBitmap(IMAGE* img); - -// HBITMAP ת HICON -HICON HICONFromHBitmap(HBITMAP hBmp); - // ����ɫ����չ enum COLORS { DARKBLUE = RGB(0x00, 0x00, 0x8B),