金币830
积分1476
注册时间2019-5-11
最后登录2024-9-29
|
使用Windows放大API,目前仅在1366x768分辨率下进行了测试。 您必须将游戏设置为全屏无边框。
您可以使用`键(波浪号)来切换叠加层。
源代码:
- // Ensure that the following definition is in effect before winuser.h is included.
- #define _WIN32_WINNT 0x0501
-
- #include <windows.h>
- #include <wincodec.h>
- #include <magnification.h>
-
- // Global variables and strings.
- HINSTANCE hInst;
- float MagFactor;
- const TCHAR WindowClassName[]= TEXT("MagnifierWindow");
- const TCHAR WindowTitle[]= TEXT("Screen Magnifier");
- const UINT timerInterval = 16; // close to the refresh rate @60hz
- HWND hwndMag;
- HWND hwndView;
- HWND hwndHost;
- RECT magWindowRect;
- HHOOK hHook;
- bool showMagnifier = true;
-
- // Forward declarations.
- LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
- ATOM RegisterHostWindowClass(HINSTANCE hInstance);
- BOOL SetupMagnifier(HINSTANCE hinst);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- void CALLBACK UpdateMagWindow(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
- float GetMagnificationFactor();
-
- //
- // FUNCTION: WinMain()
- //
- // PURPOSE: Entry point for the application.
- //
- int APIENTRY WinMain(HINSTANCE hInstance,
- HINSTANCE /*hPrevInstance*/,
- LPSTR /*lpCmdLine*/,
- int nCmdShow)
- {
- if (FALSE == MagInitialize())
- {
- return 0;
- }
- if (FALSE == SetupMagnifier(hInstance))
- {
- return 0;
- }
-
- ShowWindow(hwndHost, nCmdShow);
- UpdateWindow(hwndHost);
- //ShowCursor(false);
- // Create a timer to update the control.
- UINT_PTR timerId = SetTimer(hwndHost, 0, timerInterval, UpdateMagWindow);
-
- hHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hInstance, 0);
-
- // Main message loop.
- MSG msg;
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
-
- // Shut down.
- KillTimer(NULL, timerId);
- MagUninitialize();
- return (int) msg.wParam;
- }
-
-
- LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
- {
- PKBDLLHOOKSTRUCT press = (PKBDLLHOOKSTRUCT)lParam;
- if (wParam == WM_KEYDOWN && press->vkCode == VK_OEM_3) {
- showMagnifier = !showMagnifier;
-
- if (showMagnifier) {
- ShowWindow(hwndHost, SW_SHOW);
- }
- else {
- ShowWindow(hwndHost, SW_HIDE);
- }
- }
- return CallNextHookEx(hHook, nCode, wParam, lParam);
- }
-
-
- //
- // FUNCTION: HostWndProc()
- //
- // PURPOSE: Window procedure for the window that hosts the magnifier control.
- //
- LRESULT CALLBACK HostWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_KEYDOWN:
- if (wParam == VK_ESCAPE)
- {
- ShowCursor(true);
- PostQuitMessage(0);
- break;
- }
-
- case WM_SYSCOMMAND:
- return DefWindowProc(hWnd, message, wParam, lParam);
- break;
-
- case WM_DESTROY:
- ShowCursor(true);
- PostQuitMessage(0);
- break;
-
- case WM_SIZE:
- if ( hwndMag != NULL )
- {
- GetClientRect(hWnd, &magWindowRect);
- // Resize the control to fill the window.
- SetWindowPos(hwndMag, NULL,
- magWindowRect.left, magWindowRect.top, magWindowRect.right, magWindowRect.bottom, 0);
- }
- break;
-
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
-
- //
- // FUNCTION: RegisterHostWindowClass()
- //
- // PURPOSE: Registers the window class for the window that contains the magnification control.
- //
- ATOM RegisterHostWindowClass(HINSTANCE hInstance)
- {
- WNDCLASSEX wcex = {};
-
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = HostWndProc;
- wcex.hInstance = hInstance;
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(1 + COLOR_BTNFACE);
- wcex.lpszClassName = WindowClassName;
-
- return RegisterClassEx(&wcex);
- }
-
- //
- // FUNCTION: SetupMagnifier
- //
- // PURPOSE: Creates the windows and initializes magnification.
- //
- BOOL SetupMagnifier(HINSTANCE hinst)
- {
- int sx = GetSystemMetrics(SM_CXSCREEN);
- int sy = GetSystemMetrics(SM_CYSCREEN);
-
- // Create the host window.
- RegisterHostWindowClass(hinst);
- hwndHost = CreateWindowEx(WS_EX_TOPMOST | WS_EX_LAYERED | WS_EX_TRANSPARENT,
- WindowClassName, WindowTitle,
- WS_POPUP | WS_CLIPCHILDREN,
- sx / 2 + 100, sy / 2 - 180, 360, 360, NULL, NULL, hInst, NULL);
- if (!hwndHost)
- {
- return FALSE;
- }
- SetLayeredWindowAttributes(hwndHost, 0, (255 * 35) / 100, LWA_ALPHA);
-
- // Create a magnifier control that fills the client area.
- GetClientRect(hwndHost, &magWindowRect);
- hwndMag = CreateWindow(WC_MAGNIFIER, TEXT("MagnifierWindow"),
- WS_CHILD | MS_SHOWMAGNIFIEDCURSOR | WS_VISIBLE ,
- magWindowRect.left, magWindowRect.top, magWindowRect.right, magWindowRect.bottom, hwndHost, NULL, hInst, NULL );
- if (!hwndMag)
- {
- return FALSE;
- }
-
- MagFactor = GetMagnificationFactor();
- // Set the magnification factor.
- MAGTRANSFORM matrix;
- memset(&matrix, 0, sizeof(matrix));
- matrix.v[0][0] = MagFactor;
- matrix.v[1][1] = MagFactor;
- matrix.v[2][2] = 1.0f;
-
- BOOL ret = MagSetWindowTransform(hwndMag, &matrix);
-
- return ret;
- }
-
-
- //
- // FUNCTION: UpdateMagWindow()
- //
- // PURPOSE: Sets the source rectangle and updates the window. Called by a timer.
- //
- void CALLBACK UpdateMagWindow(HWND /*hwnd*/, UINT /*uMsg*/, UINT_PTR /*idEvent*/, DWORD /*dwTime*/)
- {
- if (showMagnifier) {
- RECT sourceRect;
- sourceRect.left = 1181;
- sourceRect.top = 578;
- sourceRect.right = 1181 + 181;
- sourceRect.bottom = 578 + 181;
-
- // Set the source rectangle for the magnifier control.
- MagSetWindowSource(hwndMag, sourceRect);
-
- // Reclaim topmost status, to prevent unmagnified menus from remaining in view.
- SetWindowPos(hwndHost, HWND_TOPMOST, 0, 0, 0, 0,
- SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
-
- // Force redraw.
- InvalidateRect(hwndMag, NULL, TRUE);
- }
- }
-
-
- float GetMagnificationFactor(){
- // For simplicity, the sample uses a constant magnification factor.
- return 2.0f;
- }
复制代码
|
|