问题描述
public enum ProgressBardisplayText
{
Percentage,CustomText
}
public class CustomProgressBar : ProgressBar
{
[DllImportAttribute("uxtheme.dll")]
private static extern int SetwindowTheme(IntPtr hWnd,string appname,string idlist);
protected override void OnHandleCreated(EventArgs e)
{
SetwindowTheme(this.Handle,"","");
base.OnHandleCreated(e);
}
//Property to set to decide whether to print a % or Text
public ProgressBardisplayText displayStyle { get; set; }
//Property to hold the custom text
public String CustomText { get; set; }
public CustomProgressBar()
{
// Modify the ControlStyles flags
//http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint,true);
SetStyle(ControlStyles.OptimizedDoubleBuffer,true);
}
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rect = ClientRectangle;
Graphics g = e.Graphics;
ProgressBarRenderer.DrawHorizontalBar(g,rect);
rect.Inflate(-3,-3);
if (Value > 0)
{
// As we doing this ourselves we need to draw the chunks on the progress bar
Rectangle clip = new Rectangle(rect.X,rect.Y,(int)Math.Round(((float)Value / Maximum) * rect.Width),rect.Height);
ProgressBarRenderer.DrawHorizontalChunks(g,clip);
}
// Set the display text (Either a % amount or our custom text
int percent = (int)(((double)this.Value / (double)this.Maximum) * 100);
string text = displayStyle == ProgressBardisplayText.Percentage ? percent.ToString() + '%' : CustomText;
//string text = displayStyle == ProgressBardisplayText.Percentage ? Value.ToString() + '%' : CustomText;
using (Font f = new Font(FontFamily.GenericSerif,10,FontStyle.Bold))
{
Sizef len = g.MeasureString(text,f);
// Calculate the location of the text (the middle of progress bar)
// Point location = new Point(Convert.ToInt32((rect.Width / 2) - (len.Width / 2)),Convert.ToInt32((rect.Height / 2) - (len.Height / 2)));
Point location = new Point(Convert.ToInt32((Width / 2) - len.Width / 2),Convert.ToInt32((Height / 2) - len.Height / 2));
// The commented-out code will centre the text into the highlighted area only. This will centre the text regardless of the highlighted area.
// Draw the custom text
g.DrawString(text,f,Brushes.Black,location);
}
}
}
现在我想更改进度条的颜色,所以我看到了这个solution
[DllImport("user32.dll",CharSet = CharSet.Auto,SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd,uint Msg,IntPtr w,IntPtr l);
public static void SetState(this ProgressBar pBar,int state)
{
SendMessage(pBar.Handle,1040,(IntPtr)state,IntPtr.Zero);
}
但是如您所见,此代码用于扩展方法,但是我当前的CustomProgressBar
继承了ProgressBar
,并且不允许使用静态类。所以把我扔了:
扩展方法必须在非通用静态类中定义
解决方法
这是一个扩展方法,那些需要进入静态类。如果您只想使其在现有的非静态类中运行,则只需删除第一个参数上的this
:
[DllImport("user32.dll",CharSet = CharSet.Auto,SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd,uint Msg,IntPtr w,IntPtr l);
public static void SetState(ProgressBar pBar,int state)
{
SendMessage(pBar.Handle,1040,(IntPtr)state,IntPtr.Zero);
}
由于ProgressBar
继承自该参数,因此该参数仍可以为CustomProgressBar
类型。