Fixed multiple problems with TrackBarEx in designer and runtime.

pull/133/head
dahall 2020-05-28 19:35:39 -06:00
parent e69b8c843c
commit e27d78772d
3 changed files with 73 additions and 23 deletions

View File

@ -51,6 +51,7 @@
this.vistaControlExtender1 = new Vanara.Windows.Forms.VistaControlExtender(this.components);
this.glassExtenderProvider1 = new Vanara.Windows.Forms.GlassExtenderProvider();
this.trackBarEx1 = new Vanara.Windows.Forms.TrackBarEx();
this.logDisplay = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
this.contextMenuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.taskbarButton1)).BeginInit();
@ -78,7 +79,7 @@
this.dlgCombo.Location = new System.Drawing.Point(7, 23);
this.vistaControlExtender1.SetMinVisibleItems(this.dlgCombo, 0);
this.dlgCombo.Name = "dlgCombo";
this.dlgCombo.Size = new System.Drawing.Size(243, 24);
this.dlgCombo.Size = new System.Drawing.Size(243, 23);
this.dlgCombo.TabIndex = 3;
this.dlgCombo.SelectedIndexChanged += new System.EventHandler(this.dlgCombo_SelectedIndexChanged);
//
@ -133,9 +134,8 @@
//
// shellItemChangeWatcher1
//
this.shellItemChangeWatcher1.EnableRaisingEvents = true;
this.shellItemChangeWatcher1.IncludeChildren = true;
this.shellItemChangeWatcher1.Path = "C:\\Temp";
this.shellItemChangeWatcher1.Changed += new System.EventHandler<Vanara.Windows.Shell.ShellItemChangeWatcher.ShellItemChangeEventArgs>(this.ShellItemChangeWatcher1_Changed);
//
// ipAddressBox1
//
@ -198,7 +198,7 @@
this.enumComboBox1.Location = new System.Drawing.Point(12, 302);
this.vistaControlExtender1.SetMinVisibleItems(this.enumComboBox1, 0);
this.enumComboBox1.Name = "enumComboBox1";
this.enumComboBox1.Size = new System.Drawing.Size(332, 24);
this.enumComboBox1.Size = new System.Drawing.Size(332, 23);
this.enumComboBox1.TabIndex = 3;
//
// customButton1
@ -223,17 +223,31 @@
//
// trackBarEx1
//
this.trackBarEx1.Location = new System.Drawing.Point(13, 333);
this.trackBarEx1.Location = new System.Drawing.Point(12, 331);
this.trackBarEx1.Name = "trackBarEx1";
this.trackBarEx1.Size = new System.Drawing.Size(331, 33);
this.trackBarEx1.TabIndex = 10;
this.trackBarEx1.TickPositions = new int[0];
this.trackBarEx1.SelectionEnd = 8;
this.trackBarEx1.SelectionStart = 2;
this.trackBarEx1.ShowSelection = true;
this.trackBarEx1.Size = new System.Drawing.Size(332, 33);
this.trackBarEx1.TabIndex = 12;
//
// logDisplay
//
this.logDisplay.Location = new System.Drawing.Point(160, 178);
this.logDisplay.Multiline = true;
this.logDisplay.Name = "logDisplay";
this.logDisplay.ReadOnly = true;
this.logDisplay.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.logDisplay.Size = new System.Drawing.Size(184, 114);
this.logDisplay.TabIndex = 11;
this.logDisplay.WordWrap = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(695, 519);
this.Controls.Add(this.logDisplay);
this.Controls.Add(this.trackBarEx1);
this.Controls.Add(this.ipAddressBox1);
this.Controls.Add(this.themedImageDraw1);
@ -284,6 +298,7 @@
private Vanara.Windows.Shell.TaskbarButton taskbarButton1;
private Vanara.Windows.Shell.ShellItemChangeWatcher shellItemChangeWatcher1;
private Vanara.Windows.Forms.TrackBarEx trackBarEx1;
private System.Windows.Forms.TextBox logDisplay;
}
}

View File

@ -22,6 +22,20 @@ namespace Windows.Forms.App
{
InitializeComponent();
FillComboWithDialogs(dlgCombo);
shellItemChangeWatcher1.Item = new ShellFolder(Shell32.KNOWNFOLDERID.FOLDERID_RecycleBinFolder);
shellItemChangeWatcher1.EnableRaisingEvents = true;
}
private void ShellItemChangeWatcher1_Changed(object sender, Vanara.Windows.Shell.ShellItemChangeWatcher.ShellItemChangeEventArgs e)
{
foreach (var i in e.ChangedItems)
logDisplay.AppendText($"{e.ChangeType}={i.Name}{Environment.NewLine}");
}
protected override void OnHandleDestroyed(EventArgs e)
{
shellItemChangeWatcher1.EnableRaisingEvents = false;
base.OnHandleDestroyed(e);
}
private static void FillComboWithDialogs(ComboBox cb) => cb.Items.AddRange(Assembly.GetAssembly(typeof(Vanara.Windows.Forms.AccessControlEditorDialog)).GetTypes().Where(t => t.IsPublic && !t.IsNested && typeof(CommonDialog).IsAssignableFrom(t) || typeof(Form).IsAssignableFrom(t)).ToArray());

View File

@ -1,7 +1,11 @@
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Design;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
@ -13,7 +17,9 @@ using static Vanara.PInvoke.User32;
namespace Vanara.Windows.Forms
{
/// <summary>Extends the <see cref="TrackBar"/> class to provide full native-control functionality, including tick marks and value, and custom drawing.</summary>
/// <summary>
/// Extends the <see cref="TrackBar"/> class to provide full native-control functionality, including tick marks and value, and custom drawing.
/// </summary>
/// <seealso cref="System.Windows.Forms.TrackBar"/>
public class TrackBarEx : TrackBar
{
@ -36,16 +42,19 @@ namespace Vanara.Windows.Forms
/// <summary>
/// Occurs when the channel for a <see cref="TrackBarEx"/> needs to be drawn and the <see cref="OwnerDraw"/> property is set to <c>true</c>.
/// </summary>
[Category("Drawing")]
public event PaintEventHandler DrawChannel;
/// <summary>
/// Occurs when the thumb for a <see cref="TrackBarEx"/> needs to be drawn and the <see cref="OwnerDraw"/> property is set to <c>true</c>.
/// </summary>
[Category("Drawing")]
public event PaintEventHandler DrawThumb;
/// <summary>
/// Occurs when the ticks for a <see cref="TrackBarEx"/> need to be drawn and the <see cref="OwnerDraw"/> property is set to <c>true</c>.
/// </summary>
[Category("Drawing")]
public event PaintEventHandler DrawTics;
/// <summary>Gets or sets a value indicating whether to draw ticks based on the <see cref="TrackBar.TickFrequency"/> interval.</summary>
@ -130,8 +139,8 @@ namespace Vanara.Windows.Forms
/// <summary>Gets or sets the upper limit of the selection range this TrackBar is working with.</summary>
/// <value>
/// The logical position at which the selection ends. This value must be less than or equal to the value of the
/// <see cref="TrackBar.Maximum"/> property.
/// The logical position at which the selection ends. This value must be less than or equal to the value of the <see
/// cref="TrackBar.Maximum"/> property.
/// </value>
[DefaultValue(0), Category("Behavior"), Description("The ending logical position of the current selection range in a trackbar.")]
public int SelectionEnd
@ -142,8 +151,8 @@ namespace Vanara.Windows.Forms
/// <summary>Gets or sets the lower limit of the selection range this TrackBar is working with.</summary>
/// <value>
/// The logical position at which the selection starts. This value must be greater than or equal to the value of the
/// <see cref="TrackBar.Minimum"/> property.
/// The logical position at which the selection starts. This value must be greater than or equal to the value of the <see
/// cref="TrackBar.Minimum"/> property.
/// </value>
[DefaultValue(0), Category("Behavior"), Description("The starting logical position of the current selection range in a trackbar.")]
public int SelectionStart
@ -152,7 +161,9 @@ namespace Vanara.Windows.Forms
set => SetSelectionRange(value, selMax);
}
/// <summary>Gets or sets a value indicating whether to show the selection area defined by <see cref="SelectionStart"/> and <see cref="SelectionEnd"/>.</summary>
/// <summary>
/// Gets or sets a value indicating whether to show the selection area defined by <see cref="SelectionStart"/> and <see cref="SelectionEnd"/>.
/// </summary>
/// <value><c>true</c> if showing selection area; otherwise, <c>false</c>.</value>
[DefaultValue(false), Category("Appearance"), Description("Indicates if the TaskBar shows a selection range.")]
public bool ShowSelection
@ -203,26 +214,29 @@ namespace Vanara.Windows.Forms
/// marks created by the trackbar. The logical positions can be any of the integer values in the trackbar's range of minimum to
/// maximum slider positions.
/// </value>
[DefaultValue(null), Category("Appearance"), Description("Indicates the logical values of the trackbar where ticks are drawn.")]
[TypeConverter(typeof(ArrayConverter))]
[Category("Appearance"), Description("Indicates the logical values of the trackbar where ticks are drawn.")]
[Browsable(false)]
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
//[Editor(typeof(ArrayEditor), typeof(UITypeEditor))]
//[TypeConverter(typeof(ArrayConverter))]
public int[] TickPositions
{
get
{
if (!IsHandleCreated || TickStyle == TickStyle.None) return null;
var tickCnt = SendMsg(TrackBarMessage.TBM_GETNUMTICS).ToInt32();
if (tickCnt == 0) return new int[0];
var tickCnt = 0;
if (!IsHandleCreated || TickStyle == TickStyle.None || 0 == (tickCnt = SendMsg(TrackBarMessage.TBM_GETNUMTICS).ToInt32()))
return new int[0];
var ptr = SendMsg(TrackBarMessage.TBM_GETPTICS);
return ptr.ToIEnum<int>(tickCnt - 2).OrderBy(i => i).Distinct().ToArray();
}
set
{
if (value != null)
if (value != null && value.Length > 0)
{
if (TickStyle == TickStyle.None) throw new ArgumentException("Tick positions cannot be set when TickStyle is None.");
if (value.Min() < Minimum || value.Max() > Maximum) throw new ArgumentOutOfRangeException(nameof(TickPositions), "All values must be between Minimum and Maximum range values.");
}
ticks = value;
ticks = value ?? new int[0];
if (IsHandleCreated)
RecreateHandle();
}
@ -350,8 +364,13 @@ namespace Vanara.Windows.Forms
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (showSel) SetSelectionRange(selMin, selMax);
if (thumbLength >= 0) SendMsg(TrackBarMessage.TBM_SETTHUMBLENGTH, thumbLength);
if (showSel)
{
SendMsg(TrackBarMessage.TBM_SETSELEND, 0, selMax);
SendMsg(TrackBarMessage.TBM_SETSELSTART, 1, selMin);
}
if (thumbLength >= 0)
SendMsg(TrackBarMessage.TBM_SETTHUMBLENGTH, thumbLength);
SendMsg(TrackBarMessage.TBM_CLEARTICS);
if (ticks != null && TickStyle != TickStyle.None)
foreach (var t in ticks)
@ -536,6 +555,8 @@ namespace Vanara.Windows.Forms
if (IsHandleCreated) RecreateHandle();
}
private void ResetTickPositions() => TickPositions = new int[0];
private IntPtr SendMsg(TrackBarMessage msg, ref RECT rect) => SendMessage(Handle, (uint)msg, IntPtr.Zero, ref rect);
private bool ShouldSerializeThumbLength() => thumbLength >= 0;