 |
Club Builder Foro de C++Builder en español
|
| Ver tema anterior :: Ver tema siguiente |
| Autor |
Mensaje |
neg1414 Master

Registrado: 31 Ene 2007Mensajes: 284
|
Publicado: Sab 01 May, 2010 1:23 pm Asunto: Componente Boton con color |
|
|
Buenas ...
Alguien puede decirme donde puedo conseguir un componente que sea un Button al que se le pueda modificar el color.
Gracias.. |
|
| Volver arriba |
|
 |
escafandra Master

Registrado: 29 Nov 2007Mensajes: 625Ubicación: Madrid - España
|
Publicado: Sab 01 May, 2010 5:55 pm Asunto: |
|
|
Pasate por aquí: TColorBtn
Saludos. |
|
| Volver arriba |
|
 |
neg1414 Master

Registrado: 31 Ene 2007Mensajes: 284
|
Publicado: Lun 03 May, 2010 2:06 pm Asunto: |
|
|
He intentado "traducir" el codigo en Delphi a C++ Builder pero se ve que no tengo aun los conocimientos necesarios.
De todas forma Gracias por Contestarme. |
|
| Volver arriba |
|
 |
escafandra Master

Registrado: 29 Nov 2007Mensajes: 625Ubicación: Madrid - España
|
Publicado: Lun 03 May, 2010 8:03 pm Asunto: |
|
|
Un buen ejercicio sería traducirlo, no es difícil, pero Builder es capaz de compilar ese código sin necesidad de ello.
Crea un archivo denominado ColorBtn.pas. En es coloca el código tal cual. Después Vete al menú Component->Install Component y en Unit File Name escoge el archivo ColorBtn.pas. Escoje un Package nuevo o existente y compila. En la pateta de Samples tendrás instalado el componente.
Saludos. |
|
| Volver arriba |
|
 |
_Leo Site Admin

Registrado: 11 Ago 2002Mensajes: 1875Ubicación: Lanzarote - Islas Canarias (España)
|
Publicado: Lun 03 May, 2010 9:07 pm Asunto: |
|
|
Como te dice escafandra puedes utilizarlo directamente con el código en pascal. De todas formas aquí va la versión en C++ (lo más literal posible para que sea fácil de comparar):
ColorBtn.h | Código: | //---------------------------------------------------------------------------
#ifndef ColorBtnH
#define ColorBtnH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Buttons.hpp>
//---------------------------------------------------------------------------
class PACKAGE TColorBtn : public TButton
{
typedef TButton inherited;
private:
bool IsFocused;
TCanvas* FCanvas;
bool F3DFrame;
TColor FButtonColor;
void __fastcall Set3DFrame(bool Value);
void __fastcall SetButtonColor(TColor Value);
MESSAGE void __fastcall CNDrawItem(TWMDrawItem& Message);
MESSAGE void __fastcall WMLButtonDblClk(TMessage& Message);
void __fastcall DrawButtonText(const String Caption, TRect TRC,
TButtonState State, long BiDiFlags);
void __fastcall CalculateTextPosition(const String Caption,
TRect& TRC, long BiDiFlags);
protected:
virtual void __fastcall CreateParams(TCreateParams& Params);
virtual void __fastcall SetButtonStyle(bool ADefault);
public:
__fastcall TColorBtn(TComponent* AOwner);
virtual __fastcall ~TColorBtn();
__published:
__property TColor ButtonColor = {read=FButtonColor, write=SetButtonColor,
default=clBtnFace};
__property bool Frame3D = {read=F3DFrame, write=Set3DFrame, default=false};
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(CN_DRAWITEM, TWMDrawItem, CNDrawItem);
VCL_MESSAGE_HANDLER(WM_LBUTTONDBLCLK, TMessage, WMLButtonDblClk);
END_MESSAGE_MAP(inherited);
};
//---------------------------------------------------------------------------
#endif |
ColorBtn.cpp | Código: | //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "ColorBtn.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall TColorBtn::TColorBtn(TComponent* AOwner) : TButton(AOwner)
{
FCanvas = new TCanvas;
FButtonColor = clBtnFace;
F3DFrame = false;
}
//---------------------------------------------------------------------------
__fastcall TColorBtn::~TColorBtn()
{
delete FCanvas;
}
//---------------------------------------------------------------------------
void __fastcall TColorBtn::CreateParams(TCreateParams& Params)
{
inherited::CreateParams(Params);
Params.Style |= BS_OWNERDRAW;
}
//---------------------------------------------------------------------------
void __fastcall TColorBtn::Set3DFrame(bool Value)
{
if (F3DFrame != Value) F3DFrame = Value;
}
//---------------------------------------------------------------------------
void __fastcall TColorBtn::SetButtonColor(TColor Value)
{
if (FButtonColor != Value) {
FButtonColor = Value;
Invalidate();
}
}
//---------------------------------------------------------------------------
void __fastcall TColorBtn::WMLButtonDblClk(TMessage& Message)
{
Perform(WM_LBUTTONDOWN, Message.WParam, Message.LParam);
}
//---------------------------------------------------------------------------
void __fastcall TColorBtn::SetButtonStyle(bool ADefault)
{
if (IsFocused != ADefault) IsFocused = ADefault;
}
//---------------------------------------------------------------------------
void __fastcall TColorBtn::CNDrawItem(TWMDrawItem& Message)
{
TRect RC;
long Flags;
TButtonState State;
bool IsDown, IsDefault;
TDrawItemStruct* DrawItemStruct;
DrawItemStruct = Message.DrawItemStruct;
FCanvas->Handle = DrawItemStruct->hDC;
RC = ClientRect;
IsDown = DrawItemStruct->itemState & ODS_SELECTED;
IsDefault = DrawItemStruct->itemState & ODS_FOCUS;
if (!Enabled)
State = bsDisabled;
else if (IsDown)
State = bsDown;
else
State = bsUp;
Flags = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
if (IsDown) Flags |= DFCS_PUSHED;
if (DrawItemStruct->itemState & ODS_DISABLED) Flags |= DFCS_INACTIVE;
if (IsFocused || IsDefault) {
FCanvas->Pen->Color = clWindowFrame;
FCanvas->Pen->Width = 1;
FCanvas->Brush->Style = bsClear;
FCanvas->Rectangle(RC.Left, RC.Top, RC.Right, RC.Bottom);
InflateRect(&RC, -1, -1);
}
if (IsDown) {
FCanvas->Pen->Color = clBtnShadow;
FCanvas->Pen->Width = 1;
FCanvas->Rectangle(RC.Left, RC.Top, RC.Right, RC.Bottom);
InflateRect(&RC, -1, -1);
if (F3DFrame) {
FCanvas->Pen->Color = FButtonColor;
FCanvas->Pen->Width = 1;
DrawFrameControl(DrawItemStruct->hDC, &RC, DFC_BUTTON, Flags);
}
}
else
DrawFrameControl(DrawItemStruct->hDC, &RC, DFC_BUTTON, Flags);
FCanvas->Brush->Color = FButtonColor;
FCanvas->FillRect(RC);
InflateRect(&RC, 1, 1);
if (IsFocused) {
RC = ClientRect;
InflateRect(&RC, -1, -1);
}
if (IsDown) OffsetRect(&RC, 1, 1);
FCanvas->Font = Font;
DrawButtonText(Caption, RC, State, 0);
if (IsFocused && IsDefault) {
RC = ClientRect;
InflateRect(&RC, -4, -4);
FCanvas->Pen->Color = clWindowFrame;
DrawFocusRect(FCanvas->Handle, &RC);
}
FCanvas->Handle = 0;
}
//---------------------------------------------------------------------------
void __fastcall TColorBtn::CalculateTextPosition(const String Caption,
TRect& TRC, long BiDiFlags)
{
TRect TB;
TPoint TS, TP;
TB = Rect(0, 0, TRC.Right + TRC.Left, TRC.Top + TRC.Bottom);
DrawText(FCanvas->Handle, Caption.c_str(), Caption.Length(), &TB,
DT_CALCRECT | BiDiFlags);
TS = Point(TB.Right - TB.Left, TB.Bottom - TB.Top);
TP.x = ((TRC.Right - TRC.Left) - TS.x + 1) / 2;
TP.y = ((TRC.Bottom - TRC.Top) - TS.y + 1) / 2;
OffsetRect(&TB, TP.x + TRC.Left, TP.y + TRC.Top);
TRC = TB;
}
//---------------------------------------------------------------------------
void __fastcall TColorBtn::DrawButtonText(const String Caption, TRect TRC,
TButtonState State, long BiDiFlags)
{
CalculateTextPosition(Caption, TRC, BiDiFlags);
FCanvas->Brush->Style = bsClear;
if (State == bsDisabled) {
OffsetRect(&TRC, 1, 1);
FCanvas->Font->Color = clBtnHighlight;
DrawText(FCanvas->Handle, Caption.c_str(), Caption.Length(), &TRC,
DT_CENTER | DT_VCENTER | BiDiFlags);
OffsetRect(&TRC, -1, -1);
FCanvas->Font->Color = clBtnShadow;
DrawText(FCanvas->Handle, Caption.c_str(), Caption.Length(), &TRC,
DT_CENTER | DT_VCENTER | BiDiFlags);
} else {
DrawText(FCanvas->Handle, Caption.c_str(), Caption.Length(), &TRC,
DT_CENTER | DT_VCENTER | BiDiFlags);
}
}
//---------------------------------------------------------------------------
namespace Colorbtn
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TColorBtn)};
RegisterComponents("Samples", classes, 0);
}
}
//--------------------------------------------------------------------------- |
(Probado en las versiones 4, 6 y 2010) |
|
| Volver arriba |
|
 |
neg1414 Master

Registrado: 31 Ene 2007Mensajes: 284
|
Publicado: Mar 04 May, 2010 7:01 am Asunto: |
|
|
Muchisimas Gracias a los dos.... Sois unos Cracks
Gracias. |
|
| Volver arriba |
|
 |
|
|
No puedes publicar temas nuevos en este foro No puedes responder a temas en este foro No puedes editar tus mensajes en este foro No puedes borrar tus mensajes en este foro No puedes votar en las encuestas de este foro
|
© 2002–2026 Club Builder · Funciona con phpBB.
|