WinForms应用程序中的控制WPF进度栏

问题描述 投票:-1回答:1

我有一个用vb .net构建的WinForms应用程序,我向其中添加了WPF用户控件。 WPF用户控件仅包含一个进度条。我可以将WPF控件从toolbax拖动到WinForms vb .net中的Main Form中。但是我不知道如何让我的WinForms应用程序动态设置WPF进度栏的值。无论如何,我可以通过WinForms应用程序动态设置WPF控件进度栏的值吗?

注意:我使用WPF进度条而不使用WinForms进度条的原因是为了使我可以在WinForms应用程序中使用蓝色的进度条。

wpf vb.net winforms wpf-controls interop
1个回答
0
投票

由于它可能不像在纸上看起来那么简单,所以这里有一个自定义的ProgressBar,它可以设置条形的颜色,还可以更改其样式。

ProgressBarStyle

属性设置当前样式:BarStyle.Standard样式使用ProgressBarRenderer.DrawHorizontalBar()方法绘制背景,而BarStyle.Flat选择Parent.BackColor作为背景色,并使用固定的白色边框绘制ProgressBar边界,并使用SystemColors.Highlight作为条形颜色。当然,可以更改它以支持任何其他颜色和标准样式。 StringFormat类用于使ProgressBar的文本居中(仅Standard样式。也可以轻松更改),将[Horizo​​ntal]和[Vertical]对齐都设置为StringAlignment.Center。文本颜色会自动更改以适应ProgressBar颜色的亮度(这只是一个简单的调整,HSL值本身不能始终确定哪种文本颜色更适合背景颜色)。

一些魔术值

只是用于使绘图矩形适应由ProgressBarRenderer]设计的形状的偏移量

这是它的工作方式:

ProgressBar Custom

VB.Net版本的自定义控件:

Imports System.ComponentModel Imports System.Drawing.Drawing2D Imports System.Drawing Imports System.Windows.Forms <DesignerCategory("Code")> Public Class ProgressBarCustomColor Inherits ProgressBar Public Sub New() SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.OptimizedDoubleBuffer, True) End Sub Public Enum BarStyle Standard Flat End Enum Public Property ProgressBarStyle As BarStyle Protected Overrides Sub OnPaintBackground(e As PaintEventArgs) MyBase.OnPaintBackground(e) Dim rect = Me.ClientRectangle If ProgressBarStyle = BarStyle.Standard Then ProgressBarRenderer.DrawHorizontalBar(e.Graphics, rect) Dim baseColor = Color.FromArgb(240, Me.BackColor) Dim highColor = Color.FromArgb(160, baseColor) Using baseBrush = New SolidBrush(baseColor), highBrush = New SolidBrush(highColor) e.Graphics.FillRectangle(highBrush, 1, 2, rect.Width, 9) e.Graphics.FillRectangle(baseBrush, 1, 7, rect.Width, rect.Height - 1) End Using Else Using pen = New Pen(Color.White, 2), baseBrush = New SolidBrush(Me.Parent.BackColor) e.Graphics.FillRectangle(baseBrush, 0, 0, rect.Width - 1, rect.Height - 1) e.Graphics.DrawRectangle(pen, 1, 1, Me.ClientSize.Width - 2, Me.ClientSize.Height - 2) End Using End If End Sub Protected Overrides Sub OnPaint(e As PaintEventArgs) Dim rect = New RectangleF(PointF.Empty, Me.ClientSize) rect.Width = CType(rect.Width * (CType(Me.Value, Single) / Me.Maximum), Integer) rect.Size = New SizeF(rect.Width - 2, rect.Height) Dim hsl As Single = Me.ForeColor.GetBrightness() If ProgressBarStyle = BarStyle.Standard Then DrawStandardBar(e.Graphics, rect, hsl) End If If ProgressBarStyle = BarStyle.Flat Then DrawFlatBar(e.Graphics, rect, hsl) End If End Sub Private Sub DrawStandardBar(g As Graphics, rect As RectangleF, hsl As Single) g.SmoothingMode = SmoothingMode.AntiAlias g.CompositingQuality = CompositingQuality.HighQuality Dim baseColor = Color.FromArgb(240, Me.ForeColor) Dim highColor = Color.FromArgb(160, baseColor) Using baseBrush = New SolidBrush(baseColor), highBrush = New SolidBrush(highColor), sf = New StringFormat(StringFormatFlags.MeasureTrailingSpaces) sf.LineAlignment = StringAlignment.Center sf.Alignment = StringAlignment.Center g.FillRectangle(highBrush, 1, 2, rect.Width, 9) g.FillRectangle(baseBrush, 1, 7, rect.Width, rect.Height - 1) g.DrawString($"{Me.Value} %", Me.Parent.Font, If(hsl > 0.49F, Brushes.Black, Brushes.White), Me.ClientRectangle, sf) End Using End Sub Private Sub DrawFlatBar(g As Graphics, rect As RectangleF, hsl As Single) Using baseBrush = New SolidBrush(SystemColors.Highlight) g.FillRectangle(baseBrush, 2, 2, rect.Width - 2, rect.Height - 4) End Using End Sub End Class

C#版本:

using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; [DesignerCategory("Code")] public class ProgressBarCustomColor : ProgressBar { public ProgressBarCustomColor() { this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true); } public enum BarStyle { Standard, Flat } public BarStyle ProgressBarStyle { get; set; } protected override void OnPaintBackground(PaintEventArgs e) { base.OnPaintBackground(e); var rect = this.ClientRectangle; if (ProgressBarStyle == BarStyle.Standard) { ProgressBarRenderer.DrawHorizontalBar(e.Graphics, rect); Color baseColor = Color.FromArgb(240, this.BackColor); Color highColor = Color.FromArgb(160, baseColor); using (var baseBrush = new SolidBrush(baseColor)) using (var highBrush = new SolidBrush(highColor)) { e.Graphics.FillRectangle(highBrush, 1, 2, rect.Width, 9); e.Graphics.FillRectangle(baseBrush, 1, 7, rect.Width, rect.Height - 1); } } else { using (var pen = new Pen(Color.White, 2)) using (var baseBrush = new SolidBrush(this.Parent.BackColor)) { e.Graphics.FillRectangle(baseBrush, 0, 0, rect.Width - 1, rect.Height - 1); e.Graphics.DrawRectangle(pen, 1, 1, this.ClientSize.Width - 2, this.ClientSize.Height - 2); } } } protected override void OnPaint(PaintEventArgs e) { var rect = new RectangleF(PointF.Empty, this.ClientSize); rect.Width = (int)(rect.Width * ((float)this.Value / this.Maximum)); rect.Size = new SizeF(rect.Width - 2, rect.Height); float hsl = this.ForeColor.GetBrightness(); if (ProgressBarStyle == BarStyle.Standard) DrawStandardBar(e.Graphics, rect, hsl); if (ProgressBarStyle == BarStyle.Flat) DrawFlatBar(e.Graphics, rect, hsl); } private void DrawStandardBar(Graphics g, RectangleF rect, float hsl) { g.SmoothingMode = SmoothingMode.AntiAlias; g.CompositingQuality = CompositingQuality.HighQuality; Color baseColor = Color.FromArgb(240, this.ForeColor); Color highColor = Color.FromArgb(160, baseColor); using (var baseBrush = new SolidBrush(baseColor)) using (var highBrush = new SolidBrush(highColor)) using (var sf = new StringFormat(StringFormatFlags.MeasureTrailingSpaces)) { sf.LineAlignment = StringAlignment.Center; sf.Alignment = StringAlignment.Center; g.FillRectangle(highBrush, 1, 2, rect.Width, 9); g.FillRectangle(baseBrush, 1, 7, rect.Width, rect.Height - 1); g.DrawString(this.Value.ToString() + " %", this.Parent.Font, hsl > .49f ? Brushes.Black : Brushes.White, this.ClientRectangle, sf); } } private void DrawFlatBar(Graphics g, RectangleF rect, float hsl) { using (var baseBrush = new SolidBrush(SystemColors.Highlight)) { g.FillRectangle(baseBrush, 2, 2, rect.Width - 2, rect.Height - 4); } } }
© www.soinside.com 2019 - 2024. All rights reserved.