Jump to content
  • Advertisement
Sign in to follow this  
White Scorpion

TextLabel class doesn't output to screen

This topic is 4868 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 guys. I'm currently writing the base part of a bigger program and I am actually creating control classes in order to speed up the writing process afterwards : I might create a lot of controls and I don't have access to my copy of MSVC++ so I can't build up in dialog mode. The following code is the code for a TextLabel, its purpose is simply to output text wherever I tell it to. The problem is quite simple right now: it doesn't output anything at all to the screen. I'll submit both the code for the Hdc class and the TextLabel class. Can you guys help me find the bug in the code, I'd really appreciate it.
class Hdc{
        Hdc(HDC, int, int);
        void Create(HDC, int, int);
        void Create(HWND);
        operator HDC() const;
        HDC& operator=(Hdc);
        HDC dev_context;
        HWND owner;
        HBITMAP compatible_bmp, default_bmp;

class IControl{
        virtual ~IControl() = 0;

        virtual void Update(HDC) = 0;
        virtual void Show(bool);
        virtual void Enable(bool);
        virtual void SetPosition(int, int);
        virtual void SetSize(int, int);
        HWND hwnd;
        int width, height, x, y;
        ControlState state;

class TextLabel : public IControl{
        TextLabel(HDC, int, int, char*);
        void Create(HDC, int, int, char*);
        virtual void Update(HDC);
        void SetText(char* txt) { text = txt; }
        Hdc GetDC() { return dev_context; }
        Font font_engine;
        Hdc dev_context;
        char* text;

                            class Hdc
    - Provide constructors and destructors;
    - Provide the create() method as an alternative to the constructor;
    - Supports casting to the HDC type;
    - Supports assignment to another Hdc object.

Hdc::Hdc(){ }

Hdc::Hdc(HDC dc, int cx, int cy){
    Create(dc, cx, cy);

Hdc::Hdc(HWND hwnd){
    if(owner != INVALID_HANDLE_VALUE) DeleteDC(dev_context);
    else ReleaseDC(owner, dev_context);

void Hdc::Create(HDC dc, int cx, int cy){
    dev_context = CreateCompatibleDC(dc);
    compatible_bmp = CreateCompatibleBitmap(dev_context, cx, cy);
    default_bmp = (HBITMAP)SelectObject(dev_context, compatible_bmp);

void Hdc::Create(HWND hwnd){
    dev_context = GetDC(hwnd);

Hdc::operator HDC() const{
    return dev_context;

HDC& Hdc::operator=(Hdc dc){
    dev_context = dc.dev_context; 
    owner = dc.owner; 

                            class TextLabel
    - Provide constructors and destructors;
    - Provide the create() method as an alternative to the constructor;
    - Updates the font engine through the mandatory Update() method;
    - Displays text in the designated area.

    // HACK: For some reason, the compiler complains about the inexistance
    // of those variables if they are used as a constructor parameter-list
    width = height = x = y = 0;

TextLabel::TextLabel(HDC hdc, int px, int py, char* text){
    Create(hdc, px, py, text);

void TextLabel::Create(HDC hdc, int px, int py, char* txt){
    text = txt;
    SIZE size;
    ZeroMemory(&size, sizeof(SIZE));
    GetTextExtentPoint32(dev_context, text, strlen(text), &size);
    x = px;
    y = py;
    width = size.cx;
    height = size.cy;
    dev_context.Create(hdc, width, height);
    SetTextColor(dev_context, RGB(125,125,125));
    SelectObject(dev_context, (HBRUSH)GetStockObject(COLOR_APPWORKSPACE+1));

void TextLabel::Update(HDC hdc){
    RECT rc;
    rc.left = x;
    rc.right = x + width;
    rc.top = y;
    rc.bottom = y + height;
    Rectangle(dev_context, x, y, width, height);
    ExtTextOut(dev_context, x, y, ETO_CLIPPED, 0, text, strlen(text), 0);
    BitBlt(hdc, x, y, width, height, dev_context, 0, 0, SRCCOPY);

Thank you.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!