日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

(WPF)酷狗音乐播放器

發布時間:2023/12/20 63 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (WPF)酷狗音乐播放器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近看了下wpf,感覺wpf做界面實在是太方便,使用blend來設計界面簡直不要太爽。通過mvvm模式來實現邏輯界面分離。我這里使用的是vs2013, .netFramework4.5.程序只是實現了很簡單的功能:歌曲播放,歌詞搜索,歌詞顯示 ,更換皮膚,textbox的水印文字。 下面請看主界面整體效果:

? ? ? 其實很簡陋,主要是圖片的效果掩人耳目了。 下面就談談具體的制作過程,同時也算是自己對wpf的一個回憶總結吧。

(1) 程序最基本的一般當然是control了。 那么首先就是Button按鈕了。Button 一般有3個狀態, 默認,懸浮,按下。 方法有很多,我這里的做法是:

?從Button派生一個類 ?ImageButton。?

public class ImageButton : Button{public ImageSource NorImage{get { return (ImageSource)GetValue(NorImageProperty); }set { SetValue(NorImageProperty, value); }}// Using a DependencyProperty as the backing store for NorImage. This enables animation, styling, binding, etc...public static readonly DependencyProperty NorImageProperty =DependencyProperty.Register("NorImage", typeof(ImageSource), typeof(ImageButton));public ImageSource HorImage{get { return (ImageSource)GetValue(HorImageProperty); }set { SetValue(HorImageProperty, value); }}// Using a DependencyProperty as the backing store for HorImage. This enables animation, styling, binding, etc...public static readonly DependencyProperty HorImageProperty =DependencyProperty.Register("HorImage", typeof(ImageSource), typeof(ImageButton));public ImageSource DownImage{get { return (ImageSource)GetValue(DownImageProperty); }set { SetValue(DownImageProperty, value); }}// Using a DependencyProperty as the backing store for DownImage. This enables animation, styling, binding, etc...public static readonly DependencyProperty DownImageProperty =DependencyProperty.Register("DownImage", typeof(ImageSource), typeof(ImageButton));}

? 然后定義Button的樣式, 定義好了樣式。所有按鈕只需使用這個樣式即可。 方法有很多 我這里是這樣定義的:

<span style="white-space:pre"> </span><pre name="code" class="html"><Style x:Key="ButtonStyle1" TargetType="{x:Type local:ImageButton}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:ImageButton}"><Grid><Image HorizontalAlignment="Left" x:Name="Nor" VerticalAlignment="Top" Source="{TemplateBinding NorImage}" Stretch="Fill"/><Image HorizontalAlignment="Left" x:Name="Hor" VerticalAlignment="Top" Source="{TemplateBinding HorImage}" Opacity="0" Stretch="Fill"/><Image HorizontalAlignment="Left" x:Name="Selected" VerticalAlignment="Top" Source="{TemplateBinding DownImage}" Opacity="0" Stretch="Fill"/></Grid><ControlTemplate.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter TargetName="Nor" Property="Opacity" Value="0" /><Setter TargetName="Hor" Property="Opacity" Value="100" /><Setter TargetName="Selected" Property="Opacity" Value="0" /></Trigger><Trigger Property="IsPressed" Value="True"><Setter TargetName="Nor" Property="Opacity" Value="0" /><Setter TargetName="Hor" Property="Opacity" Value="0" /><Setter TargetName="Selected" Property="Opacity" Value="100" /></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style> 如果.netFramework4.5還可以是用visual state 來設置。同時可以控制不同狀態的切換過渡時間,這樣視覺會更好些。 如下:

<span style="white-space:pre"></span><pre name="code" class="html"><Style x:Key="ImageButtonStyle" TargetType="{x:Type local:ImageButton}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:ImageButton}"><Grid><VisualStateManager.VisualStateGroups><VisualStateGroup x:Name="CommonStates"><VisualStateGroup.Transitions><VisualTransition GeneratedDuration="0:0:0.2"/></VisualStateGroup.Transitions><VisualState x:Name="Normal"><Storyboard><DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Hor"><EasingDoubleKeyFrame KeyTime="0" Value="0"/></DoubleAnimationUsingKeyFrames><DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Down"><EasingDoubleKeyFrame KeyTime="0" Value="0"/></DoubleAnimationUsingKeyFrames></Storyboard></VisualState><VisualState x:Name="MouseOver"><Storyboard><DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Nor"><EasingDoubleKeyFrame KeyTime="0" Value="0"/></DoubleAnimationUsingKeyFrames><DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Down"><EasingDoubleKeyFrame KeyTime="0" Value="0"/></DoubleAnimationUsingKeyFrames></Storyboard></VisualState><VisualState x:Name="Pressed"><Storyboard><DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Nor"><EasingDoubleKeyFrame KeyTime="0" Value="0"/></DoubleAnimationUsingKeyFrames><DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Hor"><EasingDoubleKeyFrame KeyTime="0" Value="0"/></DoubleAnimationUsingKeyFrames></Storyboard></VisualState><VisualState x:Name="Disabled"/></VisualStateGroup></VisualStateManager.VisualStateGroups><Image x:Name="Nor" Source="{TemplateBinding NorImage}" /><Image x:Name="Hor" Source="{TemplateBinding HorImage}" /><Image x:Name="Down" Source="{TemplateBinding DownImage}" /></Grid></ControlTemplate></Setter.Value></Setter></Style>
最后就只需要調用就行了 eg:

<local:ImageButton Style="{DynamicResource ButtonStyle1}" NorImage="pause_normal.png" HorImage="pause_hot.png" DownImage="pause_down.png" HorizontalAlignment="Left" Height="56" Margin="57,59,0,0" VerticalAlignment="Top" Width="61" />

(2) 第二部分應該就是RadionButton.。這里的做法和Button一樣。 RadionButton的控制如下:

(3) textbox的水印效果

? ? ? ?

<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}"><Style.Triggers><Trigger Property="IsFocused" Value="True"><Setter Property="Background" Value="#5FCBCBCB" /></Trigger><MultiTrigger><MultiTrigger.Conditions><Condition Property="IsFocused" Value="False"/><Condition Property="Text" Value=""/></MultiTrigger.Conditions><MultiTrigger.Setters><Setter Property="Background"><Setter.Value><VisualBrush Opacity="0.4" Stretch="None"><VisualBrush.Visual><TextBlock FontSize="12" HorizontalAlignment="Center" VerticalAlignment="Center" Text="輸入歌曲名 即可搜索歌詞" /></VisualBrush.Visual></VisualBrush></Setter.Value></Setter></MultiTrigger.Setters></MultiTrigger></Style.Triggers></Style> (4) 換膚的話 其實就是換了一張背景圖片。

(5) 歌詞搜索。 歌詞是從http://www.cnlyric.com/ 中文歌詞庫中搜索的, 調用http請求 。.接口如下:

http://www.cnlyric.com/search.php?k=KEY&t=s 。KEY就是要搜索的內容。搜索完后 就可以自己進行篩選。

(6) 歌詞滾動 。 大概方法就是 用一個StackPanel里面放若干個textblock, textblock的個數和歌詞行數一一對應。?

播放的時候用個定時器不停檢測當前播放進度,再對比StackPanel里面的歌詞的時間。 這里要多謝作者:?

QQ:919784826 的代碼。?他寫的比較全面。我只是拿了抽取了他的一部分功能來用


? ? ?程序地址(非源碼)

? ? 后續上傳源代碼。



總結

以上是生活随笔為你收集整理的(WPF)酷狗音乐播放器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。