delphi取鼠标指定位置颜色

2018-10-31

源代码:
----------------------------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
   GroupBox1: TGroupBox;
   Panel1: TPanel;
   Label1: TLabel;
   Edit1: TEdit;
   Label2: TLabel;
   Label3: TLabel;
   Label4: TLabel;
   Edit2: TEdit;
   Edit3: TEdit;
   Edit4: TEdit;
   Label5: TLabel;
   Edit5: TEdit;
   Label6: TLabel;
   Edit6: TEdit;
   Timer1: TTimer;
   procedure Timer1Timer(Sender: TObject);

  p rivate
   { P rivate declarations }
  public
   constructor Create(AOwner: TComponent); override;
   procedure hotkeyProc(var msg:TMessage);message WM_HOTKEY;
   { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
{$APPTYPE CONSOLE}
var
  hotID:THandle;
  p1:TPoint;
  hHand1:THandle;
  hdc1:HDC;
  color1:Cardinal;
{ TForm1 }


constructor TForm1.Create(AOwner: TComponent);
begin
  inherited;
  //注册热键HOME
  RegisterHotKey(Self.Handle,hotID,0,VK_HOME);
  Timer1.Enabled:=true;
  p1:=Point(0,0);
end;


procedure TForm1.hotkeyProc(var msg: TMessage);
var
  r,g,b:Integer;
  c:TColor;

begin
  { TODO -o猪悟能 : 采集颜色 }
  c:=Color1;
  r:=GetRValue(ColorToRGB(c));
  g:=GetGValue(ColorToRGB(c));
  b:=GetBValue(ColorToRGB(c));
  Edit2.Text:=IntToStr(r);
  Edit3.Text:=IntToStr(g);
  Edit4.Text:=IntToStr(b);
  //Writeln(r);
  //Writeln(g);
  //Writeln(b);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin

  if GetCursorPos(p1)then
  begin
   Edit5.Text:=IntToStr(p1.X);
   Edit6.Text:=IntToStr(p1.Y);
   hHand1:=WindowFromPoint(p1);
   hdc1:=GetDC(hHand1);
   Windows.ScreenToClient(hHand1,p1);
   color1:=GetPixel(hdc1,p1.X,p1.y);
   Panel1.Color:=color1;
   Edit1.Text:=IntToStr(color1);
  end;
end;

end.


阅读12