WPF 如何实现颜色值拾取
生活随笔
收集整理的這篇文章主要介紹了
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代碼如下:
第三步?創建計時器?DispatcherTimer??代碼如下:
private readonly DispatcherTimer_timer = new DispatcherTimer();private?const?int?MousePullInfoIntervalInMs?=?10;第四步 構造函數?代碼如下:
第五步 ?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 增加控件?代碼如下:
第七步 實現Button 事件?代碼如下:
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 如何实现颜色值拾取的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Stardust]星尘配置中心
- 下一篇: 一图看懂 ASP.NET Core 中的