2017-01-04 5 views
1

Windows CE用の独自のカスタムプログレスバーコントロールを作成しています.Net 2.0 C#アプリケーション。以下は、不足している細かい唯一のものを動作するコードである私は、右から左に進行状況を表示したいされて誰もが進捗矩形RTLプロパティに上記のコードで右下のレイアウトプロパティ

Code Refering

using System; 
using System.Drawing; 
using System.Windows.Forms; 
class CustomProgressBar:UserControl 
{ 
    int min = 0; // Minimum value for progress range 
    int max = 100; // Maximum value for progress range 
    int val = 0;  // Current progress 
    Color BarColor = Color.Blue;  // Color of progress meter 
    protected override void OnResize(EventArgs e) 
    { 
     // Invalidate the control to get a repaint. 
     this.Invalidate(); 
    } 
    protected override void OnPaint(PaintEventArgs e) 
    { 
     Graphics g = e.Graphics; 
     SolidBrush brush = new SolidBrush(BarColor); 
     float percent = (float)(val - min)/(float)(max - min); 
     Rectangle rect = this.ClientRectangle; 

     // Calculate area for drawing the progress. 
     rect.Width = (int)((float)rect.Width * percent); 

     // Draw the progress meter. 
     g.FillRectangle(brush, rect); 

     // Draw a three-dimensional border around the control. 
     Draw3DBorder(g); 

     // Clean up. 
     brush.Dispose(); 
     g.Dispose(); 
    } 

    public int Minimum 
    { 
     get 
     { 
      return min; 
     } 

     set 
     { 
      // Prevent a negative value. 
      if (value < 0) 
      { 
       min = 0; 
      } 

      // Make sure that the minimum value is never set higher than the maximum value. 
      if (value > max) 
      { 
       min = value; 
       min = value; 
      } 

      // Ensure value is still in range 
      if (val < min) 
      { 
       val = min; 
      } 

      // Invalidate the control to get a repaint. 
      this.Invalidate(); 
     } 
    } 

    public int Maximum 
    { 
     get 
     { 
      return max; 
     } 

     set 
     { 
      // Make sure that the maximum value is never set lower than the minimum value. 
      if (value < min) 
      { 
       min = value; 
      } 

      max = value; 

      // Make sure that value is still in range. 
      if (val > max) 
      { 
       val = max; 
      } 

      // Invalidate the control to get a repaint. 
      this.Invalidate(); 
     } 
    } 

    public int Value 
    { 
     get 
     { 
      return val; 
     } 

     set 
     { 
      int oldValue = val; 

      // Make sure that the value does not stray outside the valid range. 
      if (value < min) 
      { 
       val = min; 
      } 
      else if (value > max) 
      { 
       val = max; 
      } 
      else 
      { 
       val = value; 
      } 

      // Invalidate only the changed area. 
      float percent; 

      Rectangle newValueRect = this.ClientRectangle; 
      Rectangle oldValueRect = this.ClientRectangle; 

      // Use a new value to calculate the rectangle for progress. 
      percent = (float)(val - min)/(float)(max - min); 
      newValueRect.Width = (int)((float)newValueRect.Width * percent); 

      // Use an old value to calculate the rectangle for progress. 
      percent = (float)(oldValue - min)/(float)(max - min); 
      oldValueRect.Width = (int)((float)oldValueRect.Width * percent); 

      Rectangle updateRect = new Rectangle(); 

      // Find only the part of the screen that must be updated. 
      if (newValueRect.Width > oldValueRect.Width) 
      { 
       updateRect.X = oldValueRect.Size.Width; 
       updateRect.Width = newValueRect.Width - oldValueRect.Width; 
      } 
      else 
      { 
       updateRect.X = newValueRect.Size.Width; 
       updateRect.Width = oldValueRect.Width - newValueRect.Width; 
      } 

      updateRect.Height = this.Height; 

      // Invalidate the intersection region only. 
      this.Invalidate(updateRect); 
     } 
    } 

    public Color ProgressBarColor 
    { 
     get 
     { 
      return BarColor; 
     } 

     set 
     { 
      BarColor = value; 

      // Invalidate the control to get a repaint. 
      this.Invalidate(); 
     } 
    } 

    private void Draw3DBorder(Graphics g) 
    { 
     int PenWidth = (int)Pens.White.Width; 

     g.DrawLine(Pens.DarkGray, 
      new Point(this.ClientRectangle.Left, this.ClientRectangle.Top), 
      new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top)); 
     g.DrawLine(Pens.DarkGray, 
      new Point(this.ClientRectangle.Left, this.ClientRectangle.Top), 
      new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth)); 
     g.DrawLine(Pens.White, 
      new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth), 
      new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Height - PenWidth)); 
     g.DrawLine(Pens.White, 
      new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top), 
      new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Height - PenWidth)); 
    } 
} 
+0

MAXから開始してMINに進みます。 – JohnG

+0

私のコードを変更する場所 –

+0

あなたのコードを見たことがないので、 "あなたの"コードをどこで変更するのか分かりません。私は単に左(MIN)から右(MAX)に進むのではなく、右(MAX)から左(MIN)に進むことを意味しました。進行状況が更新されたときにコードコントロールを使用していると仮定すると、コードを調整して現在の値を減らすだけです。投稿したコードは、単にMSのサポートサイトへのリンクです。あなたは人々が助けることができる前に試したことを示す必要があります。あなたは、以下を見てください... [最小限で完全で検証可能な例の作成方法](http://stackoverflow.com/help/mcve) – JohnG

答えて

1

を取得するには、このコードを変更するには私を助けることができますこのようにして作成:

float percent = (float)(val - min)/(float)(max - min); 
Rectangle rect = this.ClientRectangle; 
rect.Width = (int)((float)rect.Width * percent); 

あなたは単にコントロールの右側から進行矩形を埋めるために、コードの下にコードを変更することができます。

float percent = (float)(val - min)/(float)(max - min); 
Rectangle rect = this.ClientRectangle; 
var w = (int)((float)rect.Width * percent); 
rect.X = rect.Width - w; 
rect.Width = w; 
+0

**注1:** LTR座標をRTLに変換する一般的な解決法、あなたは[この投稿](http://stackoverflow.com/a/34509304/3110834)から 'GetRTLCoordinates'を使うことができます。 –

+0

**注2:**ミラー描画のもう一つの一般的な解決策として、このように変換行列を使うことができます。e.Graphics.Transform = new Matrix(-1,0,0,1、Width、0);および描画を実行し、 'e.Graphics.ResetTransform(); 'を使用して変換をリセットします。 –

+0

保護されたオーバーライドvoid OnPaint(PaintEventArgs e) {Graphics g = e.Graphics; SolidBrushブラシ=新しいSolidBrush(BarColor)。 float percent =(float)(val - min)/(float)(max - min); Rectangle rect = this.ClientRectangle; var w =(int)((float)rect.Width * percent); rect.X = rect.Width - w; rect.Width = w; g.FillRectangle(ブラシ、矩形); Draw3DBorder(g); brush.Dispose(); g.Dispose(); }目立つことが起こる何をすべきか? –

関連する問題