Static Element mit Autosize :-(



  • also ich würde gerne mein static elemente gerne resizen wenn sie einen neuen text bekommen aber das funzt irgendwie ich post einmal das was ich hab...

    das ist die header datei

    #ifndef _CLABEL_H_
    
    #define _CLABEL_H_
    
    /*** INCLUDES ******************************************************************/
    
    #include "Steuerelement.h"
    
    /*** CLASS *********************************************************************/
    
    class CLabel : public Steuerelement  {
    
    public:
    
        CLabel(HWND hwnd, char*  Caption, int xpos, int ypos, int xsize, int ysize);
    
        ~CLabel() {};
    
        void Caption(const char* Caption);
    
        long Get_ID(void); 
    
        void SetPos(int x,int y);
    
        virtual void Enable(bool enable);
    
        virtual void Visible(bool visible);
    
        HWND hLabel;
    
    private:
    
        virtual int GenerateID(void);
    
        int XPos,YPos;
    
        short cxChar,cyChar;
    
        std::string buf;
    
    };
    
    #endif
    

    das ist die cpp datei

    #include "CLabel.h"
    
    CLabel::CLabel(HWND hwnd, char*  Caption, int xpos, int ypos, int xsize, int ysize) {
    
        TEXTMETRIC tm;
    
        HDC hdc;
    
        hdc = GetDC(this->hLabel);
    
        GetTextMetrics(hdc,&tm);
    
        this->cxChar =  tm.tmAveCharWidth;
    
        this->cyChar =  tm.tmHeight + tm.tmExternalLeading;
    
        this->XPos = xpos;
    
        this->YPos = ypos;
    
        this->hLabel = CreateWindowEx (  NULL,
                                        "static",
                                        Caption,
                                        WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                                        this->XPos, this->YPos,
                                        xsize, ysize,
                                        hwnd,
                                        (HMENU)this->GenerateID(),
                                        (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
                                        NULL);
    
        ReleaseDC(hwnd,hdc);
    
        this->Caption(Caption);
    
    }
    
    int CLabel::GenerateID(void) {
    
        return this->ID++;
    
    }
    
    void CLabel::SetPos(int x, int y) {
    
        this->XPos = x;
    
        this->YPos = y;
    
        MoveWindow(this->hLabel, this->XPos, this->YPos, (buf.length()+1)*this->cxChar, this->cyChar, TRUE);
    
    }
    
    void CLabel::Caption(const char* Caption) {
    
        buf = Caption;  
    
        MoveWindow(this->hLabel, this->XPos, this->YPos,  (buf.length()+1)*this->cxChar, this->cyChar, TRUE);
    
        SetWindowText(this->hLabel,Caption);
    
    }
    
    long CLabel::Get_ID(void) {
    
        return GetWindowLong(this->hLabel,GWL_ID);
    
    }
    
    void CLabel::Enable(bool enable) {
    
        EnableWindow(this->hLabel,enable);
    
    }
    
    void CLabel::Visible(bool visible) {
    
        ShowWindow(this->hLabel, visible);
    
    }
    

    bitte hilfe



  • kA, aber fehlt vor dem buf nicht noch das this-> ?
    Versuch mal, ob's klappt, wenn du SetWindowText vor MoveWindow aufrufst 🙄



  • ne das is es nicht hab ja keinen fehler es funzt aber net so wie ich es will

    wenn ich zb das wort "M" sende dan sieht man das der hintegrung um 4 pixel breiter ist und je grösser das wort das breiter is die fläche die b´neben frei is 😞



  • @Wolfman: Du weißt, dass du das "this->" überall weglassen kannst?!



  • na klar weiss ich mach das trotzdem in meinen klassen zweck übersicht 😉 alte gewohnj´heit von java zeiten aber trotzdem pass die schriftbreite nicht die averagecharwidth gibt kennt da jemand ne lösung ich will so machen wie es in visual basic is bei den statics autosize=true ....bitte hilfe find einfach nix gescheits



  • buf.length()+1
    

    Versuch mal wstrlen(Caption).



  • wenn ich zb das wort "M" sende dan sieht man das der hintegrung um 4 pixel breiter ist und je grösser das wort das breiter is die fläche die b´neben frei is 😞

    Tut mir leid, aber ich versteh kein Wort.



  • Das liegt dann wohl daran, dass du eine Proportionalschrift verwendest und mit der durchschnittlichen Zeichenbreite rechnest! Schau dir mal GetTextExtentPoint32 an: damit sollte es klappen 😉

    Das war wohl wieder einer schneller 😉

    [ Dieser Beitrag wurde am 14.02.2003 um 11:37 Uhr von flenders editiert. ]



  • Versuchs mal mit GetTextExtentPoint32().



  • supi danke und es funzt poste mal die funktion 🙂

    void CLabel::Get_length(const char* Caption){
    
        HDC hdc;
    
        SIZE sz; 
    
        TEXTMETRIC tm;
    
        std::string tmp = Caption;
    
        hdc = GetDC(this->hLabel);
    
        GetTextMetrics(hdc,&tm);
    
        this->cxChar = 0;
    
        for(int i=0;i<tmp.length();i++){
    
            GetTextExtentPoint32(hdc, tmp.substr(i,i+1).c_str() ,1, &sz); 
    
            this->cxChar +=  sz.cx - tm.tmOverhang;
    
        }
    
        this->cyChar =  sz.cy;
    
        ReleaseDC(this->hLabel,hdc);
    
    }
    

    Ich hab das jetzt so gelöst das er die grösse nur für einen buchstabe ermittelt und immer zu cxChar hinzuaddiert.....wenn man den ganzen string übergibt mit wird es sons zu gross aber einzeln funzt es supa ...

    wens ne bessere lösung nur herdamit 🙂

    hat wer erfahrung mit GetCharWidth32?

    [ Dieser Beitrag wurde am 14.02.2003 um 13:37 Uhr von Wolfman editiert. ]



  • Naja hab noch ne frage mit der systemschrift das ja supa aber hab da probs mit kursiv bold und natürlich mit schriftart wechsel.......



  • such mal nach WM_SETFONT


Anmelden zum Antworten