gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
C++ DLL draw text on screen over a window and follow window when it moves
- User
C++ DLL draw text on screen over a window and follow window when it moves
- Assistant
C++ DLL draw text on screen over a window and follow window when it moves
To draw text on the screen over a window and have it follow the window when it moves, you can use the Windows API functions and create a DLL in C++. Here's an example code that demonstrates this:
#include <Windows.h>
#include <string>
HWND hwnd = NULL;
std::wstring text = L"Hello, World!";
RECT clientRect;
extern "C" __declspec(dllexport) void SetWindowHandle(HWND handle)
{
hwnd = handle;
}
extern "C" __declspec(dllexport) void SetText(const wchar_t* str)
{
text = str;
}
BOOL CALLBACK EnumWindowsProc(HWND handle, LPARAM lparam)
{
DWORD currentProcessId;
GetWindowThreadProcessId(handle, ¤tProcessId);
DWORD targetProcessId;
GetWindowThreadProcessId(hwnd, &targetProcessId);
if (currentProcessId == targetProcessId)
{
hwnd = handle;
return FALSE;
}
return TRUE;
}
extern "C" __declspec(dllexport) void DrawTextOnWindow()
{
// Find the target window if not provided
if (hwnd == NULL)
{
EnumWindows(EnumWindowsProc, NULL);
if (hwnd == NULL)
return;
}
// Get the target window's client area position and size
GetClientRect(hwnd, &clientRect);
// Calculate the position to draw the text
int x = clientRect.left + (clientRect.right - clientRect.left) / 2;
int y = clientRect.top + (clientRect.bottom - clientRect.top) / 2;
// Get the target window's device context
HDC hdc = GetDC(hwnd);
// Select a font
HFONT font = CreateFont(30, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH, NULL);
SelectObject(hdc, font);
// Set the text color
SetTextColor(hdc, RGB(255, 0, 0));
// Set the background mode to transparent
SetBkMode(hdc, TRANSPARENT);
// Draw the text on the window
TextOut(hdc, x, y, text.c_str(), static_cast<int>(text.length()));
// Cleanup
DeleteObject(font);
ReleaseDC(hwnd, hdc);
}
#include <Windows.h>
#include <iostream>
typedef void (*SetWindowHandle)(HWND);
typedef void (*SetText)(const wchar_t*);
typedef void (*DrawTextOnWindow)();
int main()
{
HMODULE dllHandle = LoadLibrary(L"path_to_your_dll.dll");
if (dllHandle == NULL)
{
std::cout << "Failed to load DLL." << std::endl;
return 1;
}
SetWindowHandle setWindowHandle = reinterpret_cast<SetWindowHandle>(
GetProcAddress(dllHandle, "SetWindowHandle"));
SetText setText = reinterpret_cast<SetText>(
GetProcAddress(dllHandle, "SetText"));
DrawTextOnWindow drawTextOnWindow = reinterpret_cast<DrawTextOnWindow>(
GetProcAddress(dllHandle, "DrawTextOnWindow"));
HWND targetWindow = FindWindow(NULL, L"Window_Title");
if (targetWindow == NULL)
{
std::cout << "Failed to find target window." << std::endl;
return 1;
}
setWindowHandle(targetWindow);
setText(L"Hello, Window!");
while (true)
{
drawTextOnWindow();
Sleep(50); // Refresh rate of 20 frames per second
}
FreeLibrary(dllHandle);
return 0;
}
HMODULE dllHandle = LoadLibrary(L"path_to_your_dll.dll"); with the actual path to your DLL file.This will load the DLL and find the target window specified by the window title. It will then draw the specified text on the target window continuously, following its movement.
Not yet included in a published release.