LoginSignup
1
0

More than 5 years have passed since last update.

VBAでボタン(Shape)を凹ませる

Last updated at Posted at 2017-09-17

押されたときに凹むアニメーションをするボタン

はじめに

昨今のフラットUIの流れからは外れますが立体的なボタンが分かりやすいのも事実です。
かといって、Excel 2010のグレーの標準ボタンは古い感じがします。
そこで図形の3D効果を使用して押されたときに凹ませるアニメーションを考えました。

VBAでボタン(Shape)を凹ますアニメーション

ボタンアニメーションマクロ

  • button_down : ボタンを押したときのアニメーション
    引数: ボタン図形(Shape)オブジェクト

  • button_up : ボタンが上がるとき(処理が完了したとき)のアニメーション
    引数: ボタン図形(Shape)オブジェクト

標準モジュール
Option Explicit

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)


Private Const button_depth = 1  'ボタンの厚み 0.5 から 2をお勧め
Private Const push_speed = 10   '小さいほど早い 厚みが薄いと大きく厚いと小さく調整する

'// ボタンを押したときのアニメーション
Public Sub button_down(button_shape As Shape)
    Dim n As Single

    For n = button_depth To 0 Step -0.1
        button_shape.ThreeD.BevelTopDepth = n
        button_shape.ThreeD.BevelBottomDepth = n
        Sleep 10
        DoEvents
    Next n

    button_shape.ThreeD.BevelTopType = msoBevelSoftRound
    button_shape.ThreeD.BevelBottomType = msoBevelSoftRound

    For n = 0 To button_depth Step 0.1
        button_shape.ThreeD.BevelTopDepth = n
        button_shape.ThreeD.BevelBottomDepth = n
        Sleep 10
        DoEvents
    Next n

End Sub

'// ボタンが上がるとき(処理が完了したとき)のアニメーション
Public Sub button_up(button_shape As Shape)
    Dim n As Single

    For n = button_depth To 0 Step -0.1
        button_shape.ThreeD.BevelTopDepth = n
        button_shape.ThreeD.BevelBottomDepth = n
        Sleep 10
        DoEvents
    Next n

    button_shape.ThreeD.BevelTopType = msoBevelCircle
    button_shape.ThreeD.BevelBottomType = msoBevelCircle

    For n = 0 To button_depth Step 0.1
        button_shape.ThreeD.BevelTopDepth = n
        button_shape.ThreeD.BevelBottomDepth = n
        Sleep 10
        DoEvents
    Next n

    End Sub

使用サンプル

Sheet1シートモジュール(ボタンアニメーションマクロの使用例)
    Private Sub button_push(shape_name As String)

        button_down Me.Shapes(shape_name)

        MsgBox "処理中"

        button_up Me.Shapes(shape_name)

    End Sub

'// アニメーションボタンの使用例
    Public Sub test()

        button_push "PROC1"

    End Sub

ボタンアニメーションマクロの使い方

  1. 任意の図形に適当な3D効果を設定し名前ボックスで名前をつけます。

    • サンプルでは"PROC1"
  2. マクロを登録します。

    • サンプルでは標準モジュールにアニメーション用マクロ本体を、シートモジュールに呼び出し用のマクロを書いています。
  3. ボタンアニメーションに関するパラメータを設定します。

    button_depth : ボタンの3D枠の深さ
    push_speed : アニメーション動作のスピード

    ボタンアニメーションパラメータの設定例
    Private Const button_depth = 1  'ボタンの厚み 0.5 から 2をお勧め
    Private Const push_speed = 10   '小さいほど早い 厚みが薄いと大きく厚いと小さく調整する
    
  4. ボタン(図形(Shape))を右クリックし「マクロの登録」で"Sheet1.test"を登録します。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0