如题

解决方案 »

  1.   

    #include <winable.h>
    The BlockInput function blocks keyboard and mouse input events from reaching applications. BOOL BlockInput(
      BOOL fBlockIt  // block option
    );
      

  2.   

    使用BlockInput有一个缺点,就是键盘操作也被阻塞住了。我下面的代码实现的就是你要的功能。1、用户鼠标操作失效
    2、每1秒模拟移动鼠标一次(向右向下各20个象素)你自己编译运行试试 (9x/me操作系统下不能用)。#define _WIN32_WINNT    0x400
    #define STRICT
    #define WIN32_LEAN_AND_MEAN#include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>DWORD   g_main_tid  = 0;
    HHOOK   g_ms_hook   = 0;BOOL CALLBACK con_handler (DWORD)
        {
            PostThreadMessage (g_main_tid, WM_QUIT, 0, 0);
            return TRUE;
        };LRESULT CALLBACK ms_proc (int code, WPARAM w, LPARAM l)
        {
            PMSLLHOOKSTRUCT p = (PMSLLHOOKSTRUCT)l;
            if ((p->flags & LLMHF_INJECTED) == 0)
            {
                return 1;
            };        return CallNextHookEx (g_ms_hook, code, w, l);
        };VOID CALLBACK TimerProc (HWND, UINT, UINT, DWORD)
        {
            mouse_event (MOUSEEVENTF_MOVE, 20, 20, 0, 0);
            return;
        };int main (void)
        {
            UINT timer_id;
            g_main_tid = GetCurrentThreadId ();
            SetConsoleCtrlHandler (&con_handler, TRUE);        g_ms_hook = SetWindowsHookEx (
                WH_MOUSE_LL,
                &ms_proc,
                GetModuleHandle (NULL),
                0);        if (g_ms_hook == NULL)
            {
                fprintf (stderr,
                    "SetWindowsHookEx failed with error %d\n",
                    GetLastError ());
                return 0;
            };        timer_id = SetTimer (NULL, 0, 1000, TimerProc);
            MSG msg;
            while (GetMessage (&msg, NULL, 0, 0))
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            };
            KillTimer (NULL, timer_id);
            UnhookWindowsHookEx (g_ms_hook);
            return 0;
        };注解一下,程序中使用的是低级鼠标钩子。LRESULT CALLBACK ms_proc (int code, WPARAM w, LPARAM l)
        {
            PMSLLHOOKSTRUCT p = (PMSLLHOOKSTRUCT)l;
            // 如果p->flags中LLMHF_INJECTED标志位非0,那么表示鼠标消
            // 息是由mouse_event 模拟的。否则就是用户鼠标操作产生的。        if ((p->flags & LLMHF_INJECTED) == 0)
            {   // 对于鼠标操作产生的鼠标消息,一律不给处理,返回一个非零值就可以了
                return 1;
            };        return CallNextHookEx (g_ms_hook, code, w, l);
        };// 在timerproc内使用mouse_event来模拟鼠标操作。
    VOID CALLBACK TimerProc (HWND, UINT, UINT, DWORD)
        {
            mouse_event (MOUSEEVENTF_MOVE, 20, 20, 0, 0);
            return;
        };退出请按Ctrl + C或Ctrl + Break
    因为鼠标被禁用了,只能这么退出了