So.. I want information on drawing on top of a DirectX game, like how Steam does it. I would consider naively trying to obtain a "device context" of the client area of the game's window, and then try some 2D drawing functions. D: I don't know if that's going to work too well lol.

Enough information to understand what's going on, and what needs to be done is really what I want, thanks.

---
This simple application draws a rectangle on most non-3D programs, but it didn't work with the video games I have tried it on.

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
	auto this_window = GetConsoleWindow();
	cout << "ShowWindow:" << ShowWindow(this_window,SW_HIDE) << endl;

	Sleep(5000);
	auto wnd = GetForegroundWindow();
	auto dc = GetDC(wnd);

	cout << "Rectangle:" << Rectangle(dc,0,0,200,200) << endl;

	cout << "ReleaseDC:" << ReleaseDC(wnd,dc) << endl;
}

Recommended Answers

All 2 Replies

It's an interesting topic, so I decided to investigate a bit.

The program you wrote does work with video games.
However, don't forget that in a video game the whole
window is redrawn many times a second. This means
your rectangle is visible merely for some milliseconds.

If you try something like this, you can actually see it:

#include <windows.h>

int main()
{
    HWND wnd;
    HDC dc;

    while (true)
    {
        if (GetAsyncKeyState(VK_ESCAPE) >> 15) break;

        wnd = GetForegroundWindow();
        dc = GetDC(wnd);

        Rectangle(dc, 0, 0, 200, 200);

        ReleaseDC(wnd, dc);

        Sleep(15);
    }
}

But there's a flickering problem, even if you remove the Sleep call.

I googled a bit and discovered the so called layered windows:
http://msdn.microsoft.com/en-us/library/ms997507.aspx

Using info from msdn, I whipped up a small test program:

#define _WIN32_WINNT 0x0500

#include <windows.h>

int main()
{
    HWND hwnd;
    HWND lwnd;
    HDC dc;

    SIZE size;
    POINT ptSrc = {0, 0};
    BLENDFUNCTION blend;

    lwnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TRANSPARENT |
                          WS_EX_TOOLWINDOW | WS_EX_TOPMOST,
                          0, 0, WS_POPUP | WS_VISIBLE,
                          0, 0, 200, 200, 0, 0, 0, 0);
    size.cx = 200;
    size.cy = 200;

    blend.BlendOp = AC_SRC_OVER;
    blend.BlendFlags = 0;
    blend.AlphaFormat = 0;
    blend.SourceConstantAlpha = 100;

    while (true)
    {
        if (GetAsyncKeyState(VK_ESCAPE) >> 15) break;

        dc = GetDC(lwnd);

        Ellipse(dc, 50, 50, 100, 100);

        ReleaseDC(lwnd,dc);

        hwnd = GetForegroundWindow();

        dc = GetDC(hwnd);

        UpdateLayeredWindow(lwnd, dc, 0, 0, 0, 0, 0, &blend, ULW_OPAQUE);

        ReleaseDC(hwnd,dc);

        Sleep(15);
    }
}

However, the flickering problem is still there...

The fact that the flickering remains even if I remove the Sleep call makes me believe that
this is a synchronization rather than a speed problem. I'm not familiar with the subject,
but I think a better solution would be to intercept the directx API calls somehow (perhaps
using the MS detours library) and draw your stuff right after directx draws its stuff.

Oh so it does... I'm not interested enough to put much more effort into it, I searched around for a library last night and today, and only found a couple that don't work on W7 64bit. :( bummer

For me the app. doesn't work in full-screen mode on Americas Army 2..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.