GDI Double Buffer w/ Transparency Driving Me Bloody Mad!

Hello, there!

As you can tell from the title I'm having a bit of a problem with this program I'm writing. It's a .dll trainer that will be injected into Skyrim. All the main functions work, memcpy, pointers, and such. I just can seem to get this GDI Text thing working. (I've only done C++ for a few months now, so I'm still very new to this.) Here's the GDI Function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void TexMess()
{
	hWnd = FindWindow(TEXT("Skyrim"), TEXT("Skyrim")); //Handle to game window
	hDC = GetDC(hWnd); //Get device context from window handle

	GetClientRect(hWnd, &Rec); //Create surface area

	hdcMem = CreateCompatibleDC(hDC); //Memory DC
	hbmMem = CreateCompatibleBitmap(hDC, Rec.right, Rec.bottom); //Memory bitmap

	hOld = SelectObject(hdcMem, hbmMem);
	
	backGRND = (HBRUSH)GetStockObject(NULL_BRUSH); //Supposed to be transparent
	FillRect(hdcMem, &Rec, backGRND); //Supposed to apply transparency
	SetBkMode(hdcMem, TRANSPARENT); //Text background transparent
	SetTextColor(hdcMem, 0x000000FF); // Red text
	DrawText(hdcMem, Text, wcslen(Text), &Rec, DT_CENTER); //Outputs the text

	BitBlt(hDC, 0, 0, Rec.right, Rec.bottom, hdcMem, 0, 0, SRCCOPY); //Copies back buffer to primary buffer

	//Take out the trash
	SelectObject(hdcMem, hOld);
	DeleteObject(hbmMem);
	DeleteDC(hdcMem);
	ReleaseDC(hWnd, hDC);
}


I'm trying to display a simple red line of text that will not change, and that has a fully transparent background, allowing me to see the game. The problem lies somewhere within setting the background transparency. Sometimes it will flicker depending on the placement of the statements. That's not the issue as I have had it working, just without the transparency. It has a black background image. Which brings me to my question, How do I set this properly so the background is transparent? I call TexMess() within the main loop "while(!GetAsyncKeyState(VK_ESCAPE))" in int main(), so it gets executed over and over again. If I don't, nothing shows up. Everything else works flawlessly. It just always has a completely black screen with the text on top of it. I'm at my wits end. Without double buffering it displays correctly but flickers bad. I would also really like to keep it GDI. None of that WM_PAINT or DirectX stuff please. ;) Anyhoo, all help is greatly appreciated. Thanks guys.
Without WM_PAINT you will always get unwanted effects since the drawing to the same window is done concurrently.

See:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms644898%28v=vs.85%29.aspx

With GWLP_WNDPROC you can set your own message handler function.

In your own message handler function you need to call the original function first where all the original drawing is done. Then you can do your own drawing (int case of WM_PAINT) on top with BeginPaint(...)/EndPaint(...) instead of GetDC(...)/ReleaseDC(...).

I would say that it is not that hard to do.
Topic archived. No new replies allowed.