Whicked Hayo
Whicked Hayo

Reputation: 1

SetWindowSubclass on Windows 10 Notepad

I've been trying to solve this little problem the whole day, and I can't figure out what's wrong. I suppose this is a simple problem for some. So I'm trying to Subclass Notepad with a procedure that will block a certain message from being handed to Notepad, in my example I used WM_MOUSELEAVE

Here's my take on this:

#include <windows.h>
#include <commctrl.h>
#include <iostream>

#pragma comment(lib, "Comctl32.lib")

// Subclass procedure to block WM_MOUSELEAVE
LRESULT CALLBACK SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    if (uMsg == WM_MOUSELEAVE)
    {
        std::cout << "WM_MOUSELEAVE blocked!" << std::endl;
        return 0; // Block the message
    }

    // Pass all other messages to the default window procedure
    return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}

int main()
{
    // Initialize common controls
    INITCOMMONCONTROLSEX icex = { sizeof(icex), ICC_WIN95_CLASSES };
    InitCommonControlsEx(&icex);

    // Find the Notepad window by its class name and window name
    HWND hwndNotepad = FindWindow(L"Notepad", NULL);
    if (hwndNotepad == NULL)
    {
        std::cerr << "Notepad window not found!" << std::endl;
        return 1;
    }

    // Subclass the Notepad window using SetWindowSubclass
    if (!SetWindowSubclass(hwndNotepad, SubclassProc, 1, 0))
    {
        std::cout << "Notpad HWND is " << hwndNotepad << std::endl;
        DWORD errorCode = GetLastError();
        std::cerr << "Failed to subclass Notepad window! Error code: " << errorCode << std::endl;
        // std::cerr << "Failed to subclass Notepad window!" << std::endl;
        return 1;
    }

    std::cout << "Successfully subclassed Notepad. Blocking WM_MOUSELEAVE..." << std::endl;

    // Message loop to keep the program running
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

I get the message "Failed to subclass Notepad window!" with errorCode showing 0 so I have no way of knowing what's going on.

I'm on 64bit architecture both Notepad and my cpp program.

I tried with an empty LRESULT CALLBACK SubclassProc() that always return 0, but seems nothing is working, which make me think I've got the syntax wrong.

Upvotes: 0

Views: 58

Answers (0)

Related Questions