欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-01 21:52:22
...

点击获取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      欢迎一起进群讨论