全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2198
推到 Plurk!
推到 Facebook!

如何取得滑鼠游標所在位置的RGB顏色值(十進位, 十六進位) ?

答題得分者是:taishyang
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-04-24 14:46:48 IP:61.220.xxx.xxx 訂閱
如何取得滑鼠游標所在位置的RGB顏色值(十進位, 十六進位) ?
謝謝 ^_^
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-04-24 15:21:20 IP:118.169.xxx.xxx 訂閱
取得目標的hdc,再透過
COLORREF GetPixel(
HDC hdc, // handle of device context
int XPos, // x-coordinate of pixel
int nYPos // y-coordinate of pixel
);

不知道是否可行?
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-04-24 16:36:27 IP:61.220.xxx.xxx 訂閱

寫好了


[code delphi]

unit Unit1;

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

type
TForm1 = class(TForm)
Timer1: TTimer;
Edit1: TEdit;
Edit2: TEdit;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.Caption := 'Get Cursor RGB Value';
Timer1.Interval := 50;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
const
Digits: array[0..15] of char =
('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
var
dc:hdc;
hnd:hwnd;
p:Tpoint;
v : Integer;
R1,G1,B1 : Integer;
R2,G2,B2 : String;
begin
hnd:=getdesktopwindow(); //
dc:=getwindowdc(hnd); //
//上面的2句也可以寫成dc:=getwindowdc(0)
Windows.GetCursorPos(p);
//Edit1.Text := InttoStr(getpixel(dc,p.X,p.Y));

v := getpixel(dc,p.X,p.Y);
R1 := v div 65536;
G1 := (v mod 65536) div 256;
B1 := v mod 256 ;
R2 := Digits[(R1 div 16)] Digits[(R1 mod 16)];
G2 := Digits[(G1 div 16)] Digits[(G1 mod 16)];
B2 := Digits[(B1 div 16)] Digits[(B1 mod 16)];
Edit1.Text := IntToStr(R1) '-' IntToStr(G1) '-' IntToStr(B1);
Edit2.Text := R2 '-' G2 '-' B2;
releasedc(handle,dc);
end;

end.
[/code]
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#4 引用回覆 回覆 發表時間:2009-04-24 16:40:12 IP:118.169.xxx.xxx 訂閱
取B可用GetBValue
取G可用GetGValue
取R可用GetRValue

用IntToHex可以轉換成16進位 ^_^
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#5 引用回覆 回覆 發表時間:2009-04-24 19:37:25 IP:203.73.xxx.xxx 訂閱
修改如下 (IntToHEX 就請您表演吧)


[code delphi]
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.Caption := 'Get Cursor RGB Value';
Timer1.Interval := 50;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
const
Digits: array[0..15] of char =
('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
var
dc:hdc;
hnd:hwnd;
p:Tpoint;
v : Integer;
R1,G1,B1 : Integer;
R2,G2,B2 : String;
begin
hnd:=getdesktopwindow(); //
dc:=getwindowdc(hnd); //
//上面的2句也可以寫成dc:=getwindowdc(0)
Windows.GetCursorPos(p);
//Edit1.Text := InttoStr(getpixel(dc,p.X,p.Y));
v := getpixel(dc,p.X,p.Y);
//R1 := v div 65536;
//G1 := (v mod 65536) div 256;
//B1 := v mod 256 ;
R1 := GetRValue(v);
G1 := GetGValue(v);
B1 := GetBValue(v);
R2 := Digits[(R1 div 16)] Digits[(R1 mod 16)];
G2 := Digits[(G1 div 16)] Digits[(G1 mod 16)];
B2 := Digits[(B1 div 16)] Digits[(B1 mod 16)];
//R2 := IntToHEX(R1); // Failed
//R2 := (String)IntToHEX(R1); // Failed
Edit1.Text := IntToStr(R1) '-' IntToStr(G1) '-' IntToStr(B1);
Edit2.Text := R2 '-' G2 '-' B2;
releasedc(handle,dc);
end;

end.

[/code]
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#6 引用回覆 回覆 發表時間:2009-04-24 19:40:48 IP:118.169.xxx.xxx 訂閱
R2 := IntToHEX(R1, 2);
系統時間:2024-04-20 3:01:50
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!