鼠标单击变双击补丁(含C++代码)

鼠标单击变双击了怎么办?难道只能换鼠标吗?下面这个程序可以在一定程度上解决这个问题。程序和代码可以在这里下载。


原理一看便知,代码如下:

/*
 *  @file  : MouseClickHotfix.cpp
 *  @author: shilyx
 *  @date  : 2014-04-13 19:33:42.540
 *  @note  : Generated by SlxTemplates
 */

#include 
#pragma warning(disable: 4786)
#include 
#include "resource.h"

#define CLASS_NAME TEXT("__MouseClickHotfix")

LRESULT CALLBACK LowLevelMouseProc(
    int nCode,
    WPARAM wParam,
    LPARAM lParam
    )
{
    if (nCode == HC_ACTION)
    {
        if (WM_LBUTTONDOWN == wParam && 0 != lParam)
        {
            static POINT s_lastPt = {-1, -1};
            static DWORD s_lastTickCount = 0;

            LPMSLLHOOKSTRUCT lpHookStruct = (LPMSLLHOOKSTRUCT)lParam;

            if (lpHookStruct->pt.x == s_lastPt.x &&
                lpHookStruct->pt.y == s_lastPt.y
                )
            {
                if (GetTickCount() - s_lastTickCount < 155)
                {
                    return 1;
                }
            }

            s_lastPt = lpHookStruct->pt;
            s_lastTickCount = GetTickCount();
        }
    }

    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_CREATE:
        return 0;

    case WM_CLOSE:
        DestroyWindow(hWnd);
        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    default:
        break;
    }

    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

void Run(HINSTANCE hInstance)
{
    WNDCLASSEX wcex = {sizeof(wcex)};

    wcex.lpszClassName  = CLASS_NAME;
    wcex.hInstance      = hInstance;
    wcex.lpfnWndProc    = WndProc;
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MAINFRAME));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);

    if (RegisterClassEx(&wcex))
    {
        HWND hWindow = CreateWindowEx(
            0,
            CLASS_NAME,
            CLASS_NAME,
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            NULL,
            NULL,
            hInstance,
            NULL
            );

        if (IsWindow(hWindow))
        {
//             ShowWindow(hWindow, SW_SHOW);
//             UpdateWindow(hWindow);

            HHOOK hHook = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, hInstance, 0);

            MSG msg;

            while (TRUE)
            {
                int nRet = GetMessage(&msg, NULL, 0, 0);

                if (nRet <= 0)
                {
                    break;
                }

                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }

            UnhookWindowsHookEx(hHook);
        }
    }
}

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
    if (IsWindow(FindWindow(CLASS_NAME, CLASS_NAME)))
    {
        MessageBox(NULL, TEXT("已经在运行!"), NULL, MB_SYSTEMMODAL | MB_ICONEXCLAMATION);
        return 0;
    }

    Run(hInstance);

    return 0;
}

#if !defined(_DEBUG) && _MSC_VER < 1300

#pragma comment(linker, "/Entry:s")
#pragma comment(linker, "/Opt:NoWin98")

void s()
{
    ExitProcess(_tWinMain(GetModuleHandle(NULL), NULL, NULL, 0));
}

#endif


你可能感兴趣的:(C++,Windows编程)