Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8666231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:40:35+00:00 2026-06-12T17:40:35+00:00

I’m writing a very simple program, which prints color of selected pixel. Here is

  • 0

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? 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?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:40:36+00:00Added an answer on June 12, 2026 at 5:40 pm

    First, try WindowFromPoint. If it fails to find a window, i.e. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.