DevExpress WinForms帮助文档:表单控件 - 将图像显示为启动画面并绘制自定义信息

原创|使用教程|编辑:龚雪|2021-03-09 09:45:54.030|阅读 29 次

概述:在本文中,通过SplashScreenManager.ShowImage方法将图像显示为初始屏幕。

# 31款JAVA开发必备控件和工具 # 企业数字化建设合规无风险[专题]

相关链接:

点击获取DevExpress完整版下载

在本文中,通过SplashScreenManager.ShowImage方法将图像显示为初始屏幕。通过自定义类(SplashImagePainter)在该图像上绘制自定义信息,该自定义类的实例作为参数传递给ShowImage方法,SplashImagePainter绘制带有自定义进度信息的文本标签。

注意:完整的示例项目位于https://github.com/DevExpress-Examples/how-to-show-an-image-as-a-splash-screen-and-draw-custom-information-over-this-image-e3719

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraSplashScreen;

namespace SplashScreenManagerSample01 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
LongInitialization();
}
protected void LongInitialization() {
BaseInitialization();
LoadFonts();
LoadTextures();
}

void BaseInitialization() {
// Set progress stage to be displayed by SplashImagePainter
SplashImagePainter.Painter.ViewInfo.Stage = "Initialization";
for(int i = 1; i <= 100; i++) {
System.Threading.Thread.Sleep(20);

// Change progress to be displayed by SplashImagePainter
SplashImagePainter.Painter.ViewInfo.Counter = i;
//Force SplashImagePainter to repaint information
SplashScreenManager.Default.Invalidate();
}
}
void LoadFonts() {
// Set progress stage to be displayed by SplashImagePainter
SplashImagePainter.Painter.ViewInfo.Stage = "Loading Fonts";
for(int i = 1; i <= 100; i++) {
System.Threading.Thread.Sleep(20);

// Change progress to be displayed by SplashImagePainter
SplashImagePainter.Painter.ViewInfo.Counter = i;
//Force SplashImagePainter to repaint information
SplashScreenManager.Default.Invalidate();
}
}
void LoadTextures() {
// Set progress stage to be displayed by SplashImagePainter
SplashImagePainter.Painter.ViewInfo.Stage = "Loading Textures";
for(int i = 1; i <= 100; i++) {
System.Threading.Thread.Sleep(20);

// Change progress to be displayed by SplashImagePainter
SplashImagePainter.Painter.ViewInfo.Counter = i;
//Force SplashImagePainter to repaint information
SplashScreenManager.Default.Invalidate();
}
}
//

protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
// Close splash image
SplashScreenManager.HideImage();
}
}
}

Program.cs

using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using DevExpress.XtraSplashScreen;

namespace SplashScreenManagerSample01 {
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Image image = GetImage();
// Show splash image
SplashScreenManager.ShowImage(image, true, true, SplashImagePainter.Painter);

Application.Run(new Form1());
}
static Image GetImage() {
Assembly asm = typeof(Program).Assembly;
return new Bitmap(asm.GetManifestResourceStream("SplashScreenManagerSample01.Images.DefSplashImage.png"));
}
}
}

DrawImplementer.cs

using System.Drawing;
using DevExpress.Utils.Drawing;
using DevExpress.Utils.Text;
using DevExpress.XtraSplashScreen;

namespace SplashScreenManagerSample01 {
class SplashImagePainter : ICustomImagePainter {
static SplashImagePainter() {
Painter = new SplashImagePainter();
}
protected SplashImagePainter() { }
public static SplashImagePainter Painter { get; private set; }

ViewInfo info = null;
public ViewInfo ViewInfo {
get {
if(this.info == null) this.info = new ViewInfo();
return this.info;
}
}

#region Drawing
public void Draw(GraphicsCache cache, Rectangle bounds) {
PointF pt = ViewInfo.CalcProgressLabelPoint(cache, bounds);
cache.Graphics.DrawString(ViewInfo.Text, ViewInfo.ProgressLabelFont, ViewInfo.Brush, pt);
}
#endregion
}

class ViewInfo {
public ViewInfo() {
Counter = 1;
Stage = string.Empty;
}

public int Counter { get; set; }
public string Stage { get; set; }

public PointF CalcProgressLabelPoint(GraphicsCache cache, Rectangle bounds) {
const int yOffset = 40;
Size size = TextUtils.GetStringSize(cache.Graphics, Text, ProgressLabelFont);
return new Point(bounds.Width / 2 - size.Width / 2, yOffset);
}
Font progressFont = null;
public Font ProgressLabelFont {
get {
if(this.progressFont == null) this.progressFont = new Font("Consolas", 10);
return this.progressFont;
}
}
Brush brush = null;
public Brush Brush {
get {
if(this.brush == null) this.brush = new SolidBrush(Color.Black);
return this.brush;
}
}
public string Text { get { return string.Format("{0} - ({1}%)", Stage, Counter.ToString("D2")); } }
}
}

Program.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Reflection
Imports System.Windows.Forms
Imports DevExpress.XtraSplashScreen

Namespace SplashScreenManagerSample01
Friend NotInheritable Class Program
Private Sub New()
End Sub
<STAThread> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)

Dim image As Image = GetImage()
' Show splash image
SplashScreenManager.ShowImage(image, True, True, SplashImagePainter.Painter)

Application.Run(New Form1())
End Sub
Private Shared Function GetImage() As Image
Dim asm As System.Reflection.Assembly = GetType(Program).Assembly
Return New Bitmap(asm.GetManifestResourceStream("DefSplashImage.png"))
End Function
End Class
End Namespace

Form1.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports DevExpress.XtraSplashScreen

Namespace SplashScreenManagerSample01
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
LongInitialization()
End Sub
Protected Sub LongInitialization()
BaseInitialization()
LoadFonts()
LoadTextures()
End Sub

Private Sub BaseInitialization()
' Set progress stage to be displayed by SplashImagePainter
SplashImagePainter.Painter.ViewInfo.Stage = "Initialization"
For i As Integer = 1 To 100
System.Threading.Thread.Sleep(20)

' Change progress to be displayed by SplashImagePainter
SplashImagePainter.Painter.ViewInfo.Counter = i
'Force SplashImagePainter to repaint information
SplashScreenManager.Default.Invalidate()
Next i
End Sub
Private Sub LoadFonts()
' Set progress stage to be displayed by SplashImagePainter
SplashImagePainter.Painter.ViewInfo.Stage = "Loading Fonts"
For i As Integer = 1 To 100
System.Threading.Thread.Sleep(20)

' Change progress to be displayed by SplashImagePainter
SplashImagePainter.Painter.ViewInfo.Counter = i
'Force SplashImagePainter to repaint information
SplashScreenManager.Default.Invalidate()
Next i
End Sub
Private Sub LoadTextures()
' Set progress stage to be displayed by SplashImagePainter
SplashImagePainter.Painter.ViewInfo.Stage = "Loading Textures"
For i As Integer = 1 To 100
System.Threading.Thread.Sleep(20)

' Change progress to be displayed by SplashImagePainter
SplashImagePainter.Painter.ViewInfo.Counter = i
'Force SplashImagePainter to repaint information
SplashScreenManager.Default.Invalidate()
Next i
End Sub
'

Protected Overrides Sub OnLoad(ByVal e As EventArgs)
MyBase.OnLoad(e)
' Close splash image
SplashScreenManager.HideImage()
End Sub
End Class
End Namespace

DrawImplementer.vb

Imports Microsoft.VisualBasic
Imports System.Drawing
Imports DevExpress.Utils.Drawing
Imports DevExpress.Utils.Text
Imports DevExpress.XtraSplashScreen

Namespace SplashScreenManagerSample01
Friend Class SplashImagePainter
Implements ICustomImagePainter
Shared Sub New()
Painter = New SplashImagePainter()
End Sub
Protected Sub New()
End Sub
Private Shared privatePainter As SplashImagePainter
Public Shared Property Painter() As SplashImagePainter
Get
Return privatePainter
End Get
Private Set(ByVal value As SplashImagePainter)
privatePainter = value
End Set
End Property

Private info As ViewInfo = Nothing
Public ReadOnly Property ViewInfo() As ViewInfo
Get
If Me.info Is Nothing Then
Me.info = New ViewInfo()
End If
Return Me.info
End Get
End Property

#Region "Drawing"
Public Sub Draw(ByVal cache As GraphicsCache, ByVal bounds As Rectangle) Implements ICustomImagePainter.Draw
Dim pt As PointF = ViewInfo.CalcProgressLabelPoint(cache, bounds)
cache.Graphics.DrawString(ViewInfo.Text, ViewInfo.ProgressLabelFont, ViewInfo.Brush, pt)
End Sub
#End Region
End Class

Friend Class ViewInfo
Public Sub New()
Counter = 1
Stage = String.Empty
End Sub

Private privateCounter As Integer
Public Property Counter() As Integer
Get
Return privateCounter
End Get
Set(ByVal value As Integer)
privateCounter = value
End Set
End Property
Private privateStage As String
Public Property Stage() As String
Get
Return privateStage
End Get
Set(ByVal value As String)
privateStage = value
End Set
End Property

Public Function CalcProgressLabelPoint(ByVal cache As GraphicsCache, ByVal bounds As Rectangle) As PointF
Const yOffset As Integer = 40
Dim size As Size = TextUtils.GetStringSize(cache.Graphics, Text, ProgressLabelFont)
Return New Point(bounds.Width / 2 - size.Width / 2, yOffset)
End Function
Private progressFont As Font = Nothing
Public ReadOnly Property ProgressLabelFont() As Font
Get
If Me.progressFont Is Nothing Then
Me.progressFont = New Font("Consolas", 10)
End If
Return Me.progressFont
End Get
End Property
Private brush_Renamed As Brush = Nothing
Public ReadOnly Property Brush() As Brush
Get
If Me.brush_Renamed Is Nothing Then
Me.brush_Renamed = New SolidBrush(Color.Black)
End If
Return Me.brush_Renamed
End Get
End Property
Public ReadOnly Property Text() As String
Get
Return String.Format("{0} - ({1}%)", Stage, Counter.ToString("D2"))
End Get
End Property
End Class
End Namespace

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

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

慧都高端UI界面开发

标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至hey@evget.com

文章转载自:慧都网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
相关产品
DevExpress Universal Subscription

行业领先的界面控件开发包,帮助企业构建卓越应用!

DevExpress WinForms Subscription

为Windows Forms平台创建具有影响力的业务解决方案,高性价比WinForms界面控件套包。

DevExpress DXperience Subscription

高性价比的企业级.NET用户界面套包,助力企业创建卓越应用!

title
在线咨询
联系我们
TOP
在线客服系统
live chat