Added a progress spinner control that should work on top of WinForms even with its poor transparent control support

git-svn-id: file:///srv/devel/repo-conversion/nuwi@50 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2019-02-13 11:20:18 +00:00
parent 2b908f18eb
commit 7439c31390
5 changed files with 429 additions and 2 deletions

View File

@ -59,6 +59,12 @@
<Compile Include="Source\AutoBinding\IAutoBinder.cs" />
<Compile Include="Source\CommonDialogs\CommonDialogManager.cs" />
<Compile Include="Source\CommonDialogs\ICommonDialogService.cs" />
<Compile Include="Source\Controls\ProgressSpinner.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="Source\Controls\ProgressSpinner.Designer.cs">
<DependentUpon>ProgressSpinner.cs</DependentUpon>
</Compile>
<Compile Include="Source\LateCheckedSynchronizer.cs" />
<Compile Include="Source\Messages\IMessageService.cs" />
<Compile Include="Source\Messages\MessageEventArgs.cs" />
@ -102,6 +108,9 @@
<Compile Include="Source\WindowManager.Test.cs">
<DependentUpon>WindowManager.cs</DependentUpon>
</Compile>
<EmbeddedResource Include="Source\Controls\ProgressSpinner.resx">
<DependentUpon>ProgressSpinner.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Source\ProgressReporter\ProgressReporterForm.resx">
<DependentUpon>ProgressReporterForm.cs</DependentUpon>
<SubType>Designer</SubType>
@ -143,7 +152,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Nuclex.Support.Transactions\Nuclex.Support.Transactions %28net-4.0%29.csproj">
<Project>{2F487C4D-8E06-496F-BCD5-7119B18C78D8}</Project>
<Project>{2f487c4d-8e06-496f-bcd5-7119b18c78d8}</Project>
<Name>Nuclex.Support.Transactions %28net-4.0%29</Name>
</ProjectReference>
<ProjectReference Include="..\Nuclex.Support\Nuclex.Support %28net-4.0%29.csproj">
@ -158,7 +167,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
<Folder Include="Source\Controls\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@ -0,0 +1,49 @@
namespace Nuclex.Windows.Forms.Controls {
partial class ProgressSpinner {
/// <summary> Required designer variable.</summary>
private System.ComponentModel.IContainer components = null;
/// <summary> Clean up any resources being used.</summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if(disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
this.animationUpdateTimer = new System.Windows.Forms.Timer();
this.SuspendLayout();
//
// animationUpdateTimer
//
this.animationUpdateTimer.Tick += new System.EventHandler(this.animationTimerTicked);
//
// ProgressSpinner
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Transparent;
this.DoubleBuffered = true;
this.Name = "ProgressSpinner";
this.ResumeLayout(false);
}
#endregion
/// <summary>Timer used to update the progress animation</summary>
private System.Windows.Forms.Timer animationUpdateTimer;
}
} // namespace Nuclex.Windows.Forms.Controls

View File

@ -0,0 +1,176 @@
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace Nuclex.Windows.Forms.Controls {
/// <summary>Displays a progress spinner to entertain the user while waiting</summary>
public partial class ProgressSpinner : UserControl {
/// <summary>Number of dots the progress spinner will display</summary>
private const int DotCount = 8;
/// <summary>Size of a normal dot (only ever assumed by the trailing dot)</summary>
private const int DotRadius = 4;
/// <summary>
/// The leading dot will be DotCount times this larger than a normal dot
/// </summary>
private const int ScaleFactor = 1;
/// <summary>Initializes a new progress spinner</summary>
public ProgressSpinner() {
SetStyle(
(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw | ControlStyles.UserPaint |
ControlStyles.SupportsTransparentBackColor
),
true
);
InitializeComponent();
Disposed += new EventHandler(OnDisposed);
if(!DesignMode) {
StartSpinner();
}
}
/// <summary>Releases all resources owned by the control when it is destroyed</summary>
/// <param name="sender">Control that is being destroyed</param>
/// <param name="arguments">Not used</param>
private void OnDisposed(object sender, EventArgs arguments) {
if(this.dotOutlinePen != null) {
this.dotOutlinePen.Dispose();
this.dotOutlinePen = null;
}
if(this.dotFillBrush != null) {
this.dotFillBrush.Dispose();
this.dotFillBrush = null;
}
}
/// <summary>Starts the spinner's animation</summary>
public void StartSpinner() {
this.spinnerRunning = true;
this.animationUpdateTimer.Enabled = true;
}
/// <summary>Stops the spinner's animation</summary>
public void StopSpinner() {
this.animationUpdateTimer.Enabled = false;
this.spinnerRunning = false;
}
/// <summary>Color used to fill the dots</summary>
public Color DotFillColor {
get { return this.dotFillColor; }
set {
if(value != this.dotFillColor) {
this.dotFillColor = value;
if(this.dotFillBrush != null) {
this.dotFillBrush.Dispose();
this.dotFillBrush = null;
}
}
}
}
/// <summary>Color used for the dots' outline</summary>
public Color DotOutlineColor {
get { return this.dotOutlineColor; }
set {
if(value != this.dotOutlineColor) {
this.dotOutlineColor = value;
if(this.dotOutlinePen != null) {
this.dotOutlinePen.Dispose();
this.dotOutlinePen = null;
}
}
}
}
/// <summary>Called when the control is hidden or shown</summary>
/// <param name="arguments">Not used</param>
protected override void OnVisibleChanged(EventArgs arguments) {
base.OnVisibleChanged(arguments);
this.animationUpdateTimer.Enabled = this.spinnerRunning && Visible;
}
/// <summary>Called when the control should redraw itself</summary>
/// <param name="arguments">Provides access to the drawing surface and tools</param>
protected override void OnPaint(PaintEventArgs arguments) {
if(Parent != null && this.BackColor == Color.Transparent) {
using(var bmp = new Bitmap(Parent.Width, Parent.Height)) {
Parent.Controls.Cast<Control>()
.Where(c => Parent.Controls.GetChildIndex(c) > Parent.Controls.GetChildIndex(this))
.Where(c => c.Bounds.IntersectsWith(this.Bounds))
.OrderByDescending(c => Parent.Controls.GetChildIndex(c))
.ToList()
.ForEach(c => c.DrawToBitmap(bmp, c.Bounds));
arguments.Graphics.DrawImage(bmp, -Left, -Top);
}
}
if(this.dotOutlinePen == null) {
this.dotOutlinePen = new Pen(this.dotOutlineColor);
}
if(this.dotFillBrush == null) {
this.dotFillBrush = new SolidBrush(this.dotFillColor);
}
//e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
int diameter = Math.Min(Width, Height);
PointF center = new PointF(diameter / 2.0f, diameter / 2.0f);
int bigRadius = diameter / 2 - DotRadius - (DotCount - 1) * ScaleFactor;
float unitAngle = 360 / DotCount;
for(int index = 0; index < DotCount; ++index) {
int dotIndex = (index + leadingDotIndex) % DotCount;
var dotPosition = new PointF(
center.X + (float)(bigRadius * Math.Cos(unitAngle * dotIndex * Math.PI / 180)),
center.Y + (float)(bigRadius * Math.Sin(unitAngle * dotIndex * Math.PI / 180))
);
int currentDotRadius = DotRadius + index * ScaleFactor;
PointF c1 = new PointF(dotPosition.X - currentDotRadius, dotPosition.Y - currentDotRadius);
arguments.Graphics.FillEllipse(this.dotFillBrush, c1.X, c1.Y, 2 * currentDotRadius, 2 * currentDotRadius);
arguments.Graphics.DrawEllipse(this.dotOutlinePen, c1.X, c1.Y, 2 * currentDotRadius, 2 * currentDotRadius);
}
}
/// <summary>Called when the animation timer ticks to update the animation state</summary>
/// <param name="sender">Animation timer that has ticked</param>
/// <param name="arguments">Not used</param>
private void animationTimerTicked(object sender, EventArgs arguments) {
this.leadingDotIndex = (this.leadingDotIndex + 1) % DotCount; // Advance the animation
Invalidate(); // Request a redraw at the earliest opportune time
}
/// <summary>Whether the spinner has been started</summary>
private bool spinnerRunning;
/// <summary>Color in which the dots will be filled</summary>
private Color dotFillColor = Color.RoyalBlue;
/// <summary>Color that will be used for the dots' outline</summary>
private Color dotOutlineColor = Color.White;
/// <summary>Brush used to fill the dots</summary>
private Brush dotFillBrush;
/// <summary>Brush used for the dots' outline</summary>
private Pen dotOutlinePen;
/// <summary>Index of the currently leading dot</summary>
private int leadingDotIndex = 0;
}
} // namespace Nuclex.Windows.Forms.Controls

View File

@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="animationUpdateTimer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -0,0 +1,71 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2019 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using Nuclex.Support;
namespace Nuclex.Windows.Forms.ViewModels {
/// <summary>Base class for view models that have multiple child view models</summary>
/// <typeparam name="TPageEnumeration">Enum type by which pages can be indicated</typeparam>
public abstract class MultiPageViewModel<TPageEnumeration> : Observable
where TPageEnumeration : IEquatable<TPageEnumeration> {
/// <summary>Initializes a new multi-page view model</summary>
/// <param name="windowManager">
/// Window manager the view model uses to create child views
/// </param>
/// <param name="cachePageViewModels">
/// Whether child view models will be kept alive and reused
/// </param>
public MultiPageViewModel(IWindowManager windowManager, bool cachePageViewModels = false) {
this.windowManager = windowManager;
}
/// <summary>Child page that is currently being displayed by the view model</summary>
public TPageEnumeration ActivePage {
get { return this.activePage; }
set {
if(!this.activePage.Equals(value)) {
this.activePage = value;
OnPropertyChanged(nameof(ActivePage));
}
}
}
/// <summary>Windowmanager that can create view models and display other views</summary>
protected IWindowManager WindowManager {
get { return this.windowManager; }
}
/// <summary>Creates a view model for the specified page</summary>
/// <param name="page">Page for which a view model will be created</param>
/// <returns>The view model for the specified page</returns>
protected abstract object createViewModelForPage(TPageEnumeration page);
/// <summary>Page that is currently active in the multi-page view model</summary>
private TPageEnumeration activePage;
/// <summary>Window manager that can be used to display other views</summary>
private IWindowManager windowManager;
}
} // namespace Nuclex.Windows.Forms.ViewModels