none
WindowsMediaPlayer上での透明ボタンの配置 RRS feed

  • 質問

  • お世話になります。

    VB2008で開発を行っております。WindowsMediaPlayer上での透明ボタンの配置について質問します。

    用途は動画上の任意のポイントを選択させて、そこを拡大したりするというものです。動画の拡大などの制御自体は別の仕組みで行うのですが、GUIでの見せ方、選択イベントの取得方法で行き詰っています。

    行いたいことですが、WindowsMediaPlayerのコントロールをフォーム上に配置。そのコントロールの上層に透明で枠のあるボタンを配置し、動画をストリームで再生させながら、透明なボタンで動画を見ながら選択するというものです。

    MediaPlayerの配置はツールボックスアイテムでCOMコントロールの追加で実現。関連してピクチャボックス上のボタンの透過はMSDNなどで見つかりました。下記ソース。

    ただ、動画上での透明ボタンが見つからず困っています。どなたか実現方法があればご教授下さい。

    ※無理に透明ボタンを配置せずにクリックイベントの座標を拾う方法も並行して検討はしてます。。。

    ---ソースはここから (ビットマップを読み込んでいるPictureBox7上でButton3を透過させてます。)

            'ピクチャボックス上のボタンを透過(背景画像を切り出して貼り付けることで透過に見せかける)
            Dim bitmap As New Bitmap(Me.PictureBox7.Image)
            Me.Button3.BackgroundImage = bitmap

            Using graphic As Graphics = Graphics.FromImage(bitmap)

                graphic.DrawImageUnscaledAndClipped(Me.PictureBox7.Image, New Rectangle(Me.PictureBox7.Location.X - Me.Button3.Location.X, Me.PictureBox7.Location.Y - Me.Button3.Location.Y, Me.PictureBox7.Width, Me.PictureBox7.Height))

            End Using

     ---ここまで

     

     

    2010年5月19日 4:49

回答

  • 全くレスがつきませんね。

    > 動画上での透明ボタンが見つからず困っています。どなたか実現方法があればご教授下さい。

    調べてみましたが、残念ながら WindowsForms だと実現不可能に思われます。WPF なら何とかなりそうなので、少し試してみました。

    単一セルの Grid 上に MediaElement をセルいっぱいに配置。同じく Grid 上にボタンを二つ配置します。以下 XAML の内容です。

    <Window x:Class="Window1"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Title="Window1" Height="300" Width="300">
     <Grid >
      <MediaElement Height="Auto" Margin="0" Name="MediaElement1" VerticalAlignment="Stretch" 
          Source="C:\Projects\sample.avi" LoadedBehavior="Manual" />
      <Button Height="23" Margin="0,0,33,23" Name="btnStop" HorizontalAlignment="Right" 
        VerticalAlignment="Bottom" Width="75" Opacity="0.1">Stop</Button>
      <Button Height="23" Margin="76,0,127,23" Name="btnPlay" 
        VerticalAlignment="Bottom" Opacity="0.1">Play</Button>
     </Grid>
    </Window>

    Button の Opacity プロパティを 0.1 に設定しているところが味噌です。次に VB のコード、ボタンクリックイベントで Play・Stop を操作します。

    Class Window1
    
     Private Sub btnPlay_Click( _
      ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPlay.Click
      MediaElement1.Play()
     End Sub
    
     Private Sub btnStop_Click( _
      ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnStop.Click
      MediaElement1.Stop()
     End Sub
    
    End Class
    

    これで、動画コントロール上での透明ボタンが実装できました。実際動かしてみるとなかなかいい感じです。以上、何かの参考になれば幸いです。


    ひらぽん http://d.hatena.ne.jp/hilapon/
    2010年5月25日 2:55
    モデレータ
  • ひらぽんさま

    ほぼ、頂いたままですが、要望としてはボタンを透明にするので、OpacityをTransparentにして実現してみました。

    2つの動画をそれぞれのPlay、Stopボタンで切り替えて制御するソースです。

    あとはフォームコントロールにどう取り込むかですが、自分で試してみます。

     

     

    <Window x:Class="Window1"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Title="Window1" Height="480" Width="640">
        <Grid >
            <MediaElement LoadedBehavior="Manual" Margin="10,10,2.304,-10" Name="MediaElement2" Source="http://www.・・・/・・・.asx" Stretch="Fill" />
            <MediaElement Margin="0,0,12.304,0" Name="MediaElement1" Source="C:\・・・\・・・\・・・.wmv" Stretch="Fill" LoadedBehavior="Manual" />
            <Button Margin="0,12,76.9,0" Name="btnStop" Opacity="1" BorderThickness="300" BorderBrush="DarkBlue" HorizontalAlignment="Right" Width="153.8" ClickMode="Press" Background="Transparent" Foreground="DarkOliveGreen" Height="49.985" VerticalAlignment="Top" OverridesDefaultStyle="False">Stop</Button>
            <Button Margin="84.59,12,0,0" Name="btnPlay" Opacity="1" BorderThickness="200" BorderBrush="Red" HorizontalAlignment="Left" Width="147.648" OpacityMask="Cyan" OverridesDefaultStyle="False" Background="Transparent" Foreground="Tomato" Height="49.985" VerticalAlignment="Top">Play</Button>
            <Label Height="43.064" Margin="113.812,0,202.247,33.836" Name="Label1" VerticalAlignment="Bottom" Opacity="0.5" BorderBrush="Black" BorderThickness="10" ForceCursor="True">Label</Label>
            <Button Height="23" Margin="0,0,106.122,44.4" Name="Button1" VerticalAlignment="Bottom" ForceCursor="True" HorizontalAlignment="Right" Width="74.894">Button</Button>
            <Button Background="Transparent" BorderBrush="Red" BorderThickness="200" Foreground="Tomato" HorizontalAlignment="Left" Margin="84.617,67.672,0,0" Name="btnPlay2" Opacity="1" OpacityMask="Cyan" OverridesDefaultStyle="False" Width="147.648" Height="39.219" VerticalAlignment="Top">Play(stream)</Button>
            <Button Background="Transparent" BorderBrush="DarkBlue" BorderThickness="300" ClickMode="Release" Foreground="DarkOliveGreen" HorizontalAlignment="Right" Margin="0,67.672,77.669,0" Name="btnStop2" Opacity="1" Width="153.8" Height="39.219" VerticalAlignment="Top" OverridesDefaultStyle="False">Stop</Button>
        </Grid>
    </Window>

     

     

     

    Class Window1
        Private Sub btnPlay_Click( _
         ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPlay.Click
            MediaElement2.Visibility = Windows.Visibility.Hidden
            MediaElement1.Visibility = Windows.Visibility.Visible
            MediaElement1.Play()
        End Sub

        Private Sub btnStop_Click( _
         ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnStop.Click
            MediaElement1.Stop()
        End Sub

        Private Sub btnPlay2_Click( _
         ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPlay2.Click
            MediaElement1.Visibility = Windows.Visibility.Hidden
            MediaElement2.Visibility = Windows.Visibility.Visible

            MediaElement2.Play()
        End Sub

        Private Sub btnStop2_Click( _
         ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnStop2.Click
            MediaElement2.Stop()
        End Sub
    End Class

     

    ありがとうございました。

     

    • 回答としてマーク 山本春海 2010年5月31日 7:14
    2010年5月31日 4:51

すべての返信

  • 全くレスがつきませんね。

    > 動画上での透明ボタンが見つからず困っています。どなたか実現方法があればご教授下さい。

    調べてみましたが、残念ながら WindowsForms だと実現不可能に思われます。WPF なら何とかなりそうなので、少し試してみました。

    単一セルの Grid 上に MediaElement をセルいっぱいに配置。同じく Grid 上にボタンを二つ配置します。以下 XAML の内容です。

    <Window x:Class="Window1"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Title="Window1" Height="300" Width="300">
     <Grid >
      <MediaElement Height="Auto" Margin="0" Name="MediaElement1" VerticalAlignment="Stretch" 
          Source="C:\Projects\sample.avi" LoadedBehavior="Manual" />
      <Button Height="23" Margin="0,0,33,23" Name="btnStop" HorizontalAlignment="Right" 
        VerticalAlignment="Bottom" Width="75" Opacity="0.1">Stop</Button>
      <Button Height="23" Margin="76,0,127,23" Name="btnPlay" 
        VerticalAlignment="Bottom" Opacity="0.1">Play</Button>
     </Grid>
    </Window>

    Button の Opacity プロパティを 0.1 に設定しているところが味噌です。次に VB のコード、ボタンクリックイベントで Play・Stop を操作します。

    Class Window1
    
     Private Sub btnPlay_Click( _
      ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPlay.Click
      MediaElement1.Play()
     End Sub
    
     Private Sub btnStop_Click( _
      ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnStop.Click
      MediaElement1.Stop()
     End Sub
    
    End Class
    

    これで、動画コントロール上での透明ボタンが実装できました。実際動かしてみるとなかなかいい感じです。以上、何かの参考になれば幸いです。


    ひらぽん http://d.hatena.ne.jp/hilapon/
    2010年5月25日 2:55
    モデレータ
  • ひらぽんさん

    コメントありがとうございます!あきらめかけてました。

    すぐに試す環境がないのですが、出来るだけ早く確認してみます。

    結果は後日。。。

    2010年5月28日 6:08
  • ひらぽんさま

    ほぼ、頂いたままですが、要望としてはボタンを透明にするので、OpacityをTransparentにして実現してみました。

    2つの動画をそれぞれのPlay、Stopボタンで切り替えて制御するソースです。

    あとはフォームコントロールにどう取り込むかですが、自分で試してみます。

     

     

    <Window x:Class="Window1"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Title="Window1" Height="480" Width="640">
        <Grid >
            <MediaElement LoadedBehavior="Manual" Margin="10,10,2.304,-10" Name="MediaElement2" Source="http://www.・・・/・・・.asx" Stretch="Fill" />
            <MediaElement Margin="0,0,12.304,0" Name="MediaElement1" Source="C:\・・・\・・・\・・・.wmv" Stretch="Fill" LoadedBehavior="Manual" />
            <Button Margin="0,12,76.9,0" Name="btnStop" Opacity="1" BorderThickness="300" BorderBrush="DarkBlue" HorizontalAlignment="Right" Width="153.8" ClickMode="Press" Background="Transparent" Foreground="DarkOliveGreen" Height="49.985" VerticalAlignment="Top" OverridesDefaultStyle="False">Stop</Button>
            <Button Margin="84.59,12,0,0" Name="btnPlay" Opacity="1" BorderThickness="200" BorderBrush="Red" HorizontalAlignment="Left" Width="147.648" OpacityMask="Cyan" OverridesDefaultStyle="False" Background="Transparent" Foreground="Tomato" Height="49.985" VerticalAlignment="Top">Play</Button>
            <Label Height="43.064" Margin="113.812,0,202.247,33.836" Name="Label1" VerticalAlignment="Bottom" Opacity="0.5" BorderBrush="Black" BorderThickness="10" ForceCursor="True">Label</Label>
            <Button Height="23" Margin="0,0,106.122,44.4" Name="Button1" VerticalAlignment="Bottom" ForceCursor="True" HorizontalAlignment="Right" Width="74.894">Button</Button>
            <Button Background="Transparent" BorderBrush="Red" BorderThickness="200" Foreground="Tomato" HorizontalAlignment="Left" Margin="84.617,67.672,0,0" Name="btnPlay2" Opacity="1" OpacityMask="Cyan" OverridesDefaultStyle="False" Width="147.648" Height="39.219" VerticalAlignment="Top">Play(stream)</Button>
            <Button Background="Transparent" BorderBrush="DarkBlue" BorderThickness="300" ClickMode="Release" Foreground="DarkOliveGreen" HorizontalAlignment="Right" Margin="0,67.672,77.669,0" Name="btnStop2" Opacity="1" Width="153.8" Height="39.219" VerticalAlignment="Top" OverridesDefaultStyle="False">Stop</Button>
        </Grid>
    </Window>

     

     

     

    Class Window1
        Private Sub btnPlay_Click( _
         ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPlay.Click
            MediaElement2.Visibility = Windows.Visibility.Hidden
            MediaElement1.Visibility = Windows.Visibility.Visible
            MediaElement1.Play()
        End Sub

        Private Sub btnStop_Click( _
         ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnStop.Click
            MediaElement1.Stop()
        End Sub

        Private Sub btnPlay2_Click( _
         ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPlay2.Click
            MediaElement1.Visibility = Windows.Visibility.Hidden
            MediaElement2.Visibility = Windows.Visibility.Visible

            MediaElement2.Play()
        End Sub

        Private Sub btnStop2_Click( _
         ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnStop2.Click
            MediaElement2.Stop()
        End Sub
    End Class

     

    ありがとうございました。

     

    • 回答としてマーク 山本春海 2010年5月31日 7:14
    2010年5月31日 4:51