如何在覆蓋表單上顯示自定義按鈕
注意:GitHub上提供了完整的示例項目,網(wǎng)址為:https://github.com/DevExpress-Examples/how-to-display-a-custom-button-on-an-overlay-form
概述
覆蓋表單是一個半透明的啟動界面,它在后臺線程中運行并覆蓋控件或表單來防止對其進行訪問,覆蓋表單僅包含一個等待指示器。
本示例演示如何顯示:
- 等待指示器下方的百分比標簽;
- 一個自定義按鈕,該按鈕終止已處理的任務并關(guān)閉覆蓋表單。
本示例使您可以更改標簽文本、按鈕字形以及單擊按鈕時執(zhí)行的操作。
實現(xiàn)細節(jié)
您可以按以下方式自定義覆蓋表單圖形:
- 從OverlayWindowPainterBase類繼承它;
- 重寫Draw方法;
- 將創(chuàng)建的對象作為參數(shù)傳遞給SplashScreenManager.ShowOverlayForm方法。
本示例使用OverlayImagePainter和OverlayTextPainter對象(OverlayWindowPainterBase后代),它們實現(xiàn)了圖像和文本的繪制邏輯,并且可以使用它們的屬性來自定義圖像和文本。
OverlayImagePainter對象在覆蓋表單的頂部中心繪制一個自定義圖像,并處理該圖像上的單擊,您可以指定以下屬性:
- Image——正常狀態(tài)下的圖像;
- HoverImage——處于懸停狀態(tài)的圖片;
- ClickAction ——單擊圖像時執(zhí)行的操作。
OverlayTextPainter對象在等待指示器下方繪制一個自定義標簽,您可以指定以下屬性:
- Text ——標簽文本;
- Font ——文本字體(默認使用Tahoma,18);
- Color ——文本顏色(默認為黑色)。
OverlayWindowCompositePainter對象在OverlayImagePainter和OverlayTextPainter對象中組成繪圖邏輯,該復合繪制器作為參數(shù)傳遞給SplashScreenManager.ShowOverlayForm方法。
顯示覆蓋表單時執(zhí)行的操作是使用.Net Framework類庫(任務并行庫(TPL))中可用的可取消任務來實現(xiàn)的。
自定義按鈕和標簽位置
按鈕圖像顯示在覆蓋表單的頂部中央,標簽顯示在等待指示器下方,您可以重寫以下方法來指定其位置:
- OverlayTextPainter.CalcTextBounds —— 返回一個矩形,該矩形指定標簽的位置;
- OverlayImagePainter.CalcImageBounds ———返回一個矩形,它指定按鈕的位置。
每次需要重新繪制疊加表單時(例如,當用戶調(diào)整重疊控件的大小時),都會調(diào)用這些方法,drawArgs方法參數(shù)包含繪制覆蓋圖所需的信息。本示例使用以下屬性:
- 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
下圖說明了生成的布局。