Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

C++ injected DLL draw text on screen over a window and follow window when it moves

  1. User

    C++ injected DLL draw text on screen over a window and follow window when it moves

  2. Assistant

    To draw text on the screen over a window and follow the window when it moves using a C++ injected DLL, you can use the Windows API functions and hooks. Below is an example:

    #include <Windows.h>
    #include <dwmapi.h>
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    
    HWND targetWindow = nullptr;
    RECT targetWindowRect = {};
    
    void DrawTextOnScreen(HDC hdc)
    {
        RECT rect = { 10, 10, 300, 50 };
        DrawText(hdc, L"Text to draw", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    }
    
    LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if (nCode >= 0)
        {
            CWPSTRUCT* cwp = (CWPSTRUCT*)lParam;
    
            if (cwp->message == WM_WINDOWPOSCHANGED && cwp->hwnd == targetWindow)
            {
                GetWindowRect(targetWindow, &targetWindowRect);
                InvalidateRect(nullptr, nullptr, TRUE);
            }
        }
    
        return CallNextHookEx(nullptr, nCode, wParam, lParam);
    }
    
    void InstallHooks()
    {
        HHOOK hHook = SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, nullptr, GetCurrentThreadId());
    
        // Rest of the code
    }
    
    void UninstallHooks()
    {
        UnhookWindowsHookEx(hHook);
    
        // Rest of the code
    }
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
    
            // Draw text on screen
            DrawTextOnScreen(hdc);
    
            EndPaint(hwnd, &ps);
        }
        return 0;
    
        case WM_CLOSE:
            DestroyWindow(hwnd);
            return 0;
    
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
    
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASS wc = { 0 };
        wc.lpfnWndProc = WindowProc;
        wc.hInstance = hInstance;
        wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
        wc.lpszClassName = L"InjectedWindow";
        
        if (!RegisterClass(&wc))
            return -1;
    
        HWND hwnd = CreateWindowEx(
            WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOOLWINDOW,
            L"InjectedWindow", L"Injected Window",
            WS_POPUP,
            targetWindowRect.left, targetWindowRect.top,
            targetWindowRect.right - targetWindowRect.left, targetWindowRect.bottom - targetWindowRect.top,
            nullptr, nullptr, hInstance, nullptr);
    
        if (!hwnd)
            return -1;
    
        SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
    
        InstallHooks();
    
        ShowWindow(hwnd, SW_SHOW);
        UpdateWindow(hwnd);
    
        MSG msg;
        while (GetMessage(&msg, nullptr, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        UninstallHooks();
    
        return 0;
    }
    

    In this code, CallWndProc hook is used to detect the movement of the target window. When the window is moved (WM_WINDOWPOSCHANGED message is received) the InvalidateRect function is called to redraw the text on the screen.

    The DrawTextOnScreen function is called in the WM_PAINT message to draw the desired text over the target window. SetLayeredWindowAttributes is used to make the injected window transparent.

    This code creates a new window (injectedWindow) that draws text on the screen and follows a specified targetWindow when it moves. You need to replace the targetWindow variable with the handle of the desired window.

  3. User

    C++ draw text on screen over a window and follow window when it moves

  4. Assistant

    To draw text on the screen over a window and have it follow the window when it moves in C++, you can use the Windows API functions. Here's an example implementation:

    #include <Windows.h>
    
    HWND targetWindow = nullptr;
    RECT targetWindowRect = {};
    
    void DrawTextOnScreen(HDC hdc)
    {
        RECT rect = { targetWindowRect.left + 10, targetWindowRect.top + 10,
                      targetWindowRect.left + 300, targetWindowRect.top + 50 };
    
        DrawText(hdc, L"Text to draw", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    }
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg) {
            case WM_PAINT: {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);
                
                // Draw text on screen
                DrawTextOnScreen(hdc);
                
                EndPaint(hwnd, &ps);
                break;
            }
            case WM_MOVE: {
                // Update the target window's position
                GetWindowRect(targetWindow, &targetWindowRect);
                break;
            }
            case WM_DESTROY: {
                PostQuitMessage(0);
                break;
            }
            default:
                return DefWindowProc(hwnd, uMsg, wParam, lParam);
        }
    
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        // Create a transparent window
        WNDCLASS wc = { 0 };
        wc.lpfnWndProc = WindowProc;
        wc.hInstance = hInstance;
        wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
        wc.lpszClassName = L"OverlayWindow";
    
        if (!RegisterClass(&wc))
            return -1;
    
        HWND hwnd = CreateWindowEx(
            WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE,
            L"OverlayWindow", L"Overlay Window",
            WS_POPUP,
            CW_USEDEFAULT, CW_USEDEFAULT,
            GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
            nullptr, nullptr, hInstance, nullptr
        );
    
        if (!hwnd)
            return -1;
    
        SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
    
        // Set the target window handle
        targetWindow = FindWindow(nullptr, L"Target Window");
    
        if (targetWindow != nullptr) {
            GetWindowRect(targetWindow, &targetWindowRect);
        }
    
        ShowWindow(hwnd, SW_SHOW);
        UpdateWindow(hwnd);
    
        MSG msg;
        while (GetMessage(&msg, nullptr, 0, 0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return 0;
    }
    

    In this code, a transparent overlay window (OverlayWindow) is created, and the DrawTextOnScreen function is called in the WM_PAINT message to draw the desired text over the targetWindow. The position of the targetWindow is updated in the WM_MOVE message to ensure that the text follows the window when it moves. You would need to replace "Target Window" with the appropriate window title or other identification of the window where you want to draw the text.

    Note: Make sure to replace "Target Window" in the code with the actual title of the window you want to draw the text on.

  5. User

    does this code work with C++ empty project

  6. Assistant

    Yes, this code can work in a C++ empty project. Just make sure to include the necessary headers and link with the required libraries.

    To set up an empty C++ project, follow these steps:

    1. Create a new empty C++ project in your preferred IDE (such as Visual Studio).
    2. Copy the provided code into your main source file, typically main.cpp.
    3. Include the necessary headers at the beginning of the file:
    #include <Windows.h>
    
    1. Make sure to link with the required libraries. Add the following library to your linker settings:
    Dwmapi.lib
    
    1. Replace "Target Window" with the actual window title or identification of the window you want to draw the text on.

    Once you have completed these steps, you can build and run the project. When the target window is found, the overlay window will appear, and the text will be drawn over the target window, following its movements.

  7. User

    how to add dwmapi

  8. Assistant

    To add the dwmapi library to your project, follow these steps:

    1. Open your project in your IDE, such as Visual Studio.
    2. Right-click on your project in the Solution Explorer and select "Properties".
    3. In the Project properties window, navigate to "Configuration Properties" -> "Linker" -> "Input".
    4. On the right side, you will find "Additional Dependencies". Click on the dropdown arrow and select "Edit...".
    5. In the new window, add dwmapi.lib to a new line (if it's not already present). Click on "OK" to save the changes.
    6. Click on "Apply" and then "OK" to close the Project properties window.

    By adding dwmapi.lib to the additional dependencies, your project should now be able to link with the dwmapi library.

    Please note that the availability of the dwmapi library depends on the version of Windows SDK you have installed. If you encounter any issues, make sure you have the correct SDK version installed.

  9. User

    how to set main source file in the C++ empty project

  10. Assistant

    To set the main source file in a C++ empty project, follow these steps:

    1. Create a new empty C++ project in your preferred IDE (such as Visual Studio).
    2. Right-click on the project in the Solution Explorer and select "Properties" from the context menu.
    3. In the Project properties window, navigate to "Configuration Properties" -> "General".
    4. In the "General" properties, you will find the "Source File(s)" or "Startup File" option.
    5. Click on the dropdown arrow next to "Source File(s)" or "Startup File" and select the file that you want to set as the main source file.
    6. Once you have selected the main source file, click on "Apply" and then "OK" to save the changes.

    Now, the selected file will be treated as the main source file for your C++ empty project. Any code you write in that file will be considered as the entry point for your program.

    Note that the specific steps and options may vary slightly depending on your IDE or version. The provided steps are based on Visual Studio, so if you are using a different IDE, the process might be slightly different.

  11. User

    step 4 is wrong

  12. Assistant

    I apologize for the confusion. In some IDEs, there may not be an explicit option to set the main source file in an empty project because the main source file is typically determined by the file name or project settings.

    Instead, you can follow these alternative steps:

    1. Create a new empty C++ project in your IDE.
    2. Add a new source file to the project by right-clicking on the project in the Solution Explorer and selecting "Add" -> "New Item" (or a similar option).
    3. Choose to add a new C++ source file, such as main.cpp, and click on "Add" or "OK" to create the file.
    4. Write your main C++ code in the newly created main.cpp file.
    5. Build and run the project.

    By default, IDEs typically treat the first source file (e.g., main.cpp) added to a project as the entry point or main source file. Therefore, it is unnecessary to specify the main source file explicitly in an empty project. Instead, you can create a new source file and start writing your main code in it.

  13. User

    LINK2019 error

  14. Assistant

    The LINK2019 error is a linker error that occurs when the linker cannot find the definition of a symbol or function referenced in your code. Here are some steps to troubleshoot and resolve the LINK2019 error:

    1. Check for Typographical Errors: Ensure that the function or symbol names in your code match the function or symbol names defined in the corresponding source or header files. Typos in the names can cause the linker to fail to find the definitions.

    2. Verify Function Signatures: Confirm that the function signatures (return type, parameter types, and order) in the function declarations match the function definitions. Mismatched signatures can cause the linker to fail.

    3. Include Necessary Libraries: If the error is related to an external library function, ensure that you have included the necessary library in your project. Go to the project properties and check the "Additional Dependencies" field to verify that the required library is included.

    4. Check Library Paths: If you are using an external library, validate that the library directory is correctly included in the "Additional Library Directories" field in the project properties.

    5. Rebuild the Entire Solution: Sometimes, the error may occur due to incomplete or outdated object files. Try cleaning and rebuilding the entire solution to ensure all files are up to date.

    6. Check Function Definitions: If the error relates to your own code, ensure that you have provided a definition for each declared function or symbol. Declarations without corresponding definitions can cause linker errors.

    7. Check Function Call: Verify that you are correctly calling the function or using the symbol in your code. Incorrect usage can lead to linker errors.

    8. Check for Conflicting Definitions: If multiple source files have a definition for the same function or symbol, it can cause conflicts. Make sure there are no conflicting definitions. Consider using header guards or #pragma once to prevent multiple inclusions.

    9. Check Compiler and Platform Compatibility: Ensure that the version of the compiler and libraries you are using is compatible with your platform. Incompatibilities can result in linker errors.

    By following these steps, you can identify and resolve the underlying cause of the LINK2019 error. Remember to carefully review your code, declarations, definitions, library dependencies, and make any necessary corrections or adjustments accordingly.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.