1

マウスをボタンの上に置いたときに、Win7 Calculator のような winform アニメーションを作成するのを手伝ってくれる人がいますか?

これは、マウス入力時に発生します。

private void bgTurnOn_DoWork(object sender, DoWorkEventArgs e)
{
    Label labelSender = (Label)e.Argument;
    int ii = labelSender.ImageIndex;
    for (int i = ii + 4; i <= 11; i++)
    {
        if (labelSender.AllowDrop)
        {
            labelSender.ImageIndex = i;
            Thread.Sleep(40);
        }
    }
}

そしてこれはマウスが離れるとき

private void bgTurnOff_DoWork(object sender, DoWorkEventArgs e)
{
    Label labelSender = (Label)e.Argument;
    int ii = labelSender.ImageIndex;
    for (int i = ii; i >= 0; i--)
    {
        if (!labelSender.AllowDrop)
        {
            labelSender.ImageIndex = i;
            Thread.Sleep(80);
        }
    }
}

注: 私は AllowDrop を使用するだけなので、新しい変数を宣言する必要はありません。42 個のボタンがあるため、より効率的なソリューションが必要だと思います。

4

1 に答える 1

1

グロー効果が必要なようですので、次のアイデアを使用できます。

  • OpacityPictureBox : PictureBox不透明度 (レベル 1 ~ 100 または倍精度 0 ~ 1) をサポートするを作成します。詳細については、これを参照してください。
  • との 2 つの public const int 値をクラスに追加MaxOpacityMinOpacityOpacityPictureBox、外部からの簡単で安全な範囲チェックを行います。値は、不透明度の実装に応じて、0、100、0、1、またはその他の値になります。
  • 1 つのnamedと 1つのnamed 、両方、および 1 つの timer namedAnimatedPictureBox : UserControlを保持するを作成します。であることを確認してください。PictureBoxpbNormalOpacityPictureBoxopbHoverDock = DockStyle.FilltimerpbNormalopbHover
  • 次の 3 つのパブリック プロパティがあります。
    • NormalImageデリゲートするタイプのpbNormal.Image
    • HoverImageデリゲートするタイプのopbHover.Image
    • AnimationIntervalint委任するタイプのtimer.Interval
  • のコンストラクタで、AnimatedPictureBoxを呼び出した後InitializeComponents、 を実行しますopbHover.Opacity = 0;this.Cursor = Cursors.Hand;カーソルを合わせたときにカーソルを手に変えたい場合にも実行できます。
  • -1 または 1 になる_animationDirectiontypeの private members:を持ちます。int
  • 特定の方向にアニメーションを開始するプライベート メソッドを用意します。

コード:

private void Animate(int animationDirection)
{
    this._animationDirection = animationDirection;
    this.timer.Start();
}
  • オーバーライドOnMouseEnterしてOnMouseLeave:

コード:

 protected override void OnMouseEnter(EventArgs e)
 {
     this.Animate(1);
     base.OnMouseEnter(e);
 }

 protected override void OnMouseLeave(EventArgs e)
 {
     this.Animate(-1);
     base.OnMouseEnter(e);
 }
  • timer.Tickイベントを聞いて、これで:

コード:

private void timer_Tick(object sender, EventArgs e)
{
    var hoverOpacity = this.opbHover.Opacity + this._animationDirection;

    if (hoverOpacity < OpacityPictureBox.MinOpacity ||
        hoverOpacity > OpacityPictureBox.MaxOpacity)
    {
        this.timer.Stop();
        return;
    }

    this.opbHover.Opacity = hoverOpacity;
}
于 2012-10-06T09:54:00.260 に答える