简体   繁体   English

ANSI C和WinAPI:如何从钩子程序获取到窗口的句柄?

[英]ANSI C & WinAPI: how to get a handle to the window from hook procedure?

I'm writing a very simple program, which prints color of selected pixel. 我正在编写一个非常简单的程序,该程序可以打印选定像素的颜色。 Here is my code: 这是我的代码:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define UNICODE

LRESULT CALLBACK mouse_hook_low_level(int nCode, WPARAM wParam, LPARAM lParam)
{
    if(wParam == WM_MOUSEMOVE) {
        //Need to get a handle to the window!
        InvalidateRect(window, NULL, FALSE); 
        UpdateWindow(window);
    }
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

LRESULT CALLBACK window_process(HWND window, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC dc = GetDC(NULL);
    HDC hdc;
    HFONT font;
    PAINTSTRUCT ps;
    int r, g, b;
    POINT p;
    RECT background_rect, color_rect;
    wchar_t buffer[256];

    switch(message) {
        case WM_PAINT:
            GetCursorPos(&p);
            r = GetRValue(GetPixel(dc, p.x, p.y));
            g = GetGValue(GetPixel(dc, p.x, p.y));
            b = GetBValue(GetPixel(dc, p.x, p.y));
            hdc = BeginPaint(window, &ps);
            background_rect.left = 0;
            background_rect.right = 199;
            background_rect.top = 0;
            background_rect.bottom = 99;
            FillRect(hdc, &background_rect, (HBRUSH)(COLOR_WINDOW+1));
            font = CreateFont(
                14,
                0,
                0,
                0,
                FW_DONTCARE,
                FALSE,
                FALSE,
                FALSE,
                DEFAULT_CHARSET,
                OUT_OUTLINE_PRECIS,
                CLIP_DEFAULT_PRECIS,
                CLEARTYPE_QUALITY,
                VARIABLE_PITCH,
                TEXT("Times New Roman")
            );
            SelectObject(hdc, font);
            swprintf_s(buffer, 256, L"Coordinates: (%d, %d)", p.x, p.y);
            TextOut(hdc, 70, 10, buffer, wcslen(buffer));
            swprintf_s(buffer, 256, L"RGB: (%d, %d, %d)", r, g, b);
            TextOut(hdc, 70, 40, buffer,  wcslen(buffer));
            color_rect.left = 10;
            color_rect.right = 60;
            color_rect.top = 10;
            color_rect.bottom = 60;
            FillRect(hdc, &color_rect, (HBRUSH)CreateSolidBrush(RGB(r, g, b)));
            EndPaint(window, &ps);
            break;
        case WM_CLOSE:
            DestroyWindow(window);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            ReleaseDC(window, hdc);
            return DefWindowProc(window, message, wParam, lParam);
    }
    ReleaseDC(window, hdc);
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    LPCWSTR class_name = L"name";
    MSG message;
    WNDCLASSEX window_class;
    HWND window;
    HHOOK MouseHook;

    window_class.cbSize = sizeof(WNDCLASSEX);
    window_class.style = CS_HREDRAW | CS_VREDRAW;
    window_class.lpfnWndProc = window_process;
    window_class.cbClsExtra = 0;
    window_class.cbWndExtra = 0;
    window_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    window_class.hCursor = LoadCursor(NULL, IDC_ARROW);
    window_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    window_class.lpszMenuName = NULL;
    window_class.lpszClassName = class_name;
    window_class.hInstance = hInstance;
    window_class.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    RegisterClassEx(&window_class);
    window = CreateWindow(
        class_name,
        L"Pixel color",
        WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        200,
        100,
        NULL,
        NULL,
        hInstance,
        NULL
    );
    ShowWindow(window, SW_SHOWNORMAL);
    MouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouse_hook_low_level, hInstance, 0);
    while(GetMessage(&message, NULL, 0, 0)) {
        TranslateMessage(&message);
        DispatchMessage(&message);
    }
    UnhookWindowsHookEx(MouseHook);
    return message.wParam;
}

The question is: how can I get a handle to the window for further window update in hook procedure at line 9? 问题是:如何在第9行的挂钩过程中获取窗口的句柄以进行进一步的窗口更新? And what can you say generally about my code? 关于我的代码,您能大致说些什么? I'm a student and have a little C-programming experience, could you point out my errors? 我是一名学生,并且有一点C编程的经验,您能指出我的错误吗?

Thanks in advance. 提前致谢。

First, try WindowFromPoint . 首先,尝试WindowFromPoint If it fails to find a window, ie it's not your process window, then enumerate all top-level windows and all its child windows to find a topmost window under the mouse pointer. 如果找不到窗口,即不是您的处理窗口,则枚举所有顶级窗口及其所有子窗口,以在鼠标指针下找到最顶层的窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM