1.创建空项目
2.继承Slate的SLeafWidget类
class UIDESIGNER_API SColumnChart :public SLeafWidget
3.用slate的宏声明自定义控件
SLATE_DECLARE_WIDGET(SColumnChart, SLeafWidget)
4.用slate的宏创建构造函数
SLATE_BEGIN_ARGS(SColumnChart) {}
SLATE_END_ARGS()
void Construct(const FArguments& InArgs);
5.重写必要函数
Construct:用于构造控件结构
void SColumnChart::Construct(const SColumnChart::FArguments InArgs)
{
ChildSlot
.VAlign(VAlign_Top)
.HAlign(HAlign_Center)
[
SNew(SBox)
.WidthOverride(100)
.HeightOverride(100)
[
SNew(SButton)
.HAlign(HAlign_Center)
.VAlign(VAlign_Center)
// 设置按钮文本
.Text(FText::FromString("Click me!"))
]
];
}
PrivateRegisterAttributes::不知道有什么用但是必须实现
void SColumn::PrivateRegisterAttributes(FSlateAttributeInitializer& AttributeInitializer)
{
}
ComputeDesiredSize:不知道有什么用但是必须实现(可能是给父控件槽调整大小用的)
FVector2D SColumn::ComputeDesiredSize(float) const
{
return FVector2D(0,0);
}
6.重写OnPaint函数
不需要自定义绘制可以可以不用重写
如果需要自定义绘制重写的OnPaint必须包含绘制子控件的代码(否则将不会显示Construct函数构建的内容)
int32 SColumnChart::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const
{
//获取子控件布局信息
FArrangedChildren ArrangedChildren(EVisibility::Visible);
{
this->ArrangeChildren(AllottedGeometry, ArrangedChildren);
}
//存在子控件就绘制子控件
if (ArrangedChildren.Num() != 0)
{
//获取父控件的可用性
const bool bShouldBeEnable = ShouldBeEnabled(bParentEnabled);
//SCompoundWidget只能有一个子控件
check(ArrangedChildren.Num() == 1);
FArrangedWidget& TheChild = ArrangedChildren[0];
//控件格式
FWidgetStyle CompoundWidgetStyle = FWidgetStyle(InWidgetStyle)
.BlendColorAndOpacityTint(GetColorAndOpacity())
.SetForegroundColor(bShouldBeEnable ? GetForegroundColor() : GetDisabledForegroundColor());
{
#if WITH_VERY_VERBOSE_SLATE_STATS
SCOPE_CYCLE_COUNTER(STAT_ChildPaint);
#endif
LayerId = TheChild.Widget->Paint(Args, AllottedGeometry, MyCullingRect, OutDrawElements, LayerId, CompoundWidgetStyle, bShouldBeEnable);
}
}
return LayerId;
}