|
hook文件.h
- #include "pch.h"
- #include "hook_test.h"
- #include "NewButton.h"
- HHOOK Q_hWndHook;
- HINSTANCE Q_hMod = NULL;
- LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam);
- BOOL __stdcall Hook_Start(DWORD dwThreadID, HMODULE hModule)
- {
- Q_hWndHook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)HookProc, Q_hMod, dwThreadID);
- return TRUE;
- }
- int SetWindowBackColor(HWND _hwnd) {
- // 获取窗口的客户区域
- RECT rect;
- CWnd* pWnd = CWnd::FromHandle(_hwnd);
- pWnd->GetClientRect(&rect);
- // 获取窗口的设备上下文
- HDC hdc = GetDC(_hwnd);
- //设置背景色
- SetBkColor(hdc, RGB(21, 21, 23));
- //使用背景色填充窗口的客户区域
- ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
- // 释放设备上下文
- ReleaseDC(_hwnd, hdc); //SetWindowTheme(0, L"Explorer", NULL);
- return 0;
- wchar_t ClassName[MAX_PATH] = _T("");
- GetClassName(_hwnd, ClassName, MAX_PATH);
- if (ClassName[0] == L'#') {
- CWnd* pWnd = CWnd::FromHandle(_hwnd);
- CDC* pDC = pWnd->GetDC();
- RECT rect;
- {
- pWnd->GetWindowRect(&rect);
- pWnd->ScreenToClient(&rect);
- pDC->FillSolidRect(&rect, RGB(21, 21, 23)); // 设置窗口背景颜色
- }
- {
- rect.left += 9; rect.right -= 9; rect.top += GetSystemMetrics(SM_CYCAPTION) + 9; rect.bottom -= 9;
- pDC->Draw3dRect(&rect, RGB(88, 111, 255), RGB(88, 111, 255));
- }
- pWnd->ReleaseDC(pDC);
- }
- return 0;
- }
- LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
- {
- CWPSTRUCT* pMsg = (CWPSTRUCT*)lParam;
- //printf("pMsg->message=%X\n", pMsg->message);
- if (pMsg->message == WM_PAINT) {
- //SetWindowBackColor(pMsg->hwnd);
- wchar_t ClassName[MAX_PATH] = _T("");
- GetClassName(pMsg->hwnd, ClassName, MAX_PATH);
- if (wcscmp(ClassName, _T("Button")) == 0) {
- CWnd* pWnd = CWnd::FromHandle(pMsg->hwnd);
- CWnd* pParent = pWnd->GetParent();
- int id = pWnd->GetDlgCtrlID();
- if (id != 0) {
- NewButton* pButton = (NewButton*)GetWindowLongPtr(pWnd->m_hWnd, GWLP_USERDATA);
- if (pButton == nullptr) {
- //还没有设置
- CString title;
- pWnd->GetWindowTextW(title);
- if (title == L"") {
- SetWindowLongPtr(pWnd->m_hWnd, GWLP_USERDATA, (LONG_PTR)-1);
- return CallNextHookEx(Q_hWndHook, nCode, wParam, lParam);
- }
- //printf("%d\n", pMsg->message);
- pButton = new NewButton();
- //printf("%p\n", pButton);
- pButton->s_info.id = id;
- pButton->s_info.hwnd = pMsg->hwnd;
- pButton->s_info.pWnd = pWnd;
- pButton->s_info.souProc = GetWindowLongPtrW(pWnd->m_hWnd, GWLP_WNDPROC);
- pButton->s_info.newProc = (LONG_PTR)NewButton::ButtonProc;
- SetWindowLongPtr(pWnd->m_hWnd, GWLP_WNDPROC, pButton->s_info.newProc);
- SetWindowLongPtr(pWnd->m_hWnd, GWLP_USERDATA, (LONG_PTR)pButton);
- //return 0;
- }
- else if (pButton != (NewButton*)-1) {
- ;// printf("%p\n", pButton);
- }
- }
- }
- else {
- //printf("%X 0x%X\n", nCode, pMsg->message);
- }
- }
- else if (pMsg->message == WM_CAPTURECHANGED) {
- CWnd* pWnd = CWnd::FromHandle(pMsg->hwnd);
-
- //pWnd->Invalidate();
- //SetWindowBackColor();
-
- return 0;
- }
- else if (pMsg->message == WM_DESTROY) {
-
- UnhookWindowsHookEx(Q_hWndHook);
- return 0;
- }
- return CallNextHookEx(Q_hWndHook, nCode, wParam, lParam);
- }
复制代码
.h文件
- #pragma once
- #include <afxwin.h>
- class NewButton
- {
- public:
- NewButton();
- ~NewButton();
- public:
- HCURSOR m_cursorFocus; //按钮焦点鼠标样式(手型)
- HCURSOR m_cursorDown; //普通鼠标样式
- struct {
- COLORREF currentColor; //当前颜色
- COLORREF textColor; //字体颜色
- COLORREF bgColor; //背景颜色
- COLORREF FocusColor; //焦点颜色
- COLORREF DownColor; //按下颜色
- }s_color;
- struct//_S_BUTTON_INFO_
- {
- int id = -1; //按钮ID
- HWND hwnd = 0; //句柄
- CWnd* pWnd = 0; //对象指针
- LONG_PTR souProc = 0; //原始proc
- LONG_PTR newProc = 0; //新的proc
- }s_info;
- public:
- static LRESULT CALLBACK ButtonProc(
- HWND hwnd,
- UINT uMsg,
- WPARAM wParam,
- LPARAM lParam)
- ;
- private:
- static void Paint(const NewButton* pButton);
- };
复制代码 .cpp
- #include "pch.h"
- #include "NewButton.h"
- NewButton::NewButton()
- {
- m_cursorFocus = LoadCursor(0, MAKEINTRESOURCE((LPWSTR)32649)/*IDC_HAND*/);
- m_cursorDown = LoadCursor(0, MAKEINTRESOURCE((LPWSTR)32514)/*IDC_HAND*/);
- s_color.textColor = RGB(255, 255, 255);
- s_color.bgColor = RGB(30, 100, 188);
- s_color.FocusColor = RGB(30, 70, 200);
- s_color.DownColor = RGB(30, 122, 222); //RGB(100, 222, 100);天然绿
- s_color.currentColor = s_color.bgColor;
- }
- NewButton::~NewButton()
- {
- }
- LRESULT NewButton::ButtonProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- NewButton* pButton = (NewButton*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
- if ((pButton != nullptr) && (pButton->s_info.hwnd == hwnd)) {
- switch (uMsg)
- {
- case WM_LBUTTONDOWN://按钮按下
- SetCursor(pButton->m_cursorDown);
- pButton->s_color.currentColor = pButton->s_color.DownColor;
- pButton->s_info.pWnd->Invalidate();//重绘
- break;
- case WM_LBUTTONUP://弹起
- SetWindowLongPtr(pButton->s_info.hwnd, GWLP_WNDPROC, pButton->s_info.souProc);
- pButton->s_info.pWnd->ShowWindow(SW_HIDE);
- pButton->s_info.pWnd->SendMessage(BM_CLICK);
- pButton->s_info.pWnd->ShowWindow(SW_SHOW);
- SetWindowLongPtr(pButton->s_info.hwnd, GWLP_WNDPROC, pButton->s_info.newProc);
- pButton->s_color.currentColor = pButton->s_color.bgColor;
- break;
- case WM_MOUSEMOVE://焦点
- SetCursor(pButton->m_cursorFocus);
- break;
- case WM_PAINT://绘制
- NewButton::Paint(pButton);
- break;
- case WM_DESTROY://结束
- SetWindowLongPtr(pButton->s_info.hwnd, GWLP_WNDPROC, pButton->s_info.souProc);
- SetWindowLongPtr(pButton->s_info.hwnd, GWLP_USERDATA, (LONG_PTR)nullptr);
- delete pButton;
- break;
- default:
- break;
- }
- }
- else {
- ;// printf("空的,不可能\n");
- }
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- void NewButton::Paint(const NewButton* pButton)
- {
- //获取按钮矩形位置
- RECT rect;
- pButton->s_info.pWnd->GetWindowRect(&rect);
- pButton->s_info.pWnd->ScreenToClient(&rect);
- //获取绘制对象
- CDC* pDC = pButton->s_info.pWnd->GetDC();
- //绘制控件框架
- //pDC->DrawFrameControl(&rect, DFC_BUTTON, 0);
- //选入按钮背景色
- CBrush brush(pButton->s_color.currentColor);
- CBrush* pOldBrush = pDC->SelectObject(&brush);
- //绘制一个圆角矩形
- pDC->RoundRect(&rect, CPoint(8, 8));
- //恢复原来的画刷
- pDC->SelectObject(pOldBrush);
- //获取按钮文本内容
- CString btnCaption = L"";
- pButton->s_info.pWnd->GetWindowText(btnCaption);
- //复制按钮矩形区域
- CRect textRect;
- textRect.CopyRect(&rect);
- //获得字符串尺寸
- CSize sz = pDC->GetTextExtent(btnCaption);
- //调整文本位置使其居中
- textRect.top += (textRect.Height() - sz.cy) / 2;
- //设置文本透明背景
- pDC->SetBkMode(TRANSPARENT);
- //设置文本颜色
- pDC->SetTextColor(pButton->s_color.textColor);
- //绘制文本内容
- pDC->DrawText(btnCaption, &textRect, DT_RIGHT | DT_CENTER | DT_BOTTOM);
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?加入我们
x
|