用鼠标点击事件在屏幕上显示winapi图标

时间:2017-10-04 19:06:27

标签: c++ windows winapi

在WINAPI中,我看到一种病毒,只要发生鼠标点击事件,鼠标的x-y就会显示一个图标(红色的一个内部有一个白色的X)。每当我点击鼠标时,如何显示这样的图标?我知道如何用setwindowshookex钩住鼠标点击。是否有像下图所示的iconshow功能。

HHOOK msHOOK;
//getting the icon let's say from a rc file
HICON redIcon; 

msHOOK = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, NULL, 0);

LRESULT CALLBACK mouseProc(int nCode, WPARAM wPar, LPARAM lPar){
  IconShow(NULL, redIcon, xofMouse, yofMouse, 0);
  MessageBox(NULL, "Icon showed", "Cap", 0);
  return CallNextHookEx(NULL, nCode, wPar, lPar);
}

while(GetMessage(&msg, NULL, 0, 0) > 0){
  TranslateMessage(&msg);
}

谢谢

更新

#include <windows.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdParam, int iCmdShow){
    HWND wnd;
    MSG msg;
    WNDCLASSEX wc;
    wc.cbSize        = sizeof(WNDCLASSEX);
wc.style         = 0;
wc.lpfnWndProc   = WndProc;
wc.cbClsExtra    = 0;
wc.cbWndExtra    = 0;
wc.hInstance     = hInstance;
wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wc.lpszMenuName  = NULL;
wc.lpszClassName = "ClassName";
wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    wnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_LAYERED, "ClassName", "ss", NULL, CW_USEDEFAULT, CW_USEDEFAULT, 100, 100, NULL, NULL, NULL, NULL);
    SetLayeredWindowAttributes(wnd, RGB(255, 255, 0), 20, LWA_COLORKEY);
    ShowWindow(wnd, SW_SHOW);
    while (GetMessage(&msg, NULL, 0, 0) > 0) {
        TranslateMessage(&msg);
    }
    return 0;
}


LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        case WM_PAINT:
            //stuff here
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

1 个答案:

答案 0 :(得分:2)

您必须创建自己的永远在窗口以显示光标。

它可能需要是layered window,因此您可以使用UpdateLayeredWindow()和32位RGBA位图来使每个光标看起来都正确。

如果您只使用您所控制的特定光标,并且没有Alpha通道,则可以通过将图像按原样绘制到WM_ERASEBKGND或{{1消息处理程序,然后使用SetLayeredWindowAttributes(..., LWA_COLORKEY, ...)设置窗口的透明度颜色。