1. Create a C # Windows application
2. Add a Windows Form label to Form1.cs
3. Click the Label1 control and change the Text property to a null character
4. Change the BorderStyle property to FixedSingle
5. Right-click Form1.cs, then click View Code
Add the following using statement to the top of the Form1.cs source code
6.using System.Runtime.InteropServices;
Note that this step adds the necessary references to invoke the InteropServices function and method
7.private Bitmap Mybitmap;
Add the following Win32apicall class to the Form1 class in Form1.cs
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;
}
}
This step adds the variables, structures, and DllImport statements needed to invoke unmanaged Windows GDI API functions
8. Add the following code to the MouseDown event of the label created in step two
private void Label1_mousedown (object sender, System.Windows.Forms.MouseEventArgs e) {mybitmap = Win32apicall.getdesktop ();}
9. Add the following code to the MouseUp event of the label created in step two
private void Label1_mouseup (object sender, System.Windows.Forms.MouseEventArgs e) {Color MyColor = Mybitmap.getpixel ( MOUSEPOSITION.X,MOUSEPOSITION.Y); Label1. BackColor = MyColor;}
10. Click to run, clicking and holding the mouse button on the label to keep the state, drag the mouse pointer on the desktop, and then release the mouse button to capture the color