Using StyleHook in C++ Builder - delphi

I need to create a style hook for TEdit in C++ Builder XE7, in order to override the style color management, as the following Delphi example. Could somebody post a complete example in C++ Builder (hook unit, registration and example form)? Thanks!

A translation of the Delphi example would look something like this:
class TEditStyleHookColor : public TEditStyleHook
{
typedef TEditStyleHook inherited;
private:
void UpdateColors();
protected:
virtual void __fastcall WndProc(TMessage &Message);
public:
__fastcall TEditStyleHookColor(TWinControl *AControl);
};
#include <Vcl.Styles.hpp>
class TWinControlH : public TWinControl {};
__fastcall TEditStyleHookColor::TEditStyleHookColor(TWinControl *AControl)
: TEditStyleHook(AControl)
{
//call the UpdateColors method to use the custom colors
UpdateColors();
};
//Here you set the colors of the style hook
void TEditStyleHookColor::UpdateColors()
{
if (Control->Enabled)
{
Brush->Color = (static_cast<TWinControlH*>(Control)->Color; //use the Control color
FontColor = static_cast<TWinControlH*>(Control)->Font->Color;//use the Control font color
}
else
{
//if the control is disabled use the colors of the style
TCustomStyleServices *LStyle = StyleServices();
Brush->Color = LStyle->GetStyleColor(scEditDisabled);
FontColor = LStyle->GetStyleFontColor(sfEditBoxTextDisabled);
}
}
//Handle the messages of the control
void __fastcall TEditStyleHookColor::WndProc(TMessage &Message)
{
switch (Message.Msg)
{
case CN_CTLCOLORMSGBOX:
case CN_CTLCOLORSCROLLBAR:
case CN_CTLCOLORSTATIC:
{
//Get the colors
UpdateColors();
SetTextColor(reinterpret_cast<HDC>(Message.WParam), ColorToRGB(FontColor));
SetBkColor(reinterpret_cast<HDC>(Message.WParam), ColorToRGB(Brush->Color));
Message.Result = reinterpret_cast<LRESULT>(Brush->Handle);
Handled = true;
break;
}
case CM_ENABLEDCHANGED:
{
//Get the colors
UpdateColors();
Handled = false;
break;
}
default:
inherited::WndProc(Message);
break;
}
}
...
TStyleManager::Engine->RegisterStyleHook(__classid(TEdit), __classid(TEditStyleHookColor));
TStyleManager::Engine->RegisterStyleHook(__classid(TMaskEdit), __classid(TEditStyleHookColor));
TStyleManager::Engine->RegisterStyleHook(__classid(TLabeledEdit), __classid(TEditStyleHookColor));
TStyleManager::Engine->RegisterStyleHook(__classid(TButtonedEdit), __classid(TEditStyleHookColor));

Related

C++ Builder XE4, 10.2 Tokyo > TStreamWriter > clWhite cannot be copied

My environment:
RadStudio 10.2 Tokyo (and also XE4)
I was implementing a copy property method to copy TShape properties.
Following is what I implemented:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
// set Shape1 color to [clWhite]
Shape1->Brush->Color = clRed; // clWhite
Shape2->Brush->Color = clAqua;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::copyProperties(TControl *srcCtrl, TControl *dstCtrl)
{
// to Keep original names
String orgName_src = srcCtrl->Name;
String orgName_dst = dstCtrl->Name;
// copy properties
TMemoryStream *strm = new TMemoryStream;
Shape1->Name = L""; // to avoid source collision
try {
strm->WriteComponent(srcCtrl);
strm->Position = 0;
strm->ReadComponent(dstCtrl);
}
__finally
{
delete strm;
}
srcCtrl->Name = orgName_src;
dstCtrl->Name = orgName_dst;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
copyProperties((TControl *)Shape1, (TControl *)Shape2);
// shift to avoid position-overlapping
Shape2->Left = Shape1->Left + 150;
}
//---------------------------------------------------------------------------
The code seems work fine.
But there is a single case, in which the code does not work.
i.e. when the Brush->Color = clWhite for Shape1.
This bug? can be reproduced also for XE4.
I wonder why only the clWhite has this kind of bug? Other colors does not have this kind of bug.
There is no bug in the streaming. It is operating as designed. You are simply using it in a way that it is not intended for.
clWhite is the declared default value of the TBrush.Color property. The DFM streaming system does not stream properties that are currently set to their default values, unless those properties are declared as nodefault or stored=true. TBrush.Color is neither. So the current Brush.Color value will not be streamed when it is set to clWhite.
Consider using the RTTI system directly instead of using the DFM system to copy properties from one object to another. Then you can copy property values regardless of defaults, if you choose to do so. And you can opt to ignore the Name property without having to (re)store it each time.
For example:
#include <System.TypInfo.hpp>
void __fastcall TForm1::copyProperties(TControl *srcCtrl, TControl *dstCtrl)
{
PTypeInfo pDstTypeInfo = static_cast<PTypeInfo>(dstCtrl->ClassInfo());
PPropList srcPropList;
int srcPropCount = GetPropList(srcCtrl, srcPropList);
try
{
for (int i = 0; i < srcPropCount; ++i)
{
PPropInfo pSrcPropInfo = (*srcPropList)[i];
if (pSrcPropInfo->Name == "Name") continue;
PTypeInfo pSrcPropTypeInfo = *(pSrcPropInfo->PropType);
if (pSrcPropTypeInfo->Kind == tkClass)
{
PPropInfo pDstPropInfo = GetPropInfo(pDstTypeInfo, pSrcPropInfo->Name, TTypeKinds() << tkClass);
if (pDstPropInfo)
{
TPersistent *pDstObj = static_cast<TPersistent*>(GetObjectProp(dstCtrl, pDstPropInfo, __classid(TPersistent)));
if (pDstObj)
{
TPersistent *pSrcObj = static_cast<TPersistent*>(GetObjectProp(srcCtrl, pSrcPropInfo, __classid(TPersistent)));
pDstObj->Assign(pSrcObj);
}
}
}
else
{
PPropInfo pDstPropInfo = GetPropInfo(pDstTypeInfo, pSrcPropInfo->Name);
if (pDstPropInfo)
{
Variant value = GetPropValue(srcCtrl, pSrcPropInfo);
SetPropValue(dstCtrl, pDstPropInfo, value);
}
}
}
}
__finally
{
FreeMem(srcPropList);
}
}
Alternatively:
#include <System.Rtti.hpp>
void __fastcall TForm1::copyProperties(TControl *srcCtrl, TControl *dstCtrl)
{
TRttiContext ctx;
TRttiType *pSrcType = ctx.GetType(srcCtrl->ClassInfo());
TRttiType *pDstType = ctx.GetType(dstCtrl->ClassInfo());
DynamicArray<TRttiProperty*> srcProps = pSrcType->GetProperties();
for (int i = 0; i < srcProps.Length; ++i)
{
TRttiProperty *pSrcProp = srcProps[i];
if (pSrcProp->Name == L"Name") continue;
if (pSrcProp->PropertyType->TypeKind == tkClass)
{
TRttiProperty *pDstProp = pDstType->GetProperty(pSrcPropInfo->Name);
if ((pDstProp) && (pDstProp->PropertyType->TypeKind == tkClass))
{
TPersistent *pDstObj = dynamic_cast<TPersistent*>(pDstProp->GetValue(dstCtrl).AsObject());
if (pDstObj)
{
TPersistent *pSrcObj = dynamic_cast<TPersistent*>(pSrcProp->GetValue(srcCtrl).AsObject());
pDstObj->Assign(pSrcObj);
}
}
}
else
{
TRttiProperty *pDstProp = pDstType->GetProperty(pSrcPropInfo->Name);
if (pDstProp)
{
TValue value = pSrcProp->GetValue(srcCtrl);
pDstProp->SetValue(dstCtrl, value);
}
}
}
}

How to manually get TMouseButton in C++Builder

I am using C++Builder from Embarcadero Technology. The built in OnClick event handler does not identify if the mouse click is the left or right button. Is there a function I can call to manually fill the values for TMouseButton. Below is the OnClick event handler?
void __fastcall TForm::ListBox1Click(TObject *Sender)
{
TMouseButton Button;
Button = ???
}
As others have mentioned, you can use the OnMouseDown event to remember the current mouse button state for use in OnClick, eg.
private:
bool LButtonDown;
bool RButtonDown;
...
void __fastcall TForm1::ListBox1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
switch (Button) {
case mbLeft:
LButtonDown = true;
break;
case mbRight:
RButtonDown = true;
break;
}
}
void __fastcall TForm1::ListBox1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
switch (Button) {
case mbLeft:
LButtonDown = false;
break;
case mbRight:
RButtonDown = false;
break;
}
}
void __fastcall TForm::ListBox1Click(TObject *Sender)
{
if (LButtonDown) ...
if (RButtonDown) ...
}
If you don't want to do that way, you can use the Win32 API GetKeyState() or GetAsyncKeyState() function to query the current state of the mouse's left and right buttons, using the VK_LBUTTON and VK_RBUTTON virtual key codes, eg:
void __fastcall TForm::ListBox1Click(TObject *Sender)
{
if (GetKeyState(VK_LBUTTON)) ...
if (GetKeyState(VK_RBUTTON)) ...
}
The correct event to use for details of mouse click events is OnMouseDown (also OnMouseUp and OnMouseMove).
Override the event and then implement MouseDown event like this
void __fastcall TMyListView::MouseDown(System::Uitypes::TMouseButton Button, System::Classes::TShiftState Shift, int X, int Y)
{
if (Button == mbLeft){
}
if (Button == mbRight){
}
}
See also Vcl.Controls.TControl.OnMouseDown in Embarcadero's documentation.

How to know the Control is TLabel?

My Environment: C++ Builder XE4
how to copy all the TLabels parented with a TPanel on delphi to another TPanel?
I would like to implement above code in C++ Builder.
I do not know how to implement below in C++ Builder.
if ParentControl.Controls[i] is TLabel then
Are there any functions to get type as TLabel or some other?
You can use ClassType method as:
if(Controls[i]->ClassType() == __classid(TLabel))
{
...
}
Use dynamic_cast:
if (dynamic_cast<TLabel*>(ParentControl->Controls[i]) != NULL)
Here is a translation of that code:
void __fastcall CopyLabels(TWinControl *ParentControl, TWinControl *DestControl)
{
for(int i = 0; i < ParentControl->ControlCount; ++i)
{
if (dynamic_cast<TLabel*>(ParentControl->Controls[i]) != NULL)
{
TLabel *ALabel = new TLabel(DestControl);
ALabel->Parent = DestControl;
ALabel->Left = ParentControl->Controls[i]->Left;
ALabel->Top = ParentControl->Controls[i]->Top;
ALabel->Width = ParentControl->Controls[i]->Width;
ALabel->Height = ParentControl->Controls[i]->Height;
ALabel->Caption= static_cast<TLabel*>(ParentControl->Controls[i])->Caption;
//you can add manually more properties here like font or another
}
}
}
With that said, this would be slightly more efficient:
void __fastcall CopyLabels(TWinControl *ParentControl, TWinControl *DestControl)
{
int count = ParentControl->ControlCount;
for(int i = 0; i < count; ++i)
{
TLabel *SourceLabel = dynamic_cast<TLabel*>(ParentControl->Controls[i]);
if (SourceLabel != NULL)
{
TLabel *ALabel = new TLabel(DestControl);
ALabel->Parent = DestControl;
ALabel->SetBounds(SourceLabel->Left, SourceLabel->Top, SourceLabel->Width, SourceLabel->Height);
ALabel->Caption = SourceLabel->Caption;
//you can add manually more properties here like font or another
}
}
}
I found ClassName() method.
Following seems working.
static bool isTCheckBox(TControl *srcPtr)
{
if (srcPtr->ClassName() == L"TCheckBox") {
return true;
}
return false;
}

How to check if a component is TEdit type in c++builder?

How do I check through all the components of a form and verify that components are of type TEdit?
You can use the dynamic_cast operator.
Excuse me if I'm wrong, but won't embarcadero automatically add all form-component object pointers to the class definition ( in the header file )..
Such as:
class TFormSomeForm : public TForm
{
__published:
TEdit *SomeEditBox;
TEdit *AnotherEditBox;
...
}
Meaning that you can tell from the header which components are of type TEdit.
Or you can click on the components in the Design View and the Object Inspector will show the type.
My function sets Text property of all edits in a TWinControl and its children.
void __fastcall SetEditsText(TWinControl* winControl, UnicodeString editsText)
{
for (int c = 0; c < winControl->ControlCount; c++)
{
TControl* ctrl = winControl->Controls[c];
TWinControl* wc = dynamic_cast<TWinControl*>(ctrl);
// Check if it's grouping component
if (wc != NULL)
{
// Set edits of children
SetEditsText(wc, editsText);
}
else
{
if (ctrl->ClassType() == __classid(TEdit))
{
TEdit* ecomp = (TEdit*) ctrl;
ecomp->Text = editsText;
}
}
}
}
Using:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
SetEditsText(form1, ""); // Clear all edits
}

How can I programmatically save event handler and then set it in C++ Builder?

I need to temporary remove TFrame's OnExit and OnEnter events, so I'm trying to do following:
declare FEnterHandler and FExit Handler:
private:
// ...
TControl *FParentControl;
(__fastcall *(__closure)(TObject*))(TObject*) FEnterHandler;
(__fastcall *(__closure)(TObject*))(TObject*) FExitHandler;
// ...
and I intended to use them as typed downhere, but compilation failed on declaration.
__fastcall TProgressForm::TProgressForm(TComponent *O, TControl *PC)
: TForm(O), FMapProgressData()
{
FParentControl = PC;
if (FParentControl)
{
TFrame *frame = dynamic_cast<TFrame*>(FParentControl);
if (frame)
{
FEnterHandler = frame->OnEnter;
FExitHandler = frame->OnExit;
frame->OnEnter = 0;
frame->OnExit = 0;
}
FParentControl->Enabled = false;
}
}
//-------------------------------------------------------------------------
__fastcall TProgressForm::~TProgressForm()
{
if (FParentControl)
{
FParentControl->Enabled = true;
TFrame *frame = dynamic_cast<TFrame*>(FParentControl);
if (frame)
{
frame->OnEnter = FEnterHandler;
frame->OnExit = FExitHandler;
}
}
}
What am I doing wrong?
I don't know much C++Builder, but can't you just write
TNotifyEvent FEnterHandler;
TNotifyEvent FExitHandler;
? Looks much nicer and is less error-prone.
I'd add my own answer, because I found, that typedef from help is rather strange:
typedef void _fastcall (__closure *TNotifyEvent)(System::TObjectTObject Sender)
and written another, more clear.
typedef void (__closure __fastcall *TEventHandler)(TObject*);
//typedef TNotifyEvent TEventHandler;
TEventHandler FEnterHandler;
TEventHandler FExitHandler;
Of course, I recommend to use TNotifyEvent, it is really better.

Resources