关于c#:应用界面开发小技巧-如何在覆盖表单上显示自定义按钮

60次阅读

共计 4078 个字符,预计需要花费 11 分钟才能阅读完成。

点击获取工具 >>

留神:GitHub 上提供了残缺的示例我的项目,网址为:https://github.com/DevExpress-Examples/how-to-display-a-custom-button-on-an-overlay-form

概述

Overlay Form 是一个半透明的启动屏幕,他在后盾线程中运行并笼罩控件或表单来避免对其进行拜访,Overlay Form 仅蕴含一个期待指示器。

本示例演示如何显示:

  • 期待指示器下方的百分比标签;
  • 一个自定义按钮,该按钮终止已解决的工作并敞开 Overlay Form。

本示例使您能够更改标签文本、按钮字形以及单击按钮时执行的操作。

实现细节

您能够按以下形式自定义 Overlay Form 图形:

  • 从 OverlayWindowPainterBase 类继承它;
  • 重写 Draw 办法;
  • 将创立的对象作为参数传递给 SplashScreenManager.ShowOverlayForm 办法。

本示例应用 OverlayImagePainter 和 OverlayTextPainter 对象(OverlayWindowPainterBase 后辈),它们实现了图像和文本的绘制逻辑,并且您能够应用它们的属性来自定义图像和文本。

OverlayImagePainter 对象在 Overlay Form 的顶部核心绘制一个自定义图像,并解决该图像上的单击。您能够指定以下属性:

  • Image – 失常状态下的图像;
  • HoverImage – 处于悬停状态的图片;
  • ClickAction – 单击图像时执行的操作。

OverlayTextPainter 对象在期待指示器下方绘制一个自定义标签,您能够指定以下属性:

  • Text – 标签文本;
  • Font – 文本字体(默认应用 Tahoma,18);
  • Color – 文本色彩(默认为彩色)。

OverlayWindowCompositePainter 对象在 OverlayImagePainter 和 OverlayTextPainter 对象中组成绘图逻辑,该复合绘制器作为参数传递给 SplashScreenManager.ShowOverlayForm 办法。

显示 Overlay Form 时执行的操作是应用.Net Framework 类库(工作并行库(TPL))中可用的可勾销工作来实现的。

自定义按钮和标签地位

按钮图像显示在 Overlay Form 的顶部地方,标签显示在期待指示器下方,您能够重写以下办法来指定其地位:

  • OverlayTextPainter.CalcTextBounds — 返回一个矩形,该矩形指定标签的地位;
  • OverlayImagePainter.CalcImageBounds — 返回一个矩形,它指定按钮的地位。

每次须要从新绘制叠加表单时(例如,当用户调整重叠控件的大小时),都会调用这些办法,drawArgs 办法参数蕴含绘制笼罩图所需的信息。本示例应用以下属性:

  • Bounds — 获取 Overlay Form 边界;
  • ImageBounds — 获取期待指示器的范畴;

上面的代码段显示了如何更新以后示例来在自定义地位显示按钮和标签。

C#

`using DevExpress.Utils.Extensions;
using DevExpress.XtraSplashScreen;

class OverlayImagePainterEx : OverlayImagePainter {
public OverlayImagePainterEx(Image image, Image hoverImage = null, Action clickAction = null) : base(image, hoverImage, clickAction) {}
protected override Rectangle CalcImageBounds(OverlayLayeredWindowObjectInfoArgs drawArgs) {
int indent = 10;
return Image.Size.AlignWith(drawArgs.Bounds).WithY(indent).WithX(drawArgs.Bounds.Width – Image.Height – indent);
}
}

class OverlayTextPainterEx: OverlayTextPainter {
protected override Rectangle CalcTextBounds(OverlayLayeredWindowObjectInfoArgs drawArgs) {
Size textSz = CalcTextSize(drawArgs);
return textSz.AlignWith(drawArgs.Bounds).WithY(drawArgs.ImageBounds.Top – textSz.Height);
}
}

public partial class Form1 : RibbonForm {
//..
OverlayTextPainter overlayLabel;
OverlayImagePainter overlayButton;

public Form1() {
//…
this.overlayLabel = new OverlayTextPainterEx();
this.overlayButton = new OverlayImagePainterEx(buttonImage, hotButtonImage, OnCancelButtonClick);
InitializeComponent();
}
async void OnRunTaskItemClick(object sender, ItemClickEventArgs e) {
//…
//Pass the created descendants to the SplashScreenManager.ShowOverlayForm method.
IOverlaySplashScreenHandle overlayHandle = SplashScreenManager.ShowOverlayForm(contentPanel, customPainter: new OverlayWindowCompositePainter(overlayLabel, overlayButton));
//…
}
}`

VB.NET

`Imports DevExpress.XtraSplashScreen
Imports DevExpress.Utils.Extensions

Friend Class OverlayImagePainterEx
Inherits OverlayImagePainter

Public Sub New(ByVal image As Image, Optional ByVal hoverImage As Image = Nothing, Optional ByVal clickAction As Action = Nothing)
MyBase.New(image, hoverImage, clickAction)
End Sub
Protected Overrides Function CalcImageBounds(ByVal drawArgs As OverlayLayeredWindowObjectInfoArgs) As Rectangle
Dim indent As Integer = 10
Return Image.Size.AlignWith(drawArgs.Bounds).WithY(indent).WithX(drawArgs.Bounds.Width – Image.Height – indent)
End Function
End Class

Friend Class OverlayTextPainterEx
Inherits OverlayTextPainter

Protected Overrides Function CalcTextBounds(ByVal drawArgs As OverlayLayeredWindowObjectInfoArgs) As Rectangle
Dim textSz As Size = CalcTextSize(drawArgs)
Return textSz.AlignWith(drawArgs.Bounds).WithY(drawArgs.ImageBounds.Top – textSz.Height)
End Function
End Class

Partial Public Class Form1
Inherits RibbonForm

‘…
Private overlayLabel As OverlayTextPainter
Private overlayButton As OverlayImagePainter

Public Sub New()
‘…
Me.overlayLabel = New OverlayTextPainterEx()
Me.overlayButton = New OverlayImagePainterEx(buttonImage, hotButtonImage, AddressOf OnCancelButtonClick)
InitializeComponent()
End Sub

Private Async Sub OnRunTaskItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs) Handles biRunTask.ItemClick
‘…
‘Pass the created descendants to the SplashScreenManager.ShowOverlayForm method.
Dim overlayHandle As IOverlaySplashScreenHandle = SplashScreenManager.ShowOverlayForm(contentPanel, customPainter:=New OverlayWindowCompositePainter(overlayLabel, overlayButton))
‘…
End Sub
`
下图阐明了生成的布局。

正文完
 0