0

Right now I am attempting to draw overlays in gdiplus, however when I start my program it is able to locate our window but it wont draw my overlay line. Am I missing something? The documentation suggests that I should be able to do this.

#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
#include <iostream>

using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")




VOID OnPaint(HDC hdc)
{
    Graphics graphics(hdc);
    Pen pen(Color(255, 0, 0, 0), 5);
    graphics.DrawLine(&pen, 0, 0, 200, 100);
}
ULONG_PTR gdiplusToken;

int main() {

    //Untitled - Notepad
    HWND hWnd = FindWindow(NULL, TEXT("*Untitled - Notepad"));
    // In top of main
    GdiplusStartupInput gdiplusStartupInput;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    if (hWnd == 0) {
        std::cout << "[-] - Unable to locate window!\n";
        return 0;
    }
    std::cout << "[+] - Located Window, starting hook.\n";
    HDC hdc = GetDC(FindWindowA(NULL, "*Untitled - Notepad"));
    PAINTSTRUCT ps;
    hdc = BeginPaint(hWnd, &ps);
    if (hdc == ERROR) {
        std::cout << "[-] - An error occured\n";
        return 0;
    }
    OnPaint(hdc);
    Sleep(3000);
    EndPaint(hWnd, &ps);
    std::cout << "Finished Drawing\n";

}
brainlet
  • 41
  • 6

1 Answers1

1

You are missing a call to GdiplusStartup.

Also, take out that BeginPaint and EndPaint call. That's just going to erase the window so you won't see what you drew.

Here's what works:

#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
#include <iostream>

using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")
VOID OnPaint(HDC hdc)
{
    Graphics graphics(hdc);
    Pen pen(Color(255, 0, 0, 0), 5);
    graphics.DrawLine(&pen, 0, 0, 200, 100);
}

int main() {


    // GDI+ startup incantation
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    //Untitled - Notepad
    HWND hWnd = FindWindow(NULL, TEXT("Untitled - Notepad"));
    if (hWnd == 0) {
        std::cout << "[-] - Unable to locate window!\n";
        return 0;
    }
    std::cout << "[+] - Located Window, starting hook.\n";
    HDC hdc;
    hdc = GetDC(hWnd);
    std::cout << hdc;
    OnPaint(hdc);
    std::cout << "Finished Drawing\n";

}

Proof:

enter image description here

selbie
  • 100,020
  • 15
  • 103
  • 173
  • thank you first off, I listened to what you had said and added startup, however once adding it nothing changed so I ended up just taking your code and compiling it: https://imgur.com/a/6nGBPT9 here where the results. it seems to still not be writing. any idea on what could be causing this? – brainlet Apr 27 '20 at 02:02
  • Once you so much as click on the Notepad window, it immediate redraws itself - erasing what your program had done. Make sure your Notepad window is completely unobscured and not covered by your console window or Visual Studio before starting your program. – selbie Apr 27 '20 at 02:05
  • ahh thats why, its quickly erasing itself, once I stuck it into a while true loop it started working. Is there a better way of doing that instead of using a while true loop? – brainlet Apr 27 '20 at 03:07
  • What are you really trying to do? – selbie Apr 27 '20 at 03:38
  • My goal is to just learn how to draw in C++ I was wanting to create some art to be honest to improve my coding knowledge. – brainlet Apr 27 '20 at 15:08
  • Then start with the sample code Visual Studio creates for new desktop application. Then just add your drawing code to the WM_PAINT. In other words, draw on your own window, now someone elses. – selbie Apr 27 '20 at 17:15
  • don't forget to upvote or access this answer if you found it useful. – selbie Apr 28 '20 at 06:00