<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
WPF實現 Gitee泡泡選單
框架使用大於等於.NET40
;
Visual Studio 2022
;
專案使用 MIT 開源許可協定;
實現程式碼
1) BubblleCanvas.cs 程式碼如下;
using System.Windows; using System.Windows.Controls; using WPFDevelopers.Helpers; using WPFDevelopers.Utilities; namespace WPFDevelopers.Controls { public class BubblleCanvas : Canvas { private double _bubbleItemX; private double _bubbleItemY; private int _number; private double _size; private const int _maxSize = 120; protected override Size ArrangeOverride(Size arrangeSize) { var width = arrangeSize.Width; var height = arrangeSize.Height; double left = 0d, top = 0d; for (var y = 0; y < (int)height / _maxSize; y++) { double yNum = y + 1; yNum = _maxSize * yNum; for (var x = 0; x < (int)width / _maxSize; x++) { if (_number > InternalChildren.Count - 1) return arrangeSize; var item = InternalChildren[_number] as FrameworkElement; if (DoubleUtil.IsNaN(item.ActualWidth) || DoubleUtil.IsZero(item.ActualWidth) || DoubleUtil.IsNaN(item.ActualHeight) || DoubleUtil.IsZero(item.ActualHeight)) ResizeItem(item); _bubbleItemX = Canvas.GetLeft(item); _bubbleItemY = Canvas.GetTop(item); if (double.IsNaN(_bubbleItemX) || double.IsNaN(_bubbleItemY)) { double xNum = x + 1; xNum = _maxSize * xNum; _bubbleItemX = ControlsHelper.NextDouble(left, xNum - _size * ControlsHelper.NextDouble(0.6, 0.9)); var _width = _bubbleItemX + _size; _width = _width > width ? width - (width - _bubbleItemX) - _size : _bubbleItemX; _bubbleItemX = _width; _bubbleItemY = ControlsHelper.NextDouble(top, yNum - _size * ControlsHelper.NextDouble(0.6, 0.9)); var _height = _bubbleItemY + _size; _height = _height > height ? height - (height - _bubbleItemY) - _size : _bubbleItemY; _bubbleItemY = _height; } Canvas.SetLeft(item, _bubbleItemX); Canvas.SetTop(item, _bubbleItemY); left = left + _size; _number++; item.Arrange(new Rect(new Point(_bubbleItemX, _bubbleItemY), new Size(_size, _size))); } left = 0d; top = top + _maxSize; } return arrangeSize; } private void ResizeItem(FrameworkElement item) { if (DoubleUtil.GreaterThanOrClose(item.DesiredSize.Width, 55)) _size = ControlsHelper.GetRandom.Next(80, _maxSize); else _size = ControlsHelper.GetRandom.Next(55, _maxSize); item.Width = _size; item.Height = _size; } } }
2) ControlsHelper.cs 程式碼如下;
private static long _tick = DateTime.Now.Ticks; public static Random GetRandom = new Random((int)(_tick & 0xffffffffL) | (int)(_tick >> 32)); public static double NextDouble(double miniDouble, double maxiDouble) { if (GetRandom != null) { return GetRandom.NextDouble() * (maxiDouble - miniDouble) + miniDouble; } else { return 0.0d; } } public static Brush RandomBrush() { var R = GetRandom.Next(255); var G = GetRandom.Next(255); var B = GetRandom.Next(255); var color = Color.FromRgb((byte)R, (byte)G, (byte)B); var solidColorBrush = new SolidColorBrush(color); return solidColorBrush; }
3) BubbleControl.cs 程式碼如下;
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; using WPFDevelopers.Helpers; namespace WPFDevelopers.Controls { [TemplatePart(Name = BorderTemplateName, Type = typeof(Border))] [TemplatePart(Name = EllipseTemplateName, Type = typeof(Ellipse))] [TemplatePart(Name = RotateTransformTemplateName, Type = typeof(RotateTransform))] public class BubblleControl : Control { private const string BorderTemplateName = "PART_Border"; private const string EllipseTemplateName = "PART_Ellipse"; private const string RotateTransformTemplateName = "PART_EllipseRotateTransform"; private const string ListBoxTemplateName = "PART_ListBox"; private static readonly Type _typeofSelf = typeof(BubblleControl); private ObservableCollection<BubblleItem> _items = new ObservableCollection<BubblleItem>(); private Border _border; private Ellipse _ellipse; private RotateTransform _rotateTransform; private Brush[] brushs; private ItemsControl _listBox; private static RoutedCommand _clieckCommand; class BubblleItem { public string Text { get; set; } public Brush Bg { get; set; } } static BubblleControl() { InitializeCommands(); DefaultStyleKeyProperty.OverrideMetadata(_typeofSelf, new FrameworkPropertyMetadata(_typeofSelf)); } #region Event public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), _typeofSelf); public event RoutedEventHandler Click { add { AddHandler(ClickEvent, value); } remove { RemoveHandler(ClickEvent, value); } } #endregion #region Command private static RoutedCommand _clickCommand = null; private static void InitializeCommands() { _clickCommand = new RoutedCommand("Click", _typeofSelf); CommandManager.RegisterClassCommandBinding(_typeofSelf, new CommandBinding(_clickCommand, OnClickCommand, OnCanClickCommand)); } public static RoutedCommand ClickCommand { get { return _clickCommand; } } private static void OnClickCommand(object sender, ExecutedRoutedEventArgs e) { var ctrl = sender as BubblleControl; ctrl.SetValue(SelectedTextPropertyKey, e.Parameter?.ToString()); ctrl.RaiseEvent(new RoutedEventArgs(ClickEvent)); } private static void OnCanClickCommand(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } #endregion #region readonly Properties private static readonly DependencyPropertyKey SelectedTextPropertyKey = DependencyProperty.RegisterReadOnly("SelectedText", typeof(string), _typeofSelf, new PropertyMetadata(null)); public static readonly DependencyProperty SelectedTextProperty = SelectedTextPropertyKey.DependencyProperty; public string SelectedText { get { return (string)GetValue(SelectedTextProperty); } } public new static readonly DependencyProperty BorderBackgroundProperty = DependencyProperty.Register("BorderBackground", typeof(Brush), typeof(BubblleControl), new PropertyMetadata(null)); public new static readonly DependencyProperty EarthBackgroundProperty = DependencyProperty.Register("EarthBackground", typeof(Brush), typeof(BubblleControl), new PropertyMetadata(Brushes.DarkOrchid)); public Brush BorderBackground { get => (Brush)this.GetValue(BorderBackgroundProperty); set => this.SetValue(BorderBackgroundProperty, (object)value); } public Brush EarthBackground { get => (Brush)this.GetValue(EarthBackgroundProperty); set => this.SetValue(EarthBackgroundProperty, (object)value); } #endregion #region Property public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable<string>), typeof(BubblleControl), new PropertyMetadata(null, OnItemsSourcePropertyChanged)); public IEnumerable<string> ItemsSource { get { return (IEnumerable<string>)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } } private static void OnItemsSourcePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { var ctrl = obj as BubblleControl; var newValue = e.NewValue as IEnumerable<string>; if (newValue == null) { ctrl._items.Clear(); return; } foreach (var item in newValue) { ctrl._items.Add(new BubblleItem { Text = item, Bg = ControlsHelper.RandomBrush() }); } } #endregion #region Override public override void OnApplyTemplate() { base.OnApplyTemplate(); _border = GetTemplateChild(BorderTemplateName) as Border; _ellipse = GetTemplateChild(EllipseTemplateName) as Ellipse; _rotateTransform = GetTemplateChild(RotateTransformTemplateName) as RotateTransform; Loaded += delegate { var point = _border.TranslatePoint(new Point(_border.ActualWidth / 2, _border.ActualHeight / 2), _ellipse); _rotateTransform.CenterX = point.X - _ellipse.ActualWidth / 2; _rotateTransform.CenterY = point.Y - _ellipse.ActualHeight / 2; }; _listBox = GetTemplateChild(ListBoxTemplateName) as ItemsControl; _listBox.ItemsSource = _items; } #endregion } }
4) BubblleControl.xaml 程式碼如下;
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:WPFDevelopers.Controls"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Basic/ControlBasic.xaml"/> <ResourceDictionary Source="Basic/Animations.xaml"/> </ResourceDictionary.MergedDictionaries> <Style TargetType="controls:BubblleControl" BasedOn="{StaticResource ControlBasicStyle}"> <Setter Property="Width" Value="400"/> <Setter Property="Height" Value="400"/> <Setter Property="Background" Value="{StaticResource WhiteSolidColorBrush}"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="{StaticResource SecondaryTextSolidColorBrush}"/> <Setter Property="BorderBackground" Value="{StaticResource BaseSolidColorBrush}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="controls:BubblleControl"> <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding BorderBackground}" Margin="45" CornerRadius="400" x:Name="PART_Border"> <Ellipse Fill="{TemplateBinding Background}" Margin="20"/> </Border> <Ellipse Fill="{TemplateBinding EarthBackground}" Width="26" Height="26" RenderTransformOrigin=".5,.5" x:Name="PART_Ellipse" VerticalAlignment="Top" Margin="0,35,0,0"> <Ellipse.RenderTransform> <RotateTransform x:Name="PART_EllipseRotateTransform"></RotateTransform> </Ellipse.RenderTransform> <Ellipse.Triggers> <EventTrigger RoutedEvent="Loaded"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="(Ellipse.RenderTransform).(RotateTransform.Angle)" RepeatBehavior="Forever" From="0" To="360" Duration="00:00:13"></DoubleAnimation> </Storyboard> </BeginStoryboard> </EventTrigger> </Ellipse.Triggers> </Ellipse> <ItemsControl x:Name="PART_ListBox" ItemsSource="{TemplateBinding ItemsSource}"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid> <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> <Ellipse Fill="{Binding Bg}" Opacity=".4"/> <Ellipse Stroke="{Binding Bg}" StrokeThickness=".8"/> </Grid> <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Padding="10,0"> <Hyperlink Foreground="{Binding Bg}" Command="{x:Static controls:BubblleControl.ClickCommand}" CommandParameter="{Binding Text}" FontWeight="Normal"> <TextBlock Text="{Binding Text}" TextAlignment="Center" TextTrimming="CharacterEllipsis" ToolTip="{Binding Text}"/> </Hyperlink> </TextBlock> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <controls:BubblleCanvas/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
5) BubblleControlExample.xaml 程式碼如下;
<UserControl x:Class="WPFDevelopers.Samples.ExampleViews.BubblleControlExample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:wpfdev="https://github.com/WPFDevelopersOrg/WPFDevelopers" xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews" xmlns:sys="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid> <TabControl> <TabItem Header="隨機"> <wpfdev:BubblleControl x:Name="MyBubblleControl" Click="BubblleControl_Click"> <wpfdev:BubblleControl.ItemsSource> <x:Array Type="sys:String"> <sys:String>WPF</sys:String> <sys:String>ASP.NET</sys:String> <sys:String>WinUI</sys:String> <sys:String>WebAPI</sys:String> <sys:String>Blazor</sys:String> <sys:String>MAUI</sys:String> <sys:String>Xamarin</sys:String> <sys:String>WinForm</sys:String> <sys:String>UWP</sys:String> </x:Array> </wpfdev:BubblleControl.ItemsSource> </wpfdev:BubblleControl> </TabItem> <TabItem Header="自定義"> <wpfdev:BubblleCanvas Width="400" Height="400"> <Grid> <Grid Width="60" Height="60"> <Ellipse Fill="MediumSpringGreen" Opacity=".4"/> <Ellipse Stroke="MediumSpringGreen" StrokeThickness=".8"/> </Grid> <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Padding="10,0"> <Hyperlink Foreground="MediumSpringGreen" FontWeight="Normal" Command="{Binding ClickCommand,RelativeSource={RelativeSource AncestorType=local:BubblleControlExample}}"> <TextBlock Text="WPF" TextAlignment="Center" TextTrimming="CharacterEllipsis"/> </Hyperlink> </TextBlock> </Grid> <Grid> <Grid Width="60" Height="60"> <Ellipse Fill="Brown" Opacity=".4"/> <Ellipse Stroke="Brown" StrokeThickness=".8"/> </Grid> <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Padding="10,0"> <Hyperlink Foreground="Brown" FontWeight="Normal" Command="{Binding ClickCommand,RelativeSource={RelativeSource AncestorType=local:BubblleControlExample}}"> <TextBlock Text="MAUI" TextAlignment="Center" TextTrimming="CharacterEllipsis"/> </Hyperlink> </TextBlock> </Grid> <Grid> <Grid Width="60" Height="60"> <Ellipse Fill="DeepSkyBlue" Opacity=".4"/> <Ellipse Stroke="DeepSkyBlue" StrokeThickness=".8"/> </Grid> <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Padding="10,0"> <Hyperlink Foreground="DeepSkyBlue" FontWeight="Normal" Command="{Binding ClickCommand,RelativeSource={RelativeSource AncestorType=local:BubblleControlExample}}"> <TextBlock Text="Blazor" TextAlignment="Center" TextTrimming="CharacterEllipsis"/> </Hyperlink> </TextBlock> </Grid> </wpfdev:BubblleCanvas> </TabItem> </TabControl> </Grid> </UserControl>
6) BubblleControlExample.xaml.cs 程式碼如下;
using System.Windows; using System.Windows.Controls; using System.Windows.Input; using WPFDevelopers.Samples.Helpers; namespace WPFDevelopers.Samples.ExampleViews { /// <summary> /// BubbleControlExample.xaml 的互動邏輯 /// </summary> public partial class BubblleControlExample : UserControl { public BubblleControlExample() { InitializeComponent(); } public ICommand ClickCommand => new RelayCommand(delegate { WPFDevelopers.Minimal.Controls.MessageBox.Show("點選完成。"); }); private void BubblleControl_Click(object sender, System.Windows.RoutedEventArgs e) { MessageBox.Show($"點選了「 {MyBubblleControl.SelectedText}開發者 」.", "提示",MessageBoxButton.OK,MessageBoxImage.Information); } } }
以上就是WPF模擬實現Gitee泡泡選單的範例程式碼的詳細內容,更多關於WPF泡泡選單的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45