应该可以用
System.Drawing.Graphics.FromHdc
System.Drawing.Graphics.FromHwnd
来取得Desktop的DC
然后用这个Graphics产生一个Bitmap.
然后调用Bitmap.GetPixel(int x,int y)
Cursor Position可以通过一个Static function
System.Windows.Forms.Cursor.Position()取得.

解决方案 »

  1.   

    你好:
       首先我非常感谢你给我的帮助,但是我还是有点不明白,还请你再帮忙帮忙,
    我的问题就是怎么才能获得桌面的DC呢?总不能又是调用GetDC吧?要知道,GETDC这个API转换实在是太麻烦了,由其最后一个参数,转换太长了!!
      那是不是用FROMHWND?但是如何获得桌面的HWND?烦请相告,多谢!!!
      

  2.   

    以下是我的代码!!
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace Whatclr
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Timer timer1;
    private System.ComponentModel.IContainer components;
    Color cr ;
    Bitmap MyImage;
    int i =0;
    IntPtr dc1;

    [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
    private static extern IntPtr  CreateDC(
    string lpszDriver,        // driver name
    string lpszDevice,        // device name
    string lpszOutput,        // not used; should be NULL
    IntPtr lpInitData // optional printer data
    );
    [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
             private static extern Int32 GetPixel(IntPtr hdc,Int32 pointX,Int32 pointY); public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    this.timer1 = new System.Windows.Forms.Timer(this.components);
    // 
    // timer1
    // 
    this.timer1.Enabled = true;
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.BackColor = System.Drawing.Color.White;
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void timer1_Tick(object sender, System.EventArgs e)
    {
    Point pt = new Point(Cursor.Position.X,Cursor.Position.Y) ;
        //int crWin  =GetPixel(dc1,pt.X,pt.Y);      //Correct Way to use
    //cr =  ColorTranslator.FromWin32(crWin);
    cr = MyImage.GetPixel(pt.X,pt.Y);
    this.Invalidate(); } private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
    Graphics dc = e.Graphics ;
    dc.DrawString(cr.ToString(),this.Font,new SolidBrush(Color.Black),0,i*10);
    i++;
    } private void Form1_Load(object sender, System.EventArgs e)
    {

     dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);
         Graphics g1 = Graphics.FromHdc(dc1);
         MyImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1);
        this.BackgroundImage = MyImage; }
    }
    }
      

  3.   

    [DllImport("user32.dll")]
    int GetDesktopWindow();
      

  4.   

    [DllImport("user32.dll")]
    internal extern static IntPtr GetDesktopWindow();
      

  5.   

    谢谢上面的朋友,其实我想要的目标已经通过引用WINdows API完成了,上面的源代码已经列出来了,从代码可以看到我引用的API,但是被我注释了。
    现在我想知道的就是我按照所说blahblah朋友所说的,得不到我想要的东西,那位朋友感兴趣可以回去试试,我的程序目的其实很简单,就是在客户区上写出鼠标所指颜色!!!从我的调试来看,就是所得到的象素点颜色总是空值!!!
      

  6.   

    我的信箱是[email protected],谢谢上面的朋友!!!