龙空技术网

应用界面表单美化 - 将图像显示为启动画面并绘制自定义信息

慧都科技 40

前言:

今天咱们对“vbnetpublicshared”大致比较珍视,朋友们都需要了解一些“vbnetpublicshared”的相关文章。那么小编同时在网摘上网罗了一些关于“vbnetpublicshared””的相关内容,希望大家能喜欢,看官们一起来学习一下吧!

点击获取工具:

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

注意:完整的示例项目位于 。

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 SplashImagePainterSplashImagePainter.Painter.ViewInfo.Stage = "Initialization";for(int i = 1; i <= 100; i++) {System.Threading.Thread.Sleep(20);// Change progress to be displayed by SplashImagePainterSplashImagePainter.Painter.ViewInfo.Counter = i;//Force SplashImagePainter to repaint informationSplashScreenManager.Default.Invalidate();}}void LoadFonts() {// Set progress stage to be displayed by SplashImagePainterSplashImagePainter.Painter.ViewInfo.Stage = "Loading Fonts";for(int i = 1; i <= 100; i++) {System.Threading.Thread.Sleep(20);// Change progress to be displayed by SplashImagePainterSplashImagePainter.Painter.ViewInfo.Counter = i;//Force SplashImagePainter to repaint informationSplashScreenManager.Default.Invalidate();}}void LoadTextures() {// Set progress stage to be displayed by SplashImagePainterSplashImagePainter.Painter.ViewInfo.Stage = "Loading Textures";for(int i = 1; i <= 100; i++) {System.Threading.Thread.Sleep(20);// Change progress to be displayed by SplashImagePainterSplashImagePainter.Painter.ViewInfo.Counter = i;//Force SplashImagePainter to repaint informationSplashScreenManager.Default.Invalidate();}}//protected override void OnLoad(EventArgs e) {base.OnLoad(e);// Close splash imageSplashScreenManager.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 imageSplashScreenManager.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 Drawingpublic 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.VisualBasicImports SystemImports System.DrawingImports System.ReflectionImports System.Windows.FormsImports DevExpress.XtraSplashScreenNamespace SplashScreenManagerSample01Friend NotInheritable Class ProgramPrivate Sub New()End Sub<STAThread> _Shared Sub Main()Application.EnableVisualStyles()Application.SetCompatibleTextRenderingDefault(False)Dim image As Image = GetImage()' Show splash imageSplashScreenManager.ShowImage(image, True, True, SplashImagePainter.Painter)Application.Run(New Form1())End SubPrivate Shared Function GetImage() As ImageDim asm As System.Reflection.Assembly = GetType(Program).AssemblyReturn New Bitmap(asm.GetManifestResourceStream("DefSplashImage.png"))End FunctionEnd ClassEnd Namespace

Form1.vb

Imports Microsoft.VisualBasicImports SystemImports System.Collections.GenericImports System.ComponentModelImports System.DataImports System.DrawingImports System.LinqImports System.TextImports System.Windows.FormsImports DevExpress.XtraSplashScreenNamespace SplashScreenManagerSample01Partial Public Class Form1Inherits FormPublic Sub New()InitializeComponent()LongInitialization()End SubProtected Sub LongInitialization()BaseInitialization()LoadFonts()LoadTextures()End SubPrivate Sub BaseInitialization()' Set progress stage to be displayed by SplashImagePainterSplashImagePainter.Painter.ViewInfo.Stage = "Initialization"For i As Integer = 1 To 100System.Threading.Thread.Sleep(20)' Change progress to be displayed by SplashImagePainterSplashImagePainter.Painter.ViewInfo.Counter = i'Force SplashImagePainter to repaint informationSplashScreenManager.Default.Invalidate()Next iEnd SubPrivate Sub LoadFonts()' Set progress stage to be displayed by SplashImagePainterSplashImagePainter.Painter.ViewInfo.Stage = "Loading Fonts"For i As Integer = 1 To 100System.Threading.Thread.Sleep(20)' Change progress to be displayed by SplashImagePainterSplashImagePainter.Painter.ViewInfo.Counter = i'Force SplashImagePainter to repaint informationSplashScreenManager.Default.Invalidate()Next iEnd SubPrivate Sub LoadTextures()' Set progress stage to be displayed by SplashImagePainterSplashImagePainter.Painter.ViewInfo.Stage = "Loading Textures"For i As Integer = 1 To 100System.Threading.Thread.Sleep(20)' Change progress to be displayed by SplashImagePainterSplashImagePainter.Painter.ViewInfo.Counter = i'Force SplashImagePainter to repaint informationSplashScreenManager.Default.Invalidate()Next iEnd Sub'Protected Overrides Sub OnLoad(ByVal e As EventArgs)MyBase.OnLoad(e)' Close splash imageSplashScreenManager.HideImage()End SubEnd ClassEnd NamespaceDrawImplementer.vbImports Microsoft.VisualBasicImports System.DrawingImports DevExpress.Utils.DrawingImports DevExpress.Utils.TextImports DevExpress.XtraSplashScreenNamespace SplashScreenManagerSample01Friend Class SplashImagePainterImplements ICustomImagePainterShared Sub New()Painter = New SplashImagePainter()End SubProtected Sub New()End SubPrivate Shared privatePainter As SplashImagePainterPublic Shared Property Painter() As SplashImagePainterGetReturn privatePainterEnd GetPrivate Set(ByVal value As SplashImagePainter)privatePainter = valueEnd SetEnd PropertyPrivate info As ViewInfo = NothingPublic ReadOnly Property ViewInfo() As ViewInfoGetIf Me.info Is Nothing ThenMe.info = New ViewInfo()End IfReturn Me.infoEnd GetEnd Property#Region "Drawing"Public Sub Draw(ByVal cache As GraphicsCache, ByVal bounds As Rectangle) Implements ICustomImagePainter.DrawDim pt As PointF = ViewInfo.CalcProgressLabelPoint(cache, bounds)cache.Graphics.DrawString(ViewInfo.Text, ViewInfo.ProgressLabelFont, ViewInfo.Brush, pt)End Sub#End RegionEnd ClassFriend Class ViewInfoPublic Sub New()Counter = 1Stage = String.EmptyEnd SubPrivate privateCounter As IntegerPublic Property Counter() As IntegerGetReturn privateCounterEnd GetSet(ByVal value As Integer)privateCounter = valueEnd SetEnd PropertyPrivate privateStage As StringPublic Property Stage() As StringGetReturn privateStageEnd GetSet(ByVal value As String)privateStage = valueEnd SetEnd PropertyPublic Function CalcProgressLabelPoint(ByVal cache As GraphicsCache, ByVal bounds As Rectangle) As PointFConst yOffset As Integer = 40Dim size As Size = TextUtils.GetStringSize(cache.Graphics, Text, ProgressLabelFont)Return New Point(bounds.Width / 2 - size.Width / 2, yOffset)End FunctionPrivate progressFont As Font = NothingPublic ReadOnly Property ProgressLabelFont() As FontGetIf Me.progressFont Is Nothing ThenMe.progressFont = New Font("Consolas", 10)End IfReturn Me.progressFontEnd GetEnd PropertyPrivate brush_Renamed As Brush = NothingPublic ReadOnly Property Brush() As BrushGetIf Me.brush_Renamed Is Nothing ThenMe.brush_Renamed = New SolidBrush(Color.Black)End IfReturn Me.brush_RenamedEnd GetEnd PropertyPublic ReadOnly Property Text() As StringGetReturn String.Format("{0} - ({1}%)", Stage, Counter.ToString("D2"))End GetEnd PropertyEnd ClassEnd Namespace

标签: #vbnetpublicshared