Skip to content

Commit 0b01597

Browse files
committed
Added custom progress bar with text (to please Lazar)
1 parent e1ce5b3 commit 0b01597

4 files changed

Lines changed: 91 additions & 18 deletions

File tree

ThemePacker/CustomProgressBar.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows.Forms;
8+
9+
namespace ThemePacker
10+
{
11+
public enum ProgressBarDisplayText
12+
{
13+
Percentage,
14+
CustomText
15+
}
16+
17+
class CustomProgressBar : ProgressBar
18+
{
19+
//Property to set to decide whether to print a % or Text
20+
public ProgressBarDisplayText DisplayStyle { get; set; }
21+
22+
//Property to hold the custom text
23+
public string CustomText { get; set; }
24+
25+
public CustomProgressBar()
26+
{
27+
// Modify the ControlStyles flags
28+
//http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx
29+
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
30+
}
31+
32+
protected override void OnPaint(PaintEventArgs e)
33+
{
34+
Rectangle rect = ClientRectangle;
35+
Graphics g = e.Graphics;
36+
37+
ProgressBarRenderer.DrawHorizontalBar(g, rect);
38+
rect.Inflate(-3, -3);
39+
if (Value > 0)
40+
{
41+
// As we doing this ourselves we need to draw the chunks on the progress bar
42+
Rectangle clip = new Rectangle(rect.X, rect.Y, (int)Math.Round(((float)Value / Maximum) * rect.Width), rect.Height);
43+
ProgressBarRenderer.DrawHorizontalChunks(g, clip);
44+
}
45+
46+
// Set the Display text (Either a % amount or our custom text
47+
string text = DisplayStyle == ProgressBarDisplayText.Percentage ? Value.ToString() + '%' : CustomText;
48+
49+
50+
using (Font f = SystemFonts.DefaultFont)
51+
{
52+
53+
SizeF len = g.MeasureString(text, f);
54+
// Calculate the location of the text (the middle of progress bar)
55+
// Point location = new Point(Convert.ToInt32((rect.Width / 2) - (len.Width / 2)), Convert.ToInt32((rect.Height / 2) - (len.Height / 2)));
56+
Point location = new Point(Convert.ToInt32((Width / 2) - len.Width / 2), Convert.ToInt32((Height / 2) - len.Height / 2));
57+
// The commented-out code will centre the text into the highlighted area only. This will centre the text regardless of the highlighted area.
58+
// Draw the custom text
59+
g.DrawString(text, f, Brushes.Black, location);
60+
}
61+
}
62+
}
63+
}

ThemePacker/MainForm.Designer.cs

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ThemePacker/MainForm.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,41 @@
99
using System.Net;
1010
using System.Net.Http;
1111
using System.Net.Http.Headers;
12+
using System.Runtime.InteropServices;
1213
using System.Threading.Tasks;
1314
using System.Windows.Forms;
1415

1516
namespace ThemePacker
1617
{
1718
public partial class ThemePacker : Form
1819
{
20+
21+
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
22+
static extern uint SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
23+
1924
private List<string> _likeds;
2025
private Dictionary<string, Image> _pictures;
2126
private string _path;
2227
private int _currentPic;
2328
private bool _isFolderBased;
2429

30+
private CustomProgressBar pbProgression;
31+
2532
private EventHandler _btnNextClick;
2633

2734
private ImageList _imageList;
2835

2936
public ThemePacker()
3037
{
3138
InitializeComponent();
39+
pbProgression = new CustomProgressBar(); pbProgression.Location = new Point(69, 496);
40+
pbProgression.MarqueeAnimationSpeed = 0;
41+
pbProgression.Name = "pbProgression";
42+
pbProgression.Size = new Size(400, 28);
43+
pbProgression.Style = ProgressBarStyle.Continuous;
44+
pbProgression.TabIndex = 16;
45+
pbProgression.DisplayStyle = ProgressBarDisplayText.CustomText;
46+
Controls.Add(pbProgression);
3247
}
3348

3449
private void ThemePacker_Load(object sender, EventArgs e)
@@ -152,7 +167,9 @@ private void BtnPrevious_Click(object sender, EventArgs e)
152167
private void UpdatePic()
153168
{
154169
pbWallpaper.Image = _pictures.ElementAt(_currentPic).Value;
155-
pbProgression.Value = (int) Math.Ceiling((_currentPic * 100) / (decimal)(_pictures.Count));
170+
pbProgression.Value = (int)Math.Ceiling(((_currentPic + 1) * 100) / (decimal)(_pictures.Count));
171+
pbProgression.CustomText = $"{_currentPic + 1} / {_pictures.Count}";
172+
pbProgression.Refresh();
156173
}
157174

158175
private void BtnNext_Click(object sender, EventArgs e)
@@ -252,7 +269,7 @@ private void CreateDirectory(string path)
252269

253270
private void PicturesLsv_Click(object sender, EventArgs e)
254271
{
255-
if(picturesLsv.SelectedItems.Count > 0)
272+
if (picturesLsv.SelectedItems.Count > 0)
256273
{
257274
pbWallpaper.Image = _pictures[picturesLsv.SelectedItems[0].ImageKey];
258275
}
@@ -330,17 +347,12 @@ private async Task GetImageFromInspirobot()
330347
private void ThemePacker_FormClosed(object sender, FormClosedEventArgs e)
331348
{
332349
ClearPictures();
333-
if(Directory.Exists("temp"))
350+
if (Directory.Exists("temp"))
334351
{
335352
Directory.Delete("temp", true);
336353
}
337354
}
338355

339-
~ThemePacker()
340-
{
341-
ClearPictures();
342-
}
343-
344356
private void ImportThemepackToolStripMenuItem_Click(object sender, EventArgs e)
345357
{
346358

@@ -350,5 +362,10 @@ private void AsImageFolderToolStripMenuItem_Click(object sender, EventArgs e)
350362
{
351363

352364
}
365+
366+
~ThemePacker()
367+
{
368+
ClearPictures();
369+
}
353370
}
354371
}

ThemePacker/ThemePacker.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
<Reference Include="System.Xml" />
5353
</ItemGroup>
5454
<ItemGroup>
55+
<Compile Include="CustomProgressBar.cs">
56+
<SubType>Component</SubType>
57+
</Compile>
5558
<Compile Include="MainForm.cs">
5659
<SubType>Form</SubType>
5760
</Compile>

0 commit comments

Comments
 (0)