急!如何得到屏幕上某一点的颜色值 (50分)

青云

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在想做一个软件,其中有这样一个功能,就是把屏幕上某一点的颜色值取出来。
我是这样尝试的:
首先得到鼠标位置: var ptMousePos:Tpoint;
procedure Tgetcol.Timer1Timer(Sender: TObject);
begin
GetCursorPos(ptMousePos);
edit1.text:=inttostr(ptMousePos.x)+inttostr(ptMousePos.y);
end;
下面我就不知道该如何做了,现在知道了鼠标的位置,但是如何知道该位置的颜色值呢。
恳请网上高手不吝赐教。
 

学会用搜索 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=193861
 
var T:TCanvas;
T:=TCanvas.Create;
T.Handle:=GetDC(0);
mycolor:=T.Pixels[mouse.x,Mouse.y);
T.Free;
 
影 子 朋友,您提供的代码我其实也有,问题不象您想象的那样简单
我这里关键要解决的问题是,鼠标在屏幕上任意一点的颜色值,而不是
from窗口中的某一点颜色值,要是仅仅是实现这个功能,就没有问的必要了。
同时我还给出了一段代码,其功能是得到屏幕某一点的鼠标位置(而不是form中的鼠标位置
,所以OnMouseMove事件没有用!),不过我还是很感谢您!
-------------------------------------------------------------
一少朋友,你给出的代码对我很有用,其实我也知道用GetDc这个api函数,只是不知道getdc(0)
就是得到屏幕的句柄。
-----------------------------------------------------------------

下面是我整理的代码,基本可以实现了取屏幕颜色值的问题,细节上可以参考影子朋友提供的网址
procedure Tgetcol.Timer1Timer(Sender: TObject);
var t:tcanvas;
ptMousePos:Tpoint;

begin
GetCursorPos(ptMousePos);
edit1.text:=inttostr(ptMousePos.x)+inttostr(ptMousePos.y);

t:=tcanvas.Create;
t.Handle:=getdc(0);
edit1.Color:=t.Pixels[ptMousePos.x,ptMousePos.y];
end;
 
110. 跟踪屏幕像素颜色
在Form中放一 TLabel(Label1),再放置一定时器(TTimer).
然后粘贴以下定时器代码即可运行。
当鼠标在屏幕的任何位置移动时,Label中显示鼠标所指
像素的颜色(十六进制 RGB 格式)。

///////////////以下是定时器的处理代码:
procedure TForm1.Timer1Timer(Sender: TObject);
var
P: TPoint;
co: TColor;
ScreenDC : HDC;
fBitmap : TBitmap;
begin
GetCurSorPos(P);
fBitmap := TBitmap.Create;
fBitmap.Width := 1;
fBitmap.Height := 1;
ScreenDC:=CreateDC('DISPLAY',nil,nil,nil);
BitBlt(FBitmap.Canvas.Handle, 0,0,
FBitmap.Width, FBitmap.Height,
ScreenDC, p.x,p.y, SRCCOPY);
DeleteDC(ScreenDC);
label1.caption:=inttohex(fbitmap.canvas.pixels[0,0],8);
fBitmap.Free
end;

 

在那个链接中,Crane的代码是可以实现全屏取色的。


procedure TForm1.Timer1Timer(Sender: TObject);
begin
Caption := ColorToString(GetPixel(GetDC(0),Mouse.CursorPos.X,Mouse.CursorPos.Y));
//分色可以参考GetBValue, GetGValue, GetRValue
end;
 
procedure Tgetcol.Timer1Timer(Sender: TObject);
var t:tcanvas;
ptMousePos:Tpoint;

begin
GetCursorPos(ptMousePos);
edit1.text:=inttostr(ptMousePos.x)+inttostr(ptMousePos.y);

t.Handle:=getdc(GetDesktopWindow);
edit1.Color:=t.Pixels[ptMousePos.x,ptMousePos.y];
end;
 
顶部