C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

DrawText() & TextOut() Drawing text to external applications! Rate Topic: -----

#1 Hyper  Icon User is offline

  • Banned
  • PipPipPipPipPip

Reputation: 104
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08


Dream Kudos: 425

Share |

DrawText() & TextOut()

Post icon  Posted 04 November 2008 - 01:53 PM

There are two (out of six) possible GDI calls, TextOut and DrawText.
To aquire a DC (Device Context) of a program, you can first get the Window Handle of the application, then get the DC from the HWND.

Here's what I have, and it appears to not work with *anything*:

#include <iostream>
#include <windows.h>
#define WindowName "Untitled - Notepad"
 
using namespace std;
 
int main() {
 
    TCHAR szBuffer[20];
    int iLength = 0;
 
    HWND hwnd = FindWindow(0, WindowName); /* aquire the Window Handle */
    HDC hdc = GetDC(hwnd); /* aquire the Device Context FROM the Window Handle */
 
    RECT rect;
    rect.bottom = 50;
    rect.left = 50;
    rect.right = 50;
    rect.top = 50;
 
    iLength = wsprintf(szBuffer, "Hello World");
    cout << "szBuffer: " << szBuffer << "!";
 
    while (true) {
          /* supposed to draw out the text, but appears to not */
          DrawText(hdc, szBuffer, iLength, &rect, DT_CENTER);
 
          /* Hotkey to end the program when the "END" key is depressed */
          if ( ( (GetAsyncKeyState(VK_END)) ? 1 : 0) ) { return 0; }
    }
 
    cin.get();
    return 0;
}



My question is: How do you use DrawText to draw on an external application (or game)?
Was This Post Helpful? 0
  • +
  • -


#2 Hyper  Icon User is offline

  • Banned
  • PipPipPipPipPip

Reputation: 104
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08


Dream Kudos: 425

Re: DrawText() & TextOut()

Posted 04 November 2008 - 04:10 PM

I got it working now...! Horray for me! :D

#include <iostream>
#include <windows.h>

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[] = "Brood War";         /* The window to look for                           */
    TCHAR szBuffer[50] = {0};                /* Text to be outputted                             */
    RECT rect;                               /* The dimensions of DrawText()                     */
    HWND hwnd = FindWindow(0, WindowName);   /* Aquire the Window Handle                         */
    HDC hdc = GetDC(hwnd);                   /* Aquire the Device Context FROM the Window Handle */

    int iLength = 0;
    iLength = wsprintf(szBuffer, "Hello World!");

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

    while (true) {
          DrawOnSC(szBuffer, 50, 50, 150, 50, rect, hdc, hwnd, iLength);

          /* Hotkey to end the program when the "END" key is depressed */
          if ( ( (GetAsyncKeyState(VK_END)) ? 1 : 0) ) { return 0; }
    }

    return 0;
}



The main issue was: It was sending it to a child-window (stupid Windows)... So all this time it WAS sending it, just to wrote the wrong interface.
Was This Post Helpful? 0
  • +
  • -

#3 Hyper  Icon User is offline

  • Banned
  • PipPipPipPipPip

Reputation: 104
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08


Dream Kudos: 425

Re: DrawText() & TextOut()

Posted 04 November 2008 - 08:52 PM

How would you create a double buffer (double buffer, back buffer, whatever you want to refer to it as) for DrawText?

I'm only aware of how to do that for Blitting.
Was This Post Helpful? 0
  • +
  • -

#4 Hyper  Icon User is offline

  • Banned
  • PipPipPipPipPip

Reputation: 104
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08


Dream Kudos: 425

Re: DrawText() & TextOut()

Posted 05 November 2008 - 01:22 PM

No answers?

^ This is also known as a bump! ^
Was This Post Helpful? 0
  • +
  • -

#5 Hyper  Icon User is offline

  • Banned
  • PipPipPipPipPip

Reputation: 104
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08


Dream Kudos: 425

Re: DrawText() & TextOut()

Posted 07 November 2008 - 03:06 PM

Nobody has an answer?
Was This Post Helpful? 0
  • +
  • -

#6 James_Alex  Icon User is offline

  • D.I.C Head
  • Icon

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-August 09


Dream Kudos: 50

Re: DrawText() & TextOut()

Posted 21 August 2009 - 03:24 AM

View PostHyper, on 4 Nov, 2008 - 04:10 PM, said:

I got it working now...! Horray for me! :D

#include <iostream>
#include <windows.h>

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[] = "Brood War";         /* The window to look for                           */
    TCHAR szBuffer[50] = {0};                /* Text to be outputted                             */
    RECT rect;                               /* The dimensions of DrawText()                     */
    HWND hwnd = FindWindow(0, WindowName);   /* Aquire the Window Handle                         */
    HDC hdc = GetDC(hwnd);                   /* Aquire the Device Context FROM the Window Handle */

    int iLength = 0;
    iLength = wsprintf(szBuffer, "Hello World!");

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

    while (true) {
          DrawOnSC(szBuffer, 50, 50, 150, 50, rect, hdc, hwnd, iLength);

          /* Hotkey to end the program when the "END" key is depressed */
          if ( ( (GetAsyncKeyState(VK_END)) ? 1 : 0) ) { return 0; }
    }

    return 0;
}



The main issue was: It was sending it to a child-window (stupid Windows)... So all this time it WAS sending it, just to wrote the wrong interface.

thx that was really helpful and it creates a text
thx :D
Was This Post Helpful? 0
  • +
  • -

#7 jeff666  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: 6
  • View blog
  • Posts: 193
  • Joined: 30-December 08


Dream Kudos: 0

Re: DrawText() & TextOut()

Posted 21 August 2009 - 09:10 AM

This code has no sense.
You must always draw inside WM_PAINT on Windows.
Was This Post Helpful? 0
  • +
  • -

#8 James_Alex  Icon User is offline

  • D.I.C Head
  • Icon

Reputation: 4
  • View blog
  • Posts: 123
  • Joined: 21-August 09


Dream Kudos: 50

Re: DrawText() & TextOut()

Posted 21 August 2009 - 10:09 AM

but is there anyway to change the backround color and the text color lz ?
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

3 User(s) are reading this topic
0 members, 3 guests, 0 anonymous users