注册 登录
编程论坛 C++教室

控制鼠标反方向移动如果编译成可执行文件?

happymao 发布于 2009-12-28 22:27, 1470 次点击
网上看到有控制鼠标反方向移动这个VC的代码,想编译成可执行文件,请哪位给编译一下?谢谢!

#define _WIN32_WINNT 0x400
#include <windows.h>
#pragma data_seg("Shared")
HHOOK NextHook = NULL;
SIZE Screen = {0, 0};
POINT pt = {0, 0};
#pragma data_seg()
HMODULE Module;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
Module = hModule;
return TRUE;
}
LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == 0)
{
PMSLLHOOKSTRUCT mhs = (PMSLLHOOKSTRUCT)lParam;

if (mhs->pt.x != pt.x || mhs->pt.y != pt.y)
{
pt.x = pt.x - (mhs->pt.x - pt.x);
pt.y = pt.y - (mhs->pt.y - pt.y);

if (pt.x < 0) pt.x = 0;
if (pt.y < 0) pt.y = 0;
if (pt.x >= Screen.cx) pt.x = Screen.cx - 1;
if (pt.y >= Screen.cy) pt.y = Screen.cy - 1;
SetCursorPos(pt.x, pt.y);
return TRUE;
}
}
return CallNextHookEx(NextHook, nCode, wParam, lParam);
}
__declspec(dllexport) void StartHook()
{
Screen.cx = GetSystemMetrics(SM_CXSCREEN);
Screen.cy = GetSystemMetrics(SM_CYSCREEN);
GetCursorPos(&pt);
NextHook = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, Module, 0);
}
__declspec(dllexport) void StopHook()
{
UnhookWindowsHookEx(NextHook);
}

 

 

急用这个,但不懂,哪位高手,帮助一下,编译成可执行文件吧,非常感谢!
9 回复
#2
happymao2009-12-29 22:53
不可行吗?
#3
yxwsbobo2009-12-30 18:26
他这个只能编译成DLL 然后调用



用我的就可以了
程序代码:
#include "windows.h"

DWORD g_main_tid = 0;
HHOOK g_kb_hook = 0;

LRESULT CALLBACK ms_proc (int code, WPARAM w, LPARAM l)
{
    if(code < 0|| (int)w != 512)
        return CallNextHookEx (g_kb_hook, code, w, l);
    PMSLLHOOKSTRUCT p = (PMSLLHOOKSTRUCT)l;
    POINT CurrentPT;
    GetCursorPos(&CurrentPT);
    p->pt.x = CurrentPT.x -(p->pt.x-CurrentPT.x);
    p->pt.y = CurrentPT.y -(p->pt.y-CurrentPT.y);
    SetCursorPos(p->pt.x, p->pt.y);
    return 1;
}


int main (void)
{
    g_main_tid = GetCurrentThreadId ();
    //SetConsoleCtrlHandler ((PHANDLER_ROUTINE)&con_handler, TRUE);
    g_kb_hook = SetWindowsHookEx (
            WH_MOUSE_LL,
            (HOOKPROC)&ms_proc,
            GetModuleHandle (NULL),
            0);
    if (g_kb_hook == NULL)
    {
        fprintf (stderr,
        "SetWindowsHookEx failed with error %d\n",
        ::GetLastError ());
        return 0;
    };
    // 消息循环是必须的,想知道原因可以查msdn
    MSG msg;
    while (GetMessage (&msg, NULL, 0, 0))
    {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    };
    UnhookWindowsHookEx (g_kb_hook);
    return 0;
};



[ 本帖最后由 yxwsbobo 于 2009-12-30 18:28 编辑 ]
#4
happymao2009-12-30 22:01
老大,你就直接帮我编译成可执行文件吧,谢谢你了!急啊急啊,编译不是这个错误就是那个错误 ,郁闷啊!
-------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(10) : error C2065: 'PMSLLHOOKSTRUCT' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(10) : error C2146: syntax error : missing ';' before identifier 'p'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(10) : error C2065: 'p' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(10) : error C2146: syntax error : missing ';' before identifier 'l'
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(13) : error C2227: left of '->pt' must point to class/struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(13) : error C2228: left of '.x' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(13) : error C2227: left of '->pt' must point to class/struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(13) : error C2228: left of '.x' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(14) : error C2227: left of '->pt' must point to class/struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(14) : error C2228: left of '.y' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(14) : error C2227: left of '->pt' must point to class/struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(14) : error C2228: left of '.y' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(15) : error C2227: left of '->pt' must point to class/struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(15) : error C2228: left of '.x' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(15) : error C2227: left of '->pt' must point to class/struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(15) : error C2228: left of '.y' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(25) : error C2065: 'WH_MOUSE_LL' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(31) : error C2065: 'fprintf' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\1\1.cpp(31) : error C2065: 'stderr' : undeclared identifier
执行 cl.exe 时出错.

急啊,帮帮忙吧!

[ 本帖最后由 happymao 于 2009-12-30 23:13 编辑 ]
#5
happymao2009-12-30 23:14
哪位好心人或者高手,帮助一下吧,直接帮我编译成可执行文件吧,成不?
#6
yxwsbobo2009-12-31 09:04
下载地址
http://d.


编译不通过2个头文件 都包含了吗
#include "windows.h"
#include "stdio.h"
#7
happymao2009-12-31 09:51
非常感谢楼上的老大,可惜,可能是动态链接库的原因,我这无法运行,提示:
由于应用程序配置不正确,应用程序未能启动。重新安装应用程序可能会纠正这个问题。
正在网上查找原因中……
#8
yxwsbobo2009-12-31 13:32
http://d.

重编译了一下
#9
happymao2009-12-31 14:56
嗯,可以运行了,非常感谢
#10
lishi11222010-01-19 11:48
能把源文件给我一份吗?
1