Sign in to follow this  
White Scorpion

TextLabel class doesn't output to screen

Recommended Posts

White Scorpion    148
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{
    public:
        Hdc();
        Hdc(HDC, int, int);
        Hdc(HWND);
        ~Hdc();
        
        void Create(HDC, int, int);
        void Create(HWND);
        operator HDC() const;
        HDC& operator=(Hdc);
    private:
        HDC dev_context;
        HWND owner;
        HBITMAP compatible_bmp, default_bmp;
};

class IControl{
    public:
        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);
    protected:
        HWND hwnd;
        int width, height, x, y;
        ControlState state;
};

class TextLabel : public IControl{
    public:
        TextLabel();
        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; }
    private:
        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){
    Create(hwnd);
}
        
Hdc::~Hdc(){
    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.
********************************************************************/


TextLabel::TextLabel(){
    // 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this