//--------------------------------------------------------------------------- #include #pragma hdrstop #include "TWLabel.h" #include #pragma package(smart_init) //--------------------------------------------------------------------------- // ValidCtrCheck is used to assure that the components created do not have // any pure virtual functions. // static inline void ValidCtrCheck(TWLabel *) { new TWLabel(NULL); } //--------------------------------------------------------------------------- void __fastcall TWBevel::SetWidth(int Width) { if(m_Width!=Width) { m_Width=Width; if(m_Change) m_Change(this); } } void __fastcall TWBevel::SetVisible(bool Visible) { if(m_Visible!=Visible) { m_Visible=Visible; if(m_Change) m_Change(this); } } void __fastcall TWBevel::SetFColor(TColor Color) { if(m_FColor!=Color) { m_FColor=Color; if(m_Change) m_Change(this); } } void __fastcall TWBevel::SetBColor(TColor Color) { if(m_BColor!=Color) { m_BColor=Color; if(m_Change) m_Change(this); } } void __fastcall TWBevel::DrawBevel(TCanvas *Canvas,TRect *pRect) { if ( Visible ) Frame3D(Canvas,*pRect,m_FColor,m_BColor,m_Width); } //--------------------------------------------------------------------------- void __fastcall TWStyle::SetStyle(WStyle Style) { if (m_Style!=Style) { m_Style=Style; if (m_Change) m_Change(this); } } HRGN __fastcall TWStyle::GetRegion(TRect *pRect) { switch (m_Style) { case WS_Rect : return CreateRectRgn(pRect->left,pRect->top,pRect->right,pRect->bottom); case WS_RRect : return CreateRoundRectRgn( pRect->left,pRect->top,pRect->right,pRect->bottom, m_Round,m_Round); case WS_Ellipse : return CreateEllipticRgn(pRect->left,pRect->top,pRect->right,pRect->bottom); } return NULL; } void __fastcall TWStyle::SetRound(int Round) { if ( m_Round!=Round) { m_Round=Round; if(m_Change) m_Change(this); } } //--------------------------------------------------------------------------- __fastcall TWGradient::TWGradient() : TPersistent() , m_FColor(clGray), m_BColor(clGray), m_GradType(GT_Horz) { m_pBmp = new Graphics::TBitmap(); m_pBmp->PixelFormat = pf32bit; } void __fastcall TWGradient::MakeGradient(void) { int Width = m_pBmp->Width; int Height = m_pBmp->Height; if ( Width==0 || Height==0 ) return; unsigned int Color; unsigned int *pixel; if ( FColor==BColor ) { Color = RGB( GetBValue(FColor), GetGValue(FColor), GetRValue(FColor) ); pixel = (unsigned int*)m_pBmp->ScanLine[0]; for(int x=0;xScanLine[0]; for(int y=1;yScanLine[y],pixel,sizeof(unsigned int)*Width); } else { double r=GetRValue(m_FColor),g=GetGValue(m_FColor),b=GetBValue(m_FColor); double dr = (GetRValue(m_BColor)-r); double dg = (GetGValue(m_BColor)-g); double db = (GetBValue(m_BColor)-b); switch ( m_GradType ) { case GT_Horz : dr /= Width; dg /= Width; db /= Width; pixel = (unsigned int*)m_pBmp->ScanLine[0]; for(int i=0;iScanLine[0]; for(int i=1;iScanLine[i],pixel,sizeof(unsigned int)*Width); break; case GT_Vert : dr /= Height; dg /= Height; db /= Height; for(int y=0;yScanLine[y]; for(int x=0;xWidth!=Width)||(m_pBmp->Height!=Height) ) { // m_pBmp->SetSize(Width,Height); for BSD2000 m_pBmp->Width = Width; m_pBmp->Height = Height; MakeGradient(); } } void __fastcall TWGradient::DrawGradient(TCanvas *Canvas, TRect *pRect) { BitBlt(Canvas->Handle,0,0,pRect->Width(),pRect->Height(), m_pBmp->Canvas->Handle,0,0,SRCCOPY); } void __fastcall TWGradient::SetGradType(WGradientType GradType) { m_GradType = GradType; MakeGradient(); if ( m_Change ) m_Change(this); } void __fastcall TWGradient::SetFColor(TColor FColor) { if (m_FColor!=FColor) { m_FColor = FColor; MakeGradient(); if ( m_Change ) m_Change(this); } } void __fastcall TWGradient::SetBColor(TColor BColor) { if (m_BColor!=BColor) { m_BColor = BColor; MakeGradient(); if ( m_Change ) m_Change(this); } } //--------------------------------------------------------------------------- void __fastcall TWBorder::SetColor(TColor Color) { if ( m_Color != Color ) { m_Color = Color; if (m_Change) m_Change(this); } } void __fastcall TWBorder::SetWidth(int Width) { m_Width = Width; if ( m_Change ) m_Change(this); } void __fastcall TWBorder::DrawBorder(TCanvas *Canvas, HRGN rgn) { if (m_Width==0) return; Canvas->Brush->Style = bsSolid; Canvas->Brush->Color = m_Color; FrameRgn(Canvas->Handle,rgn,Canvas->Brush->Handle,m_Width,m_Width); } //--------------------------------------------------------------------------- void __fastcall TWTextStyle::SetTextStyle(WTextStyle TextStyle) { if ( m_TextStyle != TextStyle ) { m_TextStyle=TextStyle; if ( m_Change ) m_Change(this); } } void __fastcall TWTextStyle::SetDepth(int Depth) { if ( m_Depth != Depth ) { m_Depth = Depth; if ( m_Change ) m_Change(this); } } void __fastcall TWTextStyle::SetColor(TColor Color) { if ( m_Color != Color ) { m_Color = Color; if ( m_Change ) m_Change(this); } } void __fastcall TWTextStyle::DrawText(TCanvas *Canvas, double x, double y, TStrings *sl, double dx, double dy) { TColor tColor; int rx,ry; for(int n=0; nCount; ++n) { String &line = sl->Strings[n]; rx = (int)( x + (dx*n) ); ry = (int)( y + (dy*n) ); tColor = Canvas->Font->Color; Canvas->Font->Color = m_Color; switch ( m_TextStyle ) { case TS_Outline : for(int i=-m_Depth;i<=m_Depth;++i) for(int j=-m_Depth;j<=m_Depth;++j) TextOut(Canvas->Handle,rx+i,ry+j,line.c_str(),line.Length()); break; case TS_Shadow : TextOut(Canvas->Handle,rx+m_Depth,ry+m_Depth,line.c_str(),line.Length()); break; } Canvas->Font->Color = tColor; TextOut(Canvas->Handle,rx,ry,line.c_str(),line.Length()); } } //--------------------------------------------------------------------------- __fastcall TWLabel::TWLabel(TComponent* Owner) : TGraphicControl(Owner) , m_Transparency(false) , m_Rotate(0) , m_FontHandle(NULL) { m_Bevel = new TWBevel(); m_Style = new TWStyle(); m_Border = new TWBorder(); m_Gradient = new TWGradient(); m_Font = new TFont(); m_TextStyle = new TWTextStyle(); m_Border->m_Change = OnChange; m_Gradient->m_Change = OnChange; m_Style->m_Change = OnChange; m_TextStyle->m_Change = OnChange; m_Bevel->m_Change = OnChange; m_Font->OnChange = OnFontChange; m_Caption = new TStringList(); ((TStringList*)m_Caption)->OnChange = OnChange; m_Caption->Text = this->Name; } __fastcall TWLabel::~TWLabel() { delete m_Border; delete m_Gradient; delete m_Style; delete m_Bevel; delete m_Font; delete m_TextStyle; } void __fastcall TWLabel::SetTextStyle(TWTextStyle *TextStyle) { m_TextStyle->Assign(TextStyle); Invalidate(); } void __fastcall TWLabel::SetTransparency(bool Transparency) { m_Transparency = Transparency; Invalidate(); } void __fastcall TWLabel::SetBevel(TWBevel *Bevel) { m_Bevel->Assign(Bevel); Invalidate(); } void __fastcall TWLabel::SetStyle(TWStyle *Style) { m_Style->Assign(Style); Invalidate(); } void __fastcall TWLabel::OnHover(TMessage &Msg) { if ( m_MouseEnter ) m_MouseEnter(this); } void __fastcall TWLabel::OnLeave(TMessage &Msg) { if ( m_MouseLeave ) m_MouseLeave(this); } void __fastcall TWLabel::SetGradient(TWGradient *Gradient) { m_Gradient->Assign(Gradient); Invalidate(); } void __fastcall TWLabel::SetBorder(TWBorder *border) { m_Border->Assign(border); Invalidate(); } void __fastcall TWLabel::OnChange(TObject *Sender) { Invalidate(); } void __fastcall TWLabel::SetRotate(int Rotate) { if ( m_Rotate != Rotate ) { m_Rotate=Rotate; OnFontChange(this); } } void __fastcall TWLabel::OnFontChange(TObject *Sender) { m_FontHandle = CreateFont(m_Font->Height,0,(m_Rotate*10)%3600, 0,m_Font->Style.Contains(fsBold) ? FW_BOLD : FW_NORMAL, m_Font->Style.Contains(fsItalic),m_Font->Style.Contains(fsUnderline), m_Font->Style.Contains(fsStrikeOut),m_Font->Charset,OUT_DEFAULT_PRECIS, CLIP_LH_ANGLES,DRAFT_QUALITY,m_Font->Pitch,m_Font->Name.c_str()); Invalidate(); } void __fastcall TWLabel::SetFont(TFont *Font) { m_Font->Assign(Font); } void __fastcall TWLabel::OnPaint(TMessage &Msg) { double x=0.0, y=0.0, dx=0.0, dy=0.0, SIN, COS; int TextMargin = m_Bevel->Width + m_Border->Width; int TextWidth=0,TextHeight=0,TotalHeight; TRect rect(ClientRect); // Set Clipping Region HRGN rgn = m_Style->GetRegion(&rect); OffsetRgn(rgn,Left,Top); SelectClipRgn(Canvas->Handle,rgn); // Draw Gradient if (!m_Transparency) { m_Gradient->SetSize(Width,Height); m_Gradient->DrawGradient(Canvas,&rect); } // Draw Caption // Color ´Â HFONT °´Ã¼¿Í »ó°üÀÌ ¾ø¾î¼­ // OnFontChange¿¡¼­´Â °ËÃâÀÌ ¾ÈµÇ¹Ç·Î µû·Î ´ëÀÔ SetTextColor() ÀÌ´ø°¡... -_-; Canvas->Font->Color = m_Font->Color; Canvas->Font->Handle = m_FontHandle; Canvas->Brush->Style = bsClear; for(int n=0;nCount;++n) { TextWidth = std::max(TextWidth, Canvas->TextWidth(m_Caption->Strings[n].c_str())); TextHeight = std::max(TextHeight, Canvas->TextHeight(m_Caption->Strings[n].c_str())); } TotalHeight = TextHeight * m_Caption->Count; if ( m_Rotate != 0) { SIN = sin(m_Rotate*M_PI/180.0); COS = cos(m_Rotate*M_PI/180.0); x = -0.5 * ( TextWidth*(COS-1.0) + (TotalHeight*SIN) ); y = +0.5 * ( TextWidth*SIN + TotalHeight*(1.0-COS) ); dx = SIN*TextHeight; dy = COS*TextHeight; } else { dy = TextHeight; } switch( m_HLayout ) { case HL_Left : x += rect.left+TextMargin; break; case HL_Center : x += (rect.Width()-TextWidth)/2.0; break; case HL_Right : x += rect.right-TextWidth-TextMargin; break; } switch( m_VLayout ) { case VL_Top : y += rect.top+TextMargin; break; case VL_Center : y += (rect.Height()-TotalHeight)/2.0; break; case VL_Bottom : y += rect.bottom-TotalHeight-TextMargin; break; } m_TextStyle->DrawText(Canvas, x, y, m_Caption, dx, dy); // Draw Bevel m_Bevel->DrawBevel(Canvas,&rect); // Draw Border if ( m_Bevel->Visible && m_Bevel->Width !=0 ) { DeleteObject(rgn); ZoomRect(&rect,-(m_Bevel->Width/6)); // Magic Number 6 -_-; rgn = m_Style->GetRegion(&rect); } else OffsetRgn(rgn,-Left,-Top); m_Border->DrawBorder(Canvas,rgn); // Reset Clipping Region DeleteObject(rgn); SelectClipRgn(Canvas->Handle,NULL); } void __fastcall TWLabel::SetCaption(TStrings *Caption) { m_Caption->Text = Caption->Text; } void __fastcall TWLabel::SetHorzAlign(WHLayout hl) { if ( m_HLayout!=hl ) { m_HLayout=hl; Invalidate(); } } void __fastcall TWLabel::SetVertAlign(WVLayout vl) { if ( m_VLayout!=vl) { m_VLayout=vl; Invalidate(); } } //--------------------------------------------------------------------------- namespace Twlabel { void __fastcall PACKAGE Register() { TComponentClass classes[1] = {__classid(TWLabel)}; RegisterComponents("WComponent", classes, 0); } } //---------------------------------------------------------------------------