C# 将静态方法添加到当前自定义类
我有自定义进度条(是带有文本的进度条) 自定义条码:C# 将静态方法添加到当前自定义类,c#,winforms,custom-controls,C#,Winforms,Custom Controls,我有自定义进度条(是带有文本的进度条) 自定义条码: public enum ProgressBarDisplayText { Percentage, CustomText } public class CustomProgressBar : ProgressBar { [DllImportAttribute("uxtheme.dll")] private static ex
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);
}
}
}
现在我想改变进度条的颜色,所以我看到了这个
所以我尝试添加到我的代码中
[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
继承自该参数。扩展方法必须在static
类中定义。通常我只是创建一个公共静态类扩展{}
并将它们添加到那里。如果要将它们添加到当前类中,可以从第一个参数中删除this
,然后传入控件的实例。我复制粘贴了你的代码,但我不明白为什么?你说摆脱这个是什么意思,我看到你的代码和我的一样,有什么区别?你的有这个ProgressBar pBar,我的只有ProgressBar pBar。