日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF 如何实现颜色值拾取

發布時間:2023/12/4 asp.net 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF 如何实现颜色值拾取 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

WPF開發者QQ群:?340500857?

前言

? ? ??如何進行顏色值拾取?這里采用的是調用WindowsAPI進行實現。

吸取 @沙漠盡頭的狼?的建議多寫一些文字進行描述。

效果圖如下:

第一步 注冊WindowsAPI 代碼如下:

????????[DllImport("user32.dll")]static extern IntPtr GetDC(IntPtr hwnd);[DllImport("user32.dll")]static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);[DllImport("gdi32.dll")]static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);

第二步 定義顏色拾取方法GetPixelColor?和 鼠標GetCursorPos代碼如下:

static public System.Windows.Media.Color GetPixelColor(int x, int y){IntPtr hdc = GetDC(IntPtr.Zero);uint pixel = GetPixel(hdc, x, y);ReleaseDC(IntPtr.Zero, hdc);Color color = Color.FromRgb((byte)(pixel & 0x000000FF),(byte)((pixel & 0x0000FF00) >> 8),(byte)((pixel & 0x00FF0000) >> 16));return color;}public class MyPoint{[StructLayout(LayoutKind.Sequential)]public struct POINT{public int X;public int Y;public POINT(int x, int y){this.X = x;this.Y = y;}}[DllImport("user32.dll", CharSet = CharSet.Auto)]public static extern bool GetCursorPos(out POINT pt);}

第三步?創建計時器?DispatcherTimer??代碼如下:

private readonly DispatcherTimer_timer = new DispatcherTimer();private?const?int?MousePullInfoIntervalInMs?=?10;

第四步 構造函數?代碼如下:

_timer.Interval = TimeSpan.FromMilliseconds(MousePullInfoIntervalInMs);_timer.Tick?+=?Timer_Tick;

第五步 ?Timer_Tick 代碼如下:

private void Timer_Tick(object sender, EventArgs e){MyPoint.POINT point = new MyPoint.POINT();var isMouseDown = MyPoint.GetCursorPos(out point);var color = GetPixelColor(point.X, point.Y);btnColor.Background = new SolidColorBrush(color); //new System.Windows.Media.BrushConverter().ConvertFromString(color);Console.WriteLine(color);}

第六步 XAML 增加控件?代碼如下:

<StackPanel><Button?Content="拾Color"?x:Name="btnColor"?Click="Button_Click"></Button><Button Content="取消" Click="Button_Click_1"></Button></StackPanel>

第七步 實現Button 事件?代碼如下:

private void Button_Click(object sender, RoutedEventArgs e){if(!_timer.IsEnabled){_timer.Start();}}private void Button_Click_1(object sender, RoutedEventArgs e){if (_timer.IsEnabled){_timer.Stop();}}

WPF開發者QQ群:?340500857?

blogs:?https://www.cnblogs.com/yanjinhua

Github:https://github.com/yanjinhuagood

作者:驚鏵

出處:https://www.cnblogs.com/yanjinhua

版權:本作品采用「署名-非商業性使用-相同方式共享 4.0 國際」許可協議進行許可。

轉載請著名作者 出處 https://github.com/yanjinhuagood

總結

以上是生活随笔為你收集整理的WPF 如何实现颜色值拾取的全部內容,希望文章能夠幫你解決所遇到的問題。

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