2012-12-12 74 views
2

最近,我需要一個垂直進度條用於我的勝利表單應用程序。派生類如下所示。我還需要在進度條上添加文本。它上面的標籤不適用於transperancy問題。經過一番研究,我找到了一些東西。但問題是,當進度條是垂直的時候,其上的文字看起來是水平的。我也需要垂直。我怎樣才能做到這一點?.NET Winforms垂直進度條文本

謝謝。

public class VProgressBar : ProgressBar 
{ 
    protected override CreateParams CreateParams 
    { 
     get 
     { 
      CreateParams cp = base.CreateParams; 
      cp.Style |= 0x04; 

      if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6) 
      { 
       cp.ExStyle |= 0x02000000; // WS_EX_COMPOSITED 
      } 

      return cp; 
     } 
    } 

    protected override void WndProc(ref Message m) 
    { 
     base.WndProc(ref m); 
     if (m.Msg == 0x000F) 
     { 
      using (Graphics graphics = CreateGraphics()) 
      using (SolidBrush brush = new SolidBrush(ForeColor)) 
      { 
       SizeF textSize = graphics.MeasureString(Text, Font); 
       graphics.DrawString(Text, Font, brush, (Width - textSize.Width)/2, (Height - textSize.Height)/2); 
      } 
     } 
    } 

    [EditorBrowsable(EditorBrowsableState.Always)] 
    [Browsable(true)] 
    public override string Text 
    { 
     get 
     { 
      return base.Text; 
     } 
     set 
     { 
      base.Text = value; 
      Refresh(); 
     } 
    } 

    [EditorBrowsable(EditorBrowsableState.Always)] 
    [Browsable(true)] 
    public override Font Font 
    { 
     get 
     { 
      return base.Font; 
     } 
     set 
     { 
      base.Font = value; 
      Refresh(); 
     } 
    } 
} 
+0

查看這個問題上了年紀的答案 - http://stackoverflow.com/questions/1371943/c-sharp-vertical-label-in-a-winform – ChrisF

回答

0

您需要使用StringFormat來使用標記來垂直繪製字符串。嘗試在WndProc方法如下:

protected override void WndProc(ref Message m) 
    { 
     base.WndProc(ref m); 
     if (m.Msg == 0x000F) 
     { 
      using (Graphics graphics = CreateGraphics()) 
      using (SolidBrush brush = new SolidBrush(ForeColor)) 
      { 
       StringFormat format = new StringFormat(
         StringFormatFlags.DirectionVertical); 

       SizeF textSize = graphics.MeasureString(Text, Font); 
       graphics.DrawString(
        Text, Font, brush, 
        (Width/2 - textSize.Height/2), 
        (Height/2 - textSize.Width/2), 
        format); 
      } 
     } 
    } 
+0

感謝丹尼爾, 有效。我還需要使用graphics.RotateTransform(180)從下到上顯示文本。 –