C#中给进度条加上文字

发布时间:2009年06月04日      浏览次数:402 次
using System.Runtime.InteropServices;

[DllImport("user32.dll")]
static extern IntPtr GetWindowDC(IntPtr hWnd);

[DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

public class SubWindow : NativeWindow
{
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;
}
}
}

private void Form1_Load(object sender, EventArgs e)
{
SubWindow vSubWindow = new SubWindow();
vSubWindow.Font = Font;
vSubWindow.Text = "Zswang 路过";
vSubWindow.ForeColor = Color.Blue;
vSubWindow.Control = progressBar1;
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!