1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
//Date : 8 juin 05
//Auteurs : Gard Sébastien - Martin Grégory
#include <vcl.h>
#pragma hdrstop
#include "CustomLabelOrientable.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TCustomLabelOrientable *)
{
new TCustomLabelOrientable(NULL) ;
}
namespace Customlabelorientable
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TCustomLabelOrientable)} ;
RegisterComponents("Standard", classes, 0) ;
}
}
__fastcall TCustomLabelOrientable::TCustomLabelOrientable(TComponent* Owner)
: TCustomLabel(Owner)
{
AutoSize = true ;
FAngle = 0 ;
//Si la font Tahoma n'est pas disponible sur le système, la structure
//LogFont utilise par défaut une autre police valable
FFontName = "Tahoma" ;
FFontColor = clBlack ;
FFontHeight = 16 ;
FFontBold = FW_NORMAL ;
FFontItalic = false ;
FFontStrikeOut = false ;
FFontUnderline = false ;
//Créé dynamiquement la liste des fonts disponible sur le système
FFontNameList = new TStringList() ;
for (int i = 0 ; i < Screen->Fonts->Count ; i++)
FFontNameList->Add(Screen->Fonts->Strings[i]) ;
//Remplit l'espace mémoire utilisé par l'objet LogFont de zéros
ZeroMemory(&LogFont, sizeof(LOGFONT)) ;
//Affecte rééllement les propriétés définies plus haut à l'objet LogFont
LogFontChanged() ;
}
__fastcall TCustomLabelOrientable::~TCustomLabelOrientable()
{
delete FFontNameList ;
}
void __fastcall TCustomLabelOrientable::SetAngle(unsigned int angle)
{
if (angle >= 360)
FAngle = 0 ;
else
FAngle = angle ;
LogFontChanged() ;
}
void __fastcall TCustomLabelOrientable::SetFontName(AnsiString name)
{
//Vérifie que le nom de la font existe bien sur le système.
int i = 0 ;
bool found = false ;
while (i < FFontNameList->Count && found == false)
{
if (name == FFontNameList->Strings[i])
found = true ;
i++ ;
}
if (found == true)
{
FFontName = name ;
LogFontChanged() ;
}
}
TStrings* __fastcall TCustomLabelOrientable::GetFontNameList()
{
return FFontNameList ;
}
void __fastcall TCustomLabelOrientable::SetFontNameList(TStrings *list)
{
FFontNameList->Assign(list) ;
}
void __fastcall TCustomLabelOrientable::SetFontColor(TColor color)
{
FFontColor = color ;
LogFontChanged() ;
}
void __fastcall TCustomLabelOrientable::SetFontHeight(unsigned int height)
{
FFontHeight = height ;
LogFontChanged() ;
}
bool __fastcall TCustomLabelOrientable::GetFontBold()
{
if (FFontBold == FW_BOLD)
return true ;
else
return false ;
}
void __fastcall TCustomLabelOrientable::SetFontBold(bool state)
{
state == true ? FFontBold = FW_BOLD : FFontBold = FW_NORMAL ;
LogFontChanged() ;
}
void __fastcall TCustomLabelOrientable::SetFontItalic(bool state)
{
FFontItalic = state ;
LogFontChanged() ;
}
void __fastcall TCustomLabelOrientable::SetFontStrikeOut(bool state)
{
FFontStrikeOut = state ;
LogFontChanged() ;
}
void __fastcall TCustomLabelOrientable::SetFontUnderline(bool state)
{
FFontUnderline = state ;
LogFontChanged() ;
}
//Applique les propriétés à l'objet LogFont, utilisé pour dessiner le texte
void __fastcall TCustomLabelOrientable::LogFontChanged()
{
//Choix du nom de la police. Le nom (lfFaceName)est un tableau de char,
//il est nécessaire d'effectuer ces opérations pour utiliser l'AnsiString
//de la propriété FFontName
char f[32] ;
strcpy(f, FFontName.c_str()) ;
strcpy(LogFont.lfFaceName, f) ;
//L'angle est en 10ème de degré. A cause d'une erreur ou d'une mauvaise
//gestion dans la structure, les angles de 90, 180 et 270 degrés ne sont
//pas appliqués (le résultat est similaire à un angle de 0°), il faut
//ajouter +1 à la valeur passée. Le résultat donne 901 dixième de degrés. La
//différence est imperceptible à l'oeil nu, mais évite le bug.
//Pour une meilleure solution, il faudrait avoir accès au code source
//de cette structure (fichier d'en-tête wingdi.h)
LogFont.lfEscapement = 10 * FAngle +1 ;
LogFont.lfOrientation = 10 * FAngle +1 ;
LogFont.lfHeight = FFontHeight ;
LogFont.lfWeight = FFontBold ;
LogFont.lfItalic = FFontItalic ;
LogFont.lfStrikeOut = FFontStrikeOut ;
LogFont.lfUnderline = FFontUnderline ;
Invalidate() ;
}
void __fastcall TCustomLabelOrientable::Paint(void)
{
//Clear le label en redessinant une rectangle par dessus
Canvas->Brush->Style = bsSolid ;
Canvas->Brush->Color = Color ;
Canvas->FillRect(Rect(0,0,Width,Height)) ;
Canvas->Brush->Style = bsClear ;
//Créé une Font avec les caractéristiques de LogFont et la passe au Canvas
Canvas->Font->Handle = CreateFontIndirect(&LogFont) ;
Canvas->Font->Color = FFontColor ;
int longueurCaption = Canvas->TextWidth(Caption) ;
int hauteurCaption = Canvas->TextHeight(Caption) ;
//converti l'angle en radian pour utiliser les méthodes cos et sin de math.h
double rad = M_PI * FAngle / 180.0 ;
//Ajuste la taille du label en hauteur et largeur à la taille du texte
if (AutoSize == true)
{
//On ajoute +1 à cause de la convertion du résultat, nombre à virgule,
//en entier tel que demandé par la propriété Width et Height
Width = abs(cos(rad) * longueurCaption) +
abs(cos(M_PI * (90+FAngle) / 180.0) * hauteurCaption) +1 ;
Height = abs(sin(rad) * longueurCaption) +
abs(sin(M_PI * (90+FAngle) / 180.0) * hauteurCaption) +1 ;
}
//Défini les coordonnées X et Y à partir d'où sera dessiné le texte afin de
//l'avoir parfaitement centré dans le label, quel que soit son angle et sa
//taille
int posHor = Width/2.0 - (cos(rad) * longueurCaption/2.0) -
(sin(rad) * hauteurCaption/2.0) ;
int posVer = Height/2.0 - (cos(rad) * hauteurCaption/2.0) +
(sin(rad) * longueurCaption/2.0) ;
//Dessine le texte
Canvas->TextOut(posHor, posVer, Caption) ;
} |
Partager