代码如下,添加了重绘类以后,无论是在设计器中还是在窗口构造函数中都改变ProgressBar的属性,都不起任何作用,怎么解决?using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using System.Drawing; //for Color,Font
using System.Runtime.InteropServices; //for DllImport
using System.Windows.Forms; //for NativeWindow/// <summary>
/// 用来重绘ProgressBar外观的类,主要是在其上添加文字
/// </summary>public class SubWindow : NativeWindow
{
[DllImport("user32.dll")]
extern static IntPtr GetWindowDC(IntPtr hWnd); [DllImport("user32.dll")]
extern static int ReleaseDC(IntPtr hWnd , IntPtr hDC); private Control control;
private string text = "null";
private Color foreColor = SystemColors.ControlText;
private Font font = SystemFonts.MenuFont; public Font Font
{
set
{
font = value;
if (control != null)
control.Invalidate();
}
} public Color ForeColor
{
set
{
foreColor = value;
if (control != null)
control.Invalidate();
}
} public string Text
{
set
{
text = value;
if (control != null)
control.Invalidate();
}
} public Control Control
{
set
{
control = value;
if (control != null)
{
AssignHandle(control.Handle);
control.Invalidate();
}
}
} protected override void WndProc(ref Message m)
{
const int WM_PAINT = 0x000F;
base.WndProc(ref m);
if (control == null) return;
switch (m.Msg)
{
case WM_PAINT:
IntPtr vDC = GetWindowDC(m.HWnd);
Graphics vGraphics = Graphics.FromHdc(vDC);
StringFormat vStringFormat = new StringFormat();
vStringFormat.Alignment = StringAlignment.Center;
vStringFormat.LineAlignment = StringAlignment.Center;
vGraphics.DrawString(text , font , new SolidBrush(foreColor) , new Rectangle(0 , 0 , control.Width , control.Height) , vStringFormat);
ReleaseDC(m.HWnd , vDC);
break;
}
}
}
//包含ProgressBar的窗口构造函数
public Form_ProgressBar(string notice)
{
InitializeComponent(); //这里的设置不起作用了
progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.ForeColor = Color.Lime; //set some properties
this.ShowInTaskbar = false; //Redraw ProgressBar
vSubWindow.Font = this.Font;
vSubWindow.Text = notice;
vSubWindow.ForeColor = Color.Black;
vSubWindow.Control = this.progressBar1;
}