程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#獲取鼠標所指像素的顏色

C#獲取鼠標所指像素的顏色

編輯:關於C#

1.創建一個C# windows應用程序

2.添加一個windows表單Label到Form1.cs

3.單擊label1控件然後更改Text屬性為空字符

4.更改BorderStyle屬性為FixedSingle

5.右鍵單擊Form1.cs,然後點擊View Code

添加下面Using語句到Form1.cs源碼的頂部

6.using System.Runtime.InteropServices;

注意該步驟添加必要的引用來調用InteropServices函數和方法

7.private Bitmap myBitmap;

添加下面的Win32APICall 類到Form1.cs中的Form1類後面

public class Win32APICall
{
[DllImport("gdi32.dll",EntryPoint="DeleteDC")]
public static extern IntPtr DeleteDC(IntPtr hdc);
[DllImport("gdi32.dll",EntryPoint="DeleteObject")]
public static extern IntPtr DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll",EntryPoint="BitBlt")]
public static extern bool BitBlt(IntPtr hdcDest,int nXDest,
int nYDest,int nWidth,int nHeight,IntPtr hdcSrc,
int nXSrc,int nYSrc,int dwRop);
[DllImport ("gdi32.dll",EntryPoint="CreateCompatibleBitmap")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,
int nWidth, int nHeight);
[DllImport ("gdi32.dll",EntryPoint="CreateCompatibleDC")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport ("gdi32.dll",EntryPoint="SelectObject")]
public static extern IntPtr SelectObject(IntPtr hdc,IntPtr hgdiobjBmp);
[DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll",EntryPoint="GetDC")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll",EntryPoint="GetSystemMetrics")]
public static extern int GetSystemMetrics(int nIndex);
[DllImport("user32.dll",EntryPoint="ReleaseDC")]
public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
public static Bitmap GetDesktop()
{
  int screenX;
  int screenY;
  IntPtr hBmp;
  IntPtr hdcScreen = GetDC(GetDesktopWindow());
  IntPtr hdcCompatible = CreateCompatibleDC(hdcScreen);
  screenX = GetSystemMetrics(0);
  screenY = GetSystemMetrics(1);
  hBmp = CreateCompatibleBitmap(hdcScreen, screenX, screenY);
  if (hBmp!=IntPtr.Zero)
  {
   IntPtr hOldBmp = (IntPtr) SelectObject(hdcCompatible, hBmp);
   BitBlt(hdcCompatible, 0, 0,screenX,screenY, hdcScreen, 0, 0,13369376);
   SelectObject(hdcCompatible, hOldBmp);
   DeleteDC(hdcCompatible);
   ReleaseDC(GetDesktopWindow(), hdcScreen);
   Bitmap bmp = System.Drawing.Image.FromHbitmap(hBmp);
   DeleteObject(hBmp);
   GC.Collect();
   return bmp;
  }
  return null;
  }
}

該步驟添加用於調用非托管windows GDI API函數所需的變量,結構和DllImport語句

8.添加下面代碼到在第二步創建的標簽的MouseDown 事件中

private void label1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e){  myBitmap = Win32APICall.GetDesktop();}

9.添加下面代碼到第二步創建的標簽的MouseUp 事件中

private void label1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e){  Color myColor = myBitmap.GetPixel(MousePosition.X,MousePosition.Y);  label1.BackColor = myColor;}

10.點擊運行,單擊並保持鼠標按鈕在標簽上保持按下狀態,在桌面拖動鼠標指針,然後釋放鼠標按鈕來捕獲顏色

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved