C\#음악 재생 기 진도 바 사용자 정의

5184 단어 C#진도 표
어떤 때 는 우리 가 하 는 프로그램 에 진도 가 필요 하지만 vs 가 제공 하 는 컨트롤 은 우리 가 원 하 는 것 이 아 닙 니 다.효과 그림 먼저 보기:

진도 바 반 짝 임 애니메이션,물론 배경 은 Transparent 로 설정 할 수 있 습 니 다.
진행 라인 을 손 으로 그 리 려 고 했 는데 컨트롤 이 실 행 될 때 반 짝 거 려 서 패 널 컨트롤 을 직접 사 용 했 습 니 다.
원본 코드:

[DefaultEvent("ProgressClick")]
  [ToolboxBitmap(typeof(TrackBar))]
  public partial class ProcessBar : UserControl
  {
    public ProcessBar()
    {
      //InitializeComponent();
      //this.SetStyle(ControlStyles.UserPaint, true);
      //this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
      //this.SetStyle(ControlStyles.DoubleBuffer, true);
    }

    private int locationX=0;
    [Description("   X   ")]
    public int LocationX
    {
      get { return locationX; }
    }
  
    private int current = 0;
    [Description("    ")]
    public int Current
    {
      get { return current; }
      set
      {
        if (value > 232 || value < 0)
          return;
        current = value;
        panelCurrent.Size = new Size(value, 1);
        picture.Location = new Point(value - 4, -3);
        Invalidate();
      }
    }

    private bool isPlay = false;
    [Description("    ")]
    public bool IsPlay
    {
      get { return isPlay; }
      set { isPlay = value; tmrCurrent.Enabled = isPlay; Invalidate(); }
    }

    public delegate void MouseHandle(object sender,EventArgs e);
    [Description("    ")]
    public event MouseHandle BarMouseDown;

    int picturetype = 0;
    private void tmrCurrent_Tick(object sender, EventArgs e)
    {
      if (picturetype == 0)
      { picture.Image = Properties.Resources.play_slider_thumb; picturetype = 1; }
      else
      { picture.Image = Properties.Resources.play_slider_thumb_animate; picturetype = 0; }
      GraphicsPath g = subGraphicsPath(picture.Image);
      if (g == null) return;
      picture.Region = new Region(g);
    }

    private unsafe static GraphicsPath subGraphicsPath(Image img)
    {
      if (img == null) return null;
      //   GraphicsPath,               
      GraphicsPath g = new GraphicsPath(FillMode.Alternate);
      Bitmap bitmap = new Bitmap(img);
      int width = bitmap.Width;
      int height = bitmap.Height;
      BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
      byte* p = (byte*)bmData.Scan0;
      int offset = bmData.Stride - width * 3;
      int p0, p1, p2;     //      0,0       
      p0 = p[0];
      p1 = p[1];
      p2 = p[2];

      int start = -1;
      //     ( Y col )  
      for (int Y = 0; Y < height; Y++)
      {
        //     ( X row )  
        for (int X = 0; X < width; X++)
        {
          if (start == -1 && (p[0] != p0 || p[1] != p1 || p[2] != p2))   //                    
          {
            start = X;              //      
          }
          else if (start > -1 && (p[0] == p0 && p[1] == p1 && p[2] == p2))   //                 
          {
            g.AddRectangle(new Rectangle(start, Y, X - start, 1));  //         
            start = -1;
          }
          if (X == width - 1 && start > -1)    //                     
          {
            g.AddRectangle(new Rectangle(start, Y, X - start + 1, 1));   //         
            start = -1;
          }
          p += 3;                  //        
        }
        p += offset;
      } bitmap.UnlockBits(bmData);
      bitmap.Dispose();
      //                 
      return g;
    }

    private void panelTotal_MouseDown(object sender, MouseEventArgs e)
    {
      Current = e.Location.X;
      locationX = e.Location.X;
      if (BarMouseDown != null)
      {
        BarMouseDown.Invoke(sender, e);
      }
    }

    private void panelCurrent_MouseDown(object sender, MouseEventArgs e)
    {
      Current = e.Location.X;
      locationX = e.Location.X;
      if (BarMouseDown != null)
      {
        BarMouseDown.Invoke(sender, e);
      }
    }
  }


사용 한 소재:

직접 오른쪽 단 추 를 누 르 면 그림 으로 저 장 됩 니 다.검은색 배경 을 사용 하 는 이 유 는 그림 이 흰색 이기 때문에 보이 지 않 기 때 문 입 니 다.더 말 할 필요 가 없습니다.
알림:unsafe 키 워드 를 사 용 했 습 니 다.항목 의 속성 을 설정 해 야 합 니 다.안전 하지 않 은 코드 를 실행 할 수 있 습 니 다.설정 되 지 않 은 학생 들 은 프로그램 이 틀 렸 다 고 생각 하지 마 십시오.

좋은 웹페이지 즐겨찾기