2017-12-08 61 views
1

ボリュームコントロールを作成しようとしています(UIのみ)。バーがpicturebox.Widthの50%を下回る場合を除いて、私はすべてが機能しています。幅(100)は、三角形の色を赤から緑に変えたいと思います。 mouseMoveイベントでコメントされた3行は、私が達成したいことですが、うまくいきません。前もって感謝します。塗りつぶしポリゴンはすでに描画されています

これは私の制御の一例である。これを解決するために

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Windows; 

namespace ControlAudio 
{ 
    public partial class volumen: UserControl 
    { 
     Bitmap im_soundOn = Properties.Resources.sound_on; 
     Bitmap im_soundOff = Properties.Resources.sound_off; 
     int coordenadaX; 
     bool barClicked = false; 
     bool muted = false; 

     public volumen() 
     { 
      InitializeComponent(); 

     } 
     //Dibujar triangulo 
     private void pictureBox1_Paint(object sender, PaintEventArgs e) 
     { 
      var g = e.Graphics; 
      var points = new PointF[] { new PointF(0, 0), new PointF(1, 0), new PointF(0, 1) }; 

      var mx = g.Transform.Clone(); 

      g.TranslateTransform(100f, 100f); 
      g.ScaleTransform(-135f, -70f); 
      g.FillPolygon(Brushes.Olive, points); 
      g.Transform = mx; 
     } 

     private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      barClicked = true; 
     } 

     private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
     { 
      barClicked = false; 
     } 

     private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
     { 
      MouseEventArgs me = (MouseEventArgs)e; 
      Point coordenadasNuevas = me.Location; 
      coordenadaX = coordenadasNuevas.X; 

      if(barClicked && pictureBox1.Width <= 100) 
      { 
       if (coordenadaX > 100) 
       { 
        coordenadaX = 100; 
       } 
       pictureBox1.Width = coordenadaX; 
      } 

      //When it reaches 0 
      if (pictureBox1.Width == 0) { 
       pictureBox1.Width = 0; 
       muted = true; 
       pb_imagen.Image = im_soundOff; 
      } 
      else //When it goes over 0 
      { 
       muted = false; 
       pb_imagen.Image = im_soundOn; 
      } 

      //if(pictureBox1.Width <= 50) g.FillPolygon(Brushes.Olive, points); 
      //if(pictureBox1.Width >50 && pictureBox1.Width <= 90) g.FillPolygon(Brushes.Yellow, points); 
      //if (pictureBox1.Width > 90) g.FillPolygon(Brushes.Red, points); 

     }  
    } 
} 
+3

あなたがペイントイベントのために必要な情報を保存MouseMoveメソッドから 'pictureBox1.Invalidate();'を呼び出すだけです。ペイントイベントでは、保存した情報を使用してポイントや色を決定します。そのポイントや色はペイントのものです。 – LarsTech

+0

申し訳ありませんが、私はC#の新機能です。このポリゴンペイントの仕組みがわからないのですが、私はそれが動作することを知っています。しかし、私はそれがどのように作られたのか分かりません。 Invalidate();を呼び出します。私はそれの残りの部分をどうやって行うのか分からない。 – c4rlos96

+1

ペイントイベントでは、Brushes.Oliveを使用しています。 MouseMoveイベントの情報に基づいて異なる色を使用する場合は、フォームレベルで変数が必要になります。その変数を使用するブラシの色に設定します。 FillPolygonでそのブラシを使用して、必要な色を使用します。 – LarsTech

答えて

1

、および@LarsTechのおかげで、私は次のことをやった:これまで

enter image description here

私のコード:

  1. デフォルトの色:のブラシを作成しました。私はMouseMoveイベントでは、前のg.FillPolygon(Brushes.Olive, points);

    g.FillPolygon(brush, points);から
  2. を変更pictureBox1_Paintイベントで

  3. 、私は次のように追加しました:

    if (pictureBox1.Width <= 50) 
    { 
        brush = Brushes.LightGreen; 
        pictureBox1.Invalidate(); 
    } 
    else if (pictureBox1.Width > 50 && pictureBox1.Width <= 90) 
    { 
        brush = Brushes.Yellow; 
        pictureBox1.Invalidate(); 
    } 
    else 
    { 
        brush = Brushes.Red; 
        pictureBox1.Invalidate(); 
    } 
    
関連する問題