[SOLVED]Help with DrawText()

Posts 1–15 of 19 · Page 1 of 2
[SOLVED]Help with DrawText()
Hey guys, well i am reading Charle Petzold's Programming windows, and i learned about the DrawText() and TextOut () functions, so i got an idea to make a program that would write on other windows, not the window i make, so here is my code, and for this project i made it a console app not a win32 for this app, here is my code:

Code:
#include <Windows.h>
#include <iostream>
int main()
{
	PAINTSTRUCT ps;
	HDC hdc;
	HWND ss = FindWindow(NULL, TEXT("Spider Solitaire"));
	RECT rect;
	if(ss == NULL)
		{
			std::cout<<"Window NOT Found.";
		}
		else if (!ss ==0)
		{
			std::cout<<"you're good to go.";
		}
	

	while(true)
	{
		hdc = BeginPaint(ss, &ps);
		GetClientRect(ss, &rect);
		DrawText(hdc, TEXT("YOU WON"), -1, &rect, DT_CENTER);
		EndPaint(ss, &ps);
		
	}
}
Can someone point out what is wrong with this. Cuz i am pretty frustrated.
I don't no, but you better use
Code:
using namespace std;
wow, you just pointed out how bad ur c++ skills are, i clearly wrote "std::cout<<"blahblah";" therefore i don't need "using namespace std;" unless i am lazy and don't want to waste 10 seconds to write std::


Can someone answer my question about the winapi part not c++.

Somebody like Void, HD, schim, brinuz, freedompeace, kallisti, melodia, people who actually know what i am talking about not just posting stupid crap.
Try using GetDC on the window you want to draw over. BeginPaint doesn't return the same device context that the window itself uses.

Post results.
nope didn't work, well it's not drawing on the spider solitaire window.

here is the code:
Code:
while(true)
	{
		hdc = GetDC(ss);
		BeginPaint(ss, &ps);
		GetClientRect(ss, &rect);
		DrawText(hdc, TEXT("YOU WON"), -1, &rect, DT_CENTER);
		EndPaint(ss, &ps);
		ReleaseDC(ss, hdc);
	}
I don't see the point of using BeginPaint here, also, try using TextOut instead. I remember I used TextOut to render text before.
ok i will try now.

ok not working still here is my code

Code:
 while(true)
	{
		if(GetAsyncKeyState(VK_LBUTTON) &1)
		{
			GetCursorPos(&p);
		hdc = GetDC(ss);
		std::cout<<p.x<<p.y;
		GetClientRect(ss, &rect);
		TextOut(hdc, p.x, p.y, TEXT("YOU WON"), 0);
		ReleaseDC(ss, hdc);
		}
	
		
	}
All i want is it to write YOU WON on the spider solitaire window

come on guys

if you guys can't help me then can someone give me an example of how you would write text ontop of another window?
really guys?
I'm out of ideas. |:
omg, i never thought it was that hard, you are writing like crazy programs that i can't even understand but i am writing a simple winapi program. Well, i guess i will wait for a solution.
I believe its the way you call DrawText (arguments )

You should always draw inside WM_PAINT.

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

int DrawOnSC(char szBuffer[], int bottom, int left, int right, int top, RECT& rect, HDC& hdc, HWND& hwnd, int& iLength) 
{
	SetRect(&rect, bottom, left, right + 34, top + 34);
        DrawText(hdc, szBuffer, iLength, &rect, 32);
	return 0;

}

int main()
{
	char WindowName[] = "Spider Solitaire";         
        TCHAR szBuffer[50] = {0};                
	RECT rect;                               
	HWND hwnd = FindWindow(0, WindowName);   
	HDC hdc = GetDC(hwnd);                   

	int iLength = 0;

        iLength = wsprintf(szBuffer, "YOU WON");

	if (!FindWindow(0, WindowName)) { MessageBox(0, WindowName, "Window not found", 0); exit(0); }
	
	while (true) {

        DrawOnSC(szBuffer, 0, 0, 0, 0, rect, hdc, hwnd, iLength);
		if ( ( (GetAsyncKeyState(VK_END)) ? 1 : 0) ) { return 0; }

  }



  return 0;

}
nope, doesn't work. Have you tried it on spider solitaire? on my comp it doesn't write anything on the window and i am getting pissed. lol
I've once used d3d to write on other windows, that worked pretty good for me, although it was slow. Lemme get the code for you...

Oops, I don't have the code anymore, But I remember it being verry easy, so try and do it with d3d (it is possible using wb_paint though) but I don't know how ... :S
CreateDC then draw on that
i have figured it out. I used GetDC(), then i just used BeginPaint(), but before the loop i made it sleep for 3 seconds and it worked. Thanks guys.
Posts 1–15 of 19 · Page 1 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us