在C ++ Builder中使用StyleHook

时间:2017-03-19 20:57:09

标签: delphi c++builder

我需要在C ++ Builder XE7中为TEdit创建一个样式钩子,以便覆盖样式颜色管理,如下面的Delphi example。有人可以在C ++ Builder中发布一个完整的例子(钩子单元,注册和示例表单)吗?谢谢!

1 个答案:

答案 0 :(得分:1)

Delphi示例的翻译看起来像这样:

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));