现在,我试图在gdiplus中绘制叠加层,但是,当我启动程序时,它能够找到我们的窗口,但不会绘制叠加层线。我想念什么吗?文档建议我应该可以这样做。
#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";
}
您缺少对GdiplusStartup的呼叫。
此外,请删除BeginPaint和EndPaint调用。这只是要擦除窗口,所以您将看不到绘制的内容。
这是有效的方法:
#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";
}
证明: