2

我正在尝试捕捉屏幕保护程序事件。这是我的代码:

#include "stdafx.h"
#include <iostream>
#include "stdio.h"

#include <Windows.h>

#include <strsafe.h>

HHOOK _hook;

LRESULT CALLBACK HookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode >= 0)
    {   
        LPMSG msg = (LPMSG)lParam;

        if(msg->message == WM_SYSCOMMAND)
        {
            if (msg->wParam == SC_SCREENSAVE)
            {
                std::cout<<"SC_SCREENSAVE\n";
            }
        }

    }

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

void SetHook()
{
    HINSTANCE hinstDLL = LoadLibrary(L"user32.dll");
    if (!(_hook = SetWindowsHookEx(WH_GETMESSAGE,HookCallback,hinstDLL,0)))
    {
        std::cout<<"Failed to install hook!\n";
    }
}

void ReleaseHook()
{
    UnhookWindowsHookEx(_hook);
}


int _tmain(int argc, _TCHAR* argv[])
{

    SetHook();


    // Don't mind this, it is a meaningless loop to keep a console application running.
    // I used this to test the keyboard hook functionality. If you want to test it, keep it in ;)
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {

    }
    ReleaseHook();
    return 0;
}

注册成功。但我没有赶上这个事件。甚至没有进入回调函数。我究竟做错了什么?

4

1 回答 1

-1

您是否正在尝试创建屏幕保护程序或程序来截屏桌面?如果您正在制作屏幕保护程序,请遵循这些。

屏幕保护程序是带有.scr扩展名的普通程序。屏幕保护程序应接受以下命令行参数。

  • \S - 以全屏模式运行
  • \P - 在预览对话框中运行
  • \C - 显示配置对话框

但是\P开关有点不同。它后面是冒号和系统对话框的窗口句柄。例如,它可能是/P:1234567890

于 2013-05-24T05:41:06.747 回答