`
huobengle
  • 浏览: 861623 次
文章分类
社区版块
存档分类
最新评论

C#中给进度条加上文字

 
阅读更多
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;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics