Jump to content
  • Advertisement
Sign in to follow this  
chrisparton1991

Win32 c++ TextOut(), What's happening???

This topic is 4445 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. This is my first ever post on GDForums, so i dont know how it's gonna turn out. Anyway, I am learning the WIN32 API with c++. When I write a TextOut() function, it basically outputs every bit of quoted code in my program.[crying] Here's an image of the program(obvioiusly :P) wtf? Here's the source:
#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT   Ps;
    HDC          hDC;
    HBRUSH  NewBrush;

    NewBrush = CreateSolidBrush(RGB(255, 0, 0));

    switch(msg)
    {
        case WM_PAINT:
        hDC = BeginPaint(hwnd, &Ps);
        SelectObject(hDC, NewBrush);
      
        TextOut(hDC, 10, 200, "SEE? ", 100);
        EndPaint(hwnd, &Ps);
        DeleteObject(NewBrush);
        break;
        case WM_KEYDOWN:
        switch(wParam)
        {
            case VK_ESCAPE:
            DestroyWindow(hwnd);
            break;
        }
        break;
        case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
        PostQuitMessage(0);
        break;
        default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
        break;
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    LPCTSTR className = "myClass";
    WNDCLASSEX wc;

    wc.cbSize        = sizeof(wc);
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.cbWndExtra    = 0;
    wc.cbClsExtra    = 0;
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "myClass";
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 0);
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "ERROR! FAILED TO REGISTER CLASS!", "FATAL ERROR!", MB_ICONERROR | MB_OK);
        return 1;
    }

    HWND hwnd = CreateWindowEx(0, "myClass", "WINDOW TITLE", WS_OVERLAPPEDWINDOW, 0, 0, 500 + 7, 500 + 33 , NULL, NULL, hInstance, NULL);

    if(!hwnd)
    {
        MessageBox(NULL, "ERROR! FAILED TO CREATE WINDOW!", "FATAL ERROR!", MB_ICONERROR | MB_OK);
        return 1;
    }

    ShowWindow(hwnd, nShowCmd);

    MSG msg;

    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}




I realise that it's probably an extremely stupid mistake, and I have most likely made a complete fool out of myself, but... whatever. +--------------------------------------------------------------------------+ Thanks, please help, Chris Parton the first [Edited by - chrisparton1991 on July 19, 2006 5:53:21 AM]

Share this post


Link to post
Share on other sites
Advertisement
I think I found the problem. The last number in the function should be the length of the string.
TextOut(hDC, 10, 200, "SEE? ", 100); <- Original
TextOut(hDC, 10, 200, "SEE? ", 5); <- Fixed

Share this post


Link to post
Share on other sites
TextOut(hDC, 10, 200, "SEE? ", 100);
The last parameter is the length of the string. You're telling it there's 100 characters there, when there's actually only 5. So you have a buffer overflow. Change the last parameter to 5, or better, change it to -1 (Means this is a null terminated string)

EDIT: Too late...

Share this post


Link to post
Share on other sites

Ahh! Thanks!

The tutorial I was using was very unclear on the meaning of the values. I thought it was the size in pixels!

I knew it'd be some stupid mistake.

Thanks again!

Share this post


Link to post
Share on other sites
Chris - welcome to GameDev, but when you post everything in bold, it sounds like you are shouting everything. It is still before 9am in the UK and my head hurts [smile].

Share this post


Link to post
Share on other sites
No problem. BTW, the ususal approach with TextOut would be something like:

const char *s="SEE? ";
TextOut(hDC, 10, 200, s, strlen(s));

or (better)

std::string s="SEE? ";
TextOut(hDC, 10, 200, s.c_str(), s.length());

HTH

Share this post


Link to post
Share on other sites
another tip: you also might want to call SetBkMode(hDC, TRANSPARENT); b4 calling TextOut as well.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!