最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WPF實現(xiàn)背景燈光隨鼠標(biāo)閃動效果

 更新時間:2020年08月28日 15:08:22   作者:RunnerDNA  
這篇文章主要為大家詳細(xì)介紹了WPF實現(xiàn)背景燈光隨鼠標(biāo)閃動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了WPF實現(xiàn)背景燈光隨鼠標(biāo)閃動的具體代碼,供大家參考,具體內(nèi)容如下

實現(xiàn)效果如下:

思路:將容器分割成組合三角形Path,鼠標(biāo)移動時更新每個三角形的填充顏色。

步驟:

1、窗體xaml

只需放置一個Canvas。

<Canvas x:Name="container" Width="400" Height="400"></Canvas>

2、交互邏輯

/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
 public partial class MainWindow : Window
 {
  private Point lastMousePosition = new Point(0, 0);//鼠標(biāo)位置
  private int triangleLength = 100;//三角形邊長
 
  public MainWindow()
  {
   InitializeComponent();
   this.Loaded += MainWindow_Loaded;
   CompositionTarget.Rendering += UpdateTriangle;
   this.container.PreviewMouseMove += UpdateLastMousePosition;
  }
 
  private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  {
   //將長方形容易劃分成組合三角形
   int horizontalCount = (int)(this.container.ActualWidth / triangleLength);
   int verticalCount = (int)(this.container.ActualHeight / triangleLength);
   for (int i = 0; i < horizontalCount; i++)
   {
    for (int j = 0; j < verticalCount; j++)
    {
     Path trianglePath1 = new Path();
     var g1 = new StreamGeometry();
     using (StreamGeometryContext context = g1.Open())
     {
      context.BeginFigure(new Point(i * triangleLength, j * triangleLength), true, true);
      context.LineTo(new Point(i * triangleLength, (j + 1) * triangleLength), true, false);
      context.LineTo(new Point((i + 1) * triangleLength, (j + 1) * triangleLength), true, false);
     }
     trianglePath1.Data = g1;
     trianglePath1.Fill = new SolidColorBrush(Color.FromArgb(255, 247, 18, 65));
     this.container.Children.Add(trianglePath1);
 
     Path trianglePath2 = new Path();
     var g2 = new StreamGeometry();
     using (StreamGeometryContext context = g2.Open())
     {
      context.BeginFigure(new Point(i * triangleLength, j * triangleLength), true, true);
      context.LineTo(new Point((i + 1) * triangleLength, j * triangleLength), true, false);
      context.LineTo(new Point((i + 1) * triangleLength, (j + 1) * triangleLength), true, false);
     }
     trianglePath2.Data = g2;
     trianglePath2.Fill = new SolidColorBrush(Color.FromArgb(255, 247, 18, 65));
     this.container.Children.Add(trianglePath2);
    }
   }
  }
 
  private void UpdateTriangle(object sender, EventArgs e)
  {
   //獲取子控件
   List<Path> childList = GetChildObjects<Path>(this.container);
   for (int i = 0; i < childList.Count; i++)
   {
    for (int j = 1; j < childList.Count; j++)
    {
     string si = childList[i].Data.ToString();
     string si1 = MidStrEx(si, "M", "L");
     string si2 = MidStrEx(si, "L", " ");
     string si3 = MidStrEx(si, " ", "z");
     string sj = childList[j].Data.ToString();
     string sj1 = MidStrEx(sj, "M", "L");
     string sj2 = MidStrEx(sj, "L", " ");
     string sj3 = MidStrEx(sj, " ", "z");
     //左右三角形判斷
     if (si1 == sj1 && si3 == sj3)
     {
      double x = childList[i].Data.Bounds.X + (1 - Math.Pow(2, 0.5) / 2) * triangleLength - lastMousePosition.X;
      double y = childList[i].Data.Bounds.Y + (1 - Math.Pow(2, 0.5) / 2) * triangleLength - lastMousePosition.Y;
      double rRadio = 1 - Math.Pow(x * x + y * y, 0.5) / Math.Pow(this.container.ActualWidth * this.container.ActualWidth + this.container.ActualHeight * this.container.ActualHeight, 0.5);
      childList[j].Fill = new SolidColorBrush(Color.FromArgb((byte)(255 * rRadio), 247, 18, 65));
      x = childList[j].Data.Bounds.TopRight.X - (1 - Math.Pow(2, 0.5) / 2) * triangleLength - lastMousePosition.X;
      rRadio = 1 - Math.Pow(x * x + y * y, 0.5) / Math.Pow(this.container.ActualWidth * this.container.ActualWidth + this.container.ActualHeight * this.container.ActualHeight, 0.5);
      childList[i].Fill = new SolidColorBrush(Color.FromArgb((byte)(255 * rRadio), 247, 18, 65));
      break;
     }
    }
   }
  }
 
  private void UpdateLastMousePosition(object sender, MouseEventArgs e)
  {
   lastMousePosition = e.GetPosition(this.container);
  }
 
  /// <summary>
  /// 獲得所有子控件
  /// </summary>
  private List<T> GetChildObjects<T>(System.Windows.DependencyObject obj) where T : System.Windows.FrameworkElement
  {
   System.Windows.DependencyObject child = null;
   List<T> childList = new List<T>();
   for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
   {
    child = VisualTreeHelper.GetChild(obj, i);
    if (child is T)
    {
     childList.Add((T)child);
    }
    childList.AddRange(GetChildObjects<T>(child));
   }
   return childList;
  }
 
  /// <summary>
  /// 截取兩個指定字符中間的字符串
  /// </summary>
  public static string MidStrEx(string sourse, string startstr, string endstr)
  {
   string result = string.Empty;
   int startindex, endindex;
   try
   {
    startindex = sourse.IndexOf(startstr);
    if (startindex == -1)
     return result;
    string tmpstr = sourse.Substring(startindex + startstr.Length);
    endindex = tmpstr.IndexOf(endstr);
    if (endindex == -1)
     return result;
    result = tmpstr.Remove(endindex);
   }
   catch (Exception ex)
   {
   }
   return result;
  }
}

說明:當(dāng)組合三角形過多時,會有明顯卡頓,需要優(yōu)化色彩更新方法。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺析wpf中datagrid顯示列的問題

    淺析wpf中datagrid顯示列的問題

    這篇文章主要為大家詳細(xì)介紹了wpf中datagrid顯示列問題的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,有需要的小伙伴可以學(xué)習(xí)一下
    2024-04-04
  • C#操作IIS程序池及站點的創(chuàng)建配置實現(xiàn)代碼

    C#操作IIS程序池及站點的創(chuàng)建配置實現(xiàn)代碼

    最近在做一個WEB程序的安裝包;對一些操作IIS進(jìn)行一個簡單的總結(jié);主要包括對IIS進(jìn)行站點的新建以及新建站點的NET版本的選擇,還有針對IIS7程序池的托管模式以及版本的操作
    2013-03-03
  • 圖解如何使用C#創(chuàng)建Windows服務(wù)

    圖解如何使用C#創(chuàng)建Windows服務(wù)

    本文主要介紹了圖解如何使用C#創(chuàng)建Windows服務(wù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • C#用戶控件之溫度計設(shè)計

    C#用戶控件之溫度計設(shè)計

    這篇文章主要為大家詳細(xì)介紹了C#用戶控件之溫度計的設(shè)計方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • C#使用windows服務(wù)開啟應(yīng)用程序的方法

    C#使用windows服務(wù)開啟應(yīng)用程序的方法

    這篇文章主要介紹了C#使用windows服務(wù)開啟應(yīng)用程序的方法,實例分析了C#操作windows服務(wù)開啟應(yīng)用程序所遇到的問題及相關(guān)解決技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • C#生成唯一不重復(fù)訂單號

    C#生成唯一不重復(fù)訂單號

    本文給大家介紹的是使用C#生成唯一不重復(fù)訂單號的方法,主要用到了lock鎖,有需要的小伙伴可以參考下。
    2015-07-07
  • C#使用第三方組件生成二維碼匯總

    C#使用第三方組件生成二維碼匯總

    本文給大家匯總了幾種C#使用第三方組件生成二維碼的方法以及示例,非常的簡單實用,都是項目中經(jīng)常需要用到的,希望大家能夠喜歡
    2016-12-12
  • C#中Dictionary泛型集合7種常見的用法

    C#中Dictionary泛型集合7種常見的用法

    本文主要介紹了Dictionary集合的7種最基礎(chǔ)的用法,包括創(chuàng)建、添加、查找、遍歷、刪除等方法,程序都是由簡入繁,希望能通過閱讀簡單的示例,給大家一些啟發(fā)。
    2016-03-03
  • C#實現(xiàn)網(wǎng)絡(luò)通信共享庫NetShare的使用示例

    C#實現(xiàn)網(wǎng)絡(luò)通信共享庫NetShare的使用示例

    本文主要介紹了C#實現(xiàn)網(wǎng)絡(luò)通信共享庫NetShare,網(wǎng)絡(luò)通信共享庫NetShare用于保證客戶端與服務(wù)器通信數(shù)據(jù)包的規(guī)范和統(tǒng)一,感興趣的可以了解一下
    2023-11-11
  • 淺談Visual C#進(jìn)行圖像處理(讀取、保存以及對像素的訪問)

    淺談Visual C#進(jìn)行圖像處理(讀取、保存以及對像素的訪問)

    本文主要介紹利用C#對圖像進(jìn)行讀取、保存以及對像素的訪問等操作,介紹的比較簡單,希望對初學(xué)者有所幫助。
    2016-04-04

最新評論

嘉禾县| 灯塔市| 永福县| 钟祥市| 刚察县| 多伦县| 黄骅市| 兰西县| 舞阳县| 隆回县| 奉新县| 安仁县| 大悟县| 长宁区| 商都县| 巩留县| 门源| 旅游| 盐边县| 平顶山市| 枞阳县| 兰溪市| 宁津县| 九江县| 镇平县| 临洮县| 剑川县| 牡丹江市| 台北县| 绍兴县| 岫岩| 登封市| 乌兰县| 高密市| 中江县| 丰台区| 南陵县| 青川县| 泾源县| 博野县| 崇义县|