應用界面開發小技巧 - 如何在覆蓋表單上顯示自定義按鈕

點擊獲取工具>>git

注意:GitHub上提供了完整的示例項目,網址爲:https://github.com/DevExpress-Examples/how-to-display-a-custom-button-on-an-overlay-formgithub

概述

Overlay Form是一個半透明的啓動屏幕,他在後臺線程中運行並覆蓋控件或表單來防止對其進行訪問,Overlay Form僅包含一個等待指示器。express

DevExpress WinForms幫助文檔

本示例演示如何顯示:async

  • 等待指示器下方的百分比標籤;
  • 一個自定義按鈕,該按鈕終止已處理的任務並關閉Overlay Form。

DevExpress WinForms幫助文檔

本示例使您能夠更改標籤文本、按鈕字形以及單擊按鈕時執行的操做。ide

實現細節

您能夠按如下方式自定義Overlay Form圖形:工具

  • 從OverlayWindowPainterBase類繼承它;
  • 重寫Draw方法;
  • 將建立的對象做爲參數傳遞給SplashScreenManager.ShowOverlayForm方法。

本示例使用OverlayImagePainter和OverlayTextPainter對象(OverlayWindowPainterBase後代),它們實現了圖像和文本的繪製邏輯,而且您能夠使用它們的屬性來自定義圖像和文本。佈局

OverlayImagePainter對象在Overlay Form的頂部中心繪製一個自定義圖像,並處理該圖像上的單擊。 您能夠指定如下屬性:字體

  • Image - 正常狀態下的圖像;
  • HoverImage - 處於懸停狀態的圖片;
  • ClickAction - 單擊圖像時執行的操做。

OverlayTextPainter對象在等待指示器下方繪製一個自定義標籤,您能夠指定如下屬性:this

  • Text - 標籤文本;
  • Font - 文本字體(默認使用Tahoma,18);
  • Color - 文本顏色(默認爲黑色)。

OverlayWindowCompositePainter對象在OverlayImagePainter和OverlayTextPainter對象中組成繪圖邏輯,該複合繪製器做爲參數傳遞給SplashScreenManager.ShowOverlayForm方法。spa

顯示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
`
下圖說明了生成的佈局。

DevExpress WinForms幫助文檔

相關文章
相關標籤/搜索