DevExpress汉化套包火热销售中 - 慧都网

DevExpress控件使用交流,DevExpress中国社区Dev联系电话 联系电话:023-68661681

DevExpress WinForms帮助文档:表单和用户控件 -覆盖表单

来源:   发布时间:2021-04-06   浏览:215次

点击获取DevExpress完整版下载

覆盖表单是执行以下操作的半透明启动屏幕:

  • 覆盖控件或表单
  • 防止用户与重叠控件进行交互
  • 覆盖控件,即使它更改其大小或在屏幕上的位置
  • 在单独的线程中运行,并且不阻塞主线程和操作线程
  • 允许您在重叠控件上显示自定义消息和按钮
DevExpress WinForms帮助文档

注意:运行Overlay Form module in the XtraEditors MainDemo来查看正在使用的表单,单击功能区中的Open Solution获取源代码。

显示覆盖表单

调用ShowOverlayForm(Control) 方法来在控件或表单上显示覆盖表单,该方法返回一个句柄,您可以将其传递给 CloseOverlayForm(IOverlaySplashScreenHandle)方法以关闭表单。

下面的代码显示在应用程序执行长时间运行的操作时如何在当前表单上显示覆盖表单。

C#

using DevExpress.XtraSplashScreen;
//...
IOverlaySplashScreenHandle ShowProgressPanel() {
return SplashScreenManager.ShowOverlayForm(this);
}
void CloseProgressPanel(IOverlaySplashScreenHandle handle) {
if(handle != null)
SplashScreenManager.CloseOverlayForm(handle);
}
//...
IOverlaySplashScreenHandle handle = null;
try {
handle = ShowProgressPanel();
// Launch a long-running operation while
// the Overlay Form overlaps the current form.
}
finally {
CloseProgressPanel(handle);
}

VB.NET

Imports DevExpress.XtraSplashScreen
'...
Private Function ShowProgressPanel() As IOverlaySplashScreenHandle
Dim handle As IOverlaySplashScreenHandle = SplashScreenManager.ShowOverlayForm(Me)
Return handle
End Function

Private Sub CloseProgressPanel(ByVal handle As IOverlaySplashScreenHandle)
If handle IsNot Nothing Then SplashScreenManager.CloseOverlayForm(handle)
End Sub
'...
Dim Handle As IOverlaySplashScreenHandle = Nothing
Try
Handle = ShowProgressPanel()
'Launch a long-running operation while 
'the Overlay Form overlaps the main form.
Finally
CloseProgressPanel(Handle)
End Try

警告:您只能在已初始化(创建其句柄)的控件/表单上显示覆盖表单;否则将抛出InvalidOperationException,请参见 IsHandleCreated

自定义覆盖表单

ShowOverlayForm(Control, OverlayWindowOptions) 方法允许您显示具有以下参数的覆盖表单:

  • StartupDelay — 显示表单之前的延迟。
  • BackColor — 背景颜色。
  • Opacity — 不透明表单。
  • FadeIn, FadeOut — 用于显示和隐藏表单的淡入淡出效果。
  • AnimationType — 动画的类型(等待指示符):
    • Image — 旋转的图像,默认图像取决于皮肤。 使用ImageSize属性指定默认图像的大小,默认图像大小取决于重叠控件的大小,Image属性指定一个自定义图像。
      RotationParameters属性指定旋转周期和单旋转帧数。
    • Line — 使用LineAnimationParameters属性指定点数,点的大小以及点之间的距离。
  • CustomPainter — 一个用于绘制表单的OverlayWindowPainterBase后代,请参阅Custom Painter部分中的示例。
  • SkinName — 应用于表单的skin名称,默认的等待指示器,淡入淡出效果和颜色取决于皮肤,默认外观对应于重叠控件的外观
  • UseDirectX — 指定是否使用DirectX渲染覆盖表单,要将DirectX用于所有兼容的DevExpress控件,请在Project Settings中启用Use DirectX选项,有关更多信息,请参阅以下主题: DirectX Hardware Acceleration

所有这些参数都是可选的。 如果省略参数,则使用默认值。 不带选项的ShowOverlayForm(Control)方法使用静态(在VB中共享)默认选项。

下面的代码显示了如何显示带有自定义参数的覆盖表单。

C#

using DevExpress.XtraSplashScreen;

OverlayWindowOptions options = new OverlayWindowOptions(
startupDelay: 1000,
backColor: Color.Red,
opacity: 0.5,
fadeIn: false,
fadeOut: false,
imageSize: new Size(64, 64)
);
IOverlaySplashScreenHandle handle1 = SplashScreenManager.ShowOverlayForm(gridControl1, options);

IOverlaySplashScreenHandle handle2 = SplashScreenManager.ShowOverlayForm(
owner: gridControl1,
startupDelay: 1000,
backColor: Color.Red,
opacity: 127,
fadeIn: false,
fadeOut: false,
imageSize: new Size(64, 64)
);

VB.NET

Imports DevExpress.XtraSplashScreen

Dim options As New OverlayWindowOptions(
startupDelay:=1000,
backColor:=Color.Red,
opacity:=0.5,
fadeIn:=False,
fadeOut:=False,
imageSize:=New Size(64, 64)
)
Dim formHandle1 As IOverlaySplashScreenHandle = SplashScreenManager.ShowOverlayForm(gridControl1, options)

Dim formHandle2 As IOverlaySplashScreenHandle = SplashScreenManager.ShowOverlayForm(
owner:=gridControl1,
startupDelay:=1000,
backColor:=Color.Red,
opacity:=127,
fadeIn:=False,
fadeOut:=False,
imageSize:=New Size(64, 64)
)
自定义Painter

您可以按以下方式呈现重叠表单:

  • 继承自OverlayWindowPainterBase类
  • 重写Draw方法
  • 将创建的对象作为参数传递给ShowOverlayForm方法

下面的代码段显示了如何显示自定义消息,如下图所示:

DevExpress WinForms帮助文档

C#

using DevExpress.XtraSplashScreen;
using DevExpress.Utils.Drawing;
using System.Drawing;
//...
class CustomOverlayPainter : OverlayWindowPainterBase
{
// Defines the string’s font.
static readonly Font drawFont;
static CustomOverlayPainter() {
drawFont = new Font("Tahoma", 18);
}
protected override void Draw(OverlayWindowCustomDrawContext context)
{
//The Handled event parameter should be set to true. 
//to disable the default drawing algorithm. 
context.Handled = true;
//Provides access to the drawing surface. 
GraphicsCache cache = context.DrawArgs.Cache;
//Adjust the TextRenderingHint option
//to improve the image quality.
cache.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
//Overlapped control bounds. 
Rectangle bounds = context.DrawArgs.Bounds;
//Draws the default background. 
context.DrawBackground();
//Specify the string that will be drawn on the Overlay Form instead of the wait indicator.
String drawString = "Please wait...";
//Get the system's black brush.
Brush drawBrush = Brushes.Black;
//Calculate the size of the message string.
SizeF textSize = cache.CalcTextSize(drawString, drawFont);
//A point that specifies the upper-left corner of the rectangle where the string will be drawn.
PointF drawPoint = new PointF(
bounds.Left + bounds.Width / 2 - textSize.Width / 2,
bounds.Top + bounds.Height / 2 - textSize.Height / 2
);
//Draw the string on the screen.
cache.DrawString(drawString, drawFont, drawBrush, drawPoint);
}
}
//...
IOverlaySplashScreenHandle handle = SplashScreenManager.ShowOverlayForm(this, customPainter: new CustomOverlayPainter());

VB.NET

Imports DevExpress.Utils.Drawing
Imports DevExpress.XtraSplashScreen
Imports System.Drawing
'...
Class CustomOverlayPainter
Inherits OverlayWindowPainterBase
'Defines the string’s font.
Shared ReadOnly drawFont As Font

Shared Sub New()
drawFont = New Font("Tahoma", 18)
End Sub

Protected Overrides Sub Draw(context As OverlayWindowCustomDrawContext)
'The Handled event parameter should be set to true 
'to disable the default drawing algorithm.
context.Handled = True
'Provides access to the drawing surface. 
Dim cache As GraphicsCache = context.DrawArgs.Cache
'Adjust the TextRenderingHint option 
’to improve the image quality. 
cache.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
'Overlapped control bounds.
Dim bounds As Rectangle = context.DrawArgs.Bounds
'Draws the default background. 
context.DrawBackground()
'Create the string to draw. 
Dim drawString As String = "Please wait..."
'Get the system black brush. 
Dim drawBrush As Brush = Brushes.Black
'Calculate the size of the message string. 
Dim textSize As SizeF = cache.CalcTextSize(drawString, drawFont)
'A point that specifies the upper-left corner of the rectangle where the string should be drawn.
Dim drawPoint As PointF = New PointF(bounds.Left + bounds.Width / 2 - textSize.Width / 2, bounds.Top + bounds.Height / 2 - textSize.Height / 2)
'Draw the string on the screen.
cache.DrawString(drawString, drawFont, drawBrush, drawPoint)
End Sub
End Class
'...
Dim handle As IOverlaySplashScreenHandle = SplashScreenManager.ShowOverlayForm(Me, customPainter:=New CustomOverlayPainter())

上DevExpress中文网,获取第一手最新产品资讯!

DevExpress技术交流群3:700924826      欢迎一起进群讨论

慧都高端UI界面开发
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/2339.html

相关产品: DevExpress Universal Subscription,


联系我们
  • 重庆总部 023-68661681 400-700-1020
购买
  • sales@evget.com
合作
  • business@evget.com


在线
客服
在线
QQ
电话
咨询
023-68661681
返回
顶部