{$R *.res}
const
MyWindowStyleword=(WS_DLGFRAME or ws_popup);
Cr_Blackword=$000000;
Cr_Redword=$0000FF;
Cr_Greenword=$00FF00;
Cr_Blueword=$FF0000;
SC_DragMove = $F012;
WindowWidth=400;
WindowHeight=40;
{请勿删除此注释}
SoftInfo:string='Design By RHL 76517@21cn.com 95211.delphibbs.com';
var
hHook:Cardinal;
hwnd1:Cardinal;
DC:Longint;
MyHdc,HBrush:Cardinal;
RectRepaint:Trect;
ScreenWidth,ScreenHeight:integer;
{ 回调函数 }
function AppWindowProc(
hWnd:HWND; uMsg:UINT;
wParam:WPARAM; lParam:LPARAM):LRESULT; stdcall;
begin
Result := 0;
case uMsg of
WM_DESTROY:begin
PostQuitMessage(0);
Exit;
end;
wm_moving:begin
if PRECT(lParam)^.right>=ScreenWidth then
begin
PRECT(lParam)^.Right:=ScreenWidth;
PRECT(lParam)^.Left:=ScreenWidth-WindowWidth;
if PRECT(lParam)^.top<=0 then
begin
PRECT(lParam)^.top:=0;
PRECT(lParam)^.Bottom:=WindowHeight;
end;
if PRECT(lParam)^.Bottom>=ScreenHeight then
begin
PRECT(lParam)^.top:=ScreenHeight-WindowHeight;
PRECT(lParam)^.Bottom:=ScreenHeight;
end;
end
else if PRECT(lParam)^.left<=0 then
begin
PRECT(lParam)^.Left:=0;
PRECT(lParam)^.Right:=WindowWidth;
if PRECT(lParam)^.top<=0 then
begin
PRECT(lParam)^.top:=0;
PRECT(lParam)^.Bottom:=WindowHeight;
end;
if PRECT(lParam)^.Bottom>=ScreenHeight then
begin
PRECT(lParam)^.Bottom:=ScreenHeight;
PRECT(lParam)^.top:=ScreenHeight-WindowHeight;
end;
end
else if PRECT(lParam)^.top<=0 then
begin
PRECT(lParam)^.top:=0;
PRECT(lParam)^.Bottom:=WindowHeight;
if PRECT(lParam)^.right>=ScreenWidth then
begin
PRECT(lParam)^.Right:=ScreenWidth;
PRECT(lParam)^.Left:=ScreenWidth-WindowWidth;
end;
if PRECT(lParam)^.left<=0 then
begin
PRECT(lParam)^.Right:=WindowWidth;
PRECT(lParam)^.Left:=0;
end;
end
else if PRECT(lParam)^.Bottom>=600 then
begin
PRECT(lParam)^.top:=ScreenHeight-WindowHeight;
PRECT(lParam)^.Bottom:=ScreenHeight;
if PRECT(lParam)^.right>=ScreenWidth then
begin
PRECT(lParam)^.Right:=ScreenWidth;
PRECT(lParam)^.Left:=ScreenWidth-WindowWidth;
end;
if PRECT(lParam)^.left<=0 then
begin
PRECT(lParam)^.Left:=0;
PRECT(lParam)^.right:=WindowWidth;
end;
end;
end;
end;
Result:=DefWindowProc(hWnd, uMsg, wParam, lParam);
end;
{指定当前的钩子函数}
function HookProc(iCode:Integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
var
MyEventMsg:EVENTMSG;
MouseX,MouseY:Longint;
ColorHex,TempStr:string;
begin
Result:=0;
if (iCode < 0) then
result:=CallNextHookEx(hHook,iCode,wParam,lParam)
else if iCode = HC_ACTION then
begin
if pEVENTMSG(lParam)^.message=WM_MOUSEMOVE then
begin
MyEventMsg:=pEVENTMSG(lParam)^;
MouseX:=MyEventMsg.paramL;
MouseY:=MyEventMsg.paramH;
SetTextColor(MyHdc,Cr_black);
fillrect(myhdc,RectRepaint,HBrush);
ColorHex:=inttohex(getpixel(DC,MouseX,MouseY),6);
TempStr:='CurrentColor:'+ColorHex;
textOut(MyHdc,0,0,Pchar(TempStr),19);
SetTextColor(MyHdc,Cr_Red);
TempStr:='R:'+copy(ColorHex,5,2)+' ';
textOut(MyHdc,140,0,Pchar(tempstr),4);
SetTextColor(MyHdc,Cr_Green);
TempStr:='G:'+copy(ColorHex,3,2)+' ';
textOut(MyHdc,175,0,Pchar(tempstr),4);
SetTextColor(MyHdc,Cr_Blue);
TempStr:='B:'+copy(ColorHex,1,2)+' ';
textOut(MyHdc,210,0,Pchar(tempstr),4);
textOut(MyHdc,0,17,Pchar(SoftInfo),48);
DrawIcon(MyHdc,WindowWidth-40,0,loadicon(Hinstance,'MainIcon'));
end;
end;
end;
var
wc: TWndClass;
MSG: TMsg;
begin
{ 程序从这里开始执行}
wc.style := CS_VREDRAW or CS_HREDRAW;
wc.lpfnWndProc := @AppWindowProc;
wc.cbClsExtra :=0;
wc.cbWndExtra :=0;
wc.hInstance := HInstance;
wc.hIcon := LoadIcon(Hinstance,Pchar('CloseSign'));
wc.hCursor := LoadCursor(0, IDC_ARROW);
wc.hbrBackground := (Color_infoBk+1);
wc.lpszMenuName := nil;
wc.lpszClassName := 'My App';
if RegisterClass(wc)=0 then Exit;
hWnd1 := CreateWindow(
wc.lpszClassName, 'Color Hunt',
mywindowstyle,
0, 0,
windowWidth, WindowHeight,
0, 0, HInstance, nil);
if hWnd1=0 then Exit;
ShowWindow(hWnd1,SW_SHOWDEFAULT);
Setwindowpos(hwnd1,HWND_TOPMOST,20,20,0,0,SWP_NOMOVE or SWP_NOSIZE);
//HMyFont:=createfont(8,0,0,0,0,0,0,0,GB2312_CHARSET,0,0,0,0,'宋体');
RectRepaint.Left:=0;
RectRepaint.Top:=0;
RectRepaint.Right:=260;
RectRepaint.Bottom:=40;
DC:=CreateDc('DISPLAY',nil,nil,nil);
Hbrush:=CreateSolidBrush(getsyscolor(Color_infoBk));
selectobject(dc,Hbrush);
ScreenWidth:=GetDeviceCaps(DC,HORZRES);
ScreenHeight:=GetDeviceCaps(DC,VERTRES);
MyHdc:=GetDC(Hwnd1);
setbkcolor(myhdc,getsyscolor(Color_infoBk));
DrawIcon(MyHdc,WindowWidth-40,0,loadicon(Hinstance,'MainIcon'));
hHook:=SetwindowsHookEx(WH_JOURNALRECORD,HookProc,HInstance,0);
while GetMessage(MSG, 0, 0, 0) do begin
TranslateMessage(MSG);
DispatchMessage(MSG);
if msg.message=WM_LBUTTONDOWN then
begin
ScreenToClient(hwnd1,msg.pt);
if ((msg.pt.X >windowwidth-40) and (msg.pt.X<windowwidth) and (msg.pt.y>0) and (msg.pt.Y<32)) then
begin
ClientToScreen(hwnd1,msg.pt);
sendmessage(hwnd1,WM_DESTROY,0,0);
break;
end else
begin
ReleaseCapture;
SendMessage(hwnd1,wm_syscommand,sc_dragmove,0);
end;
ClientToScreen(hwnd1,msg.pt);
end;
end;
UnHookWindowsHookEx(hHook);
DeleteDc(DC);
Deletedc(myHdc);
Deleteobject(Hbrush);
Halt(MSG.wParam);
end.
function ColorToHTMLHex(Color: TColor): string;
begin
Result := IntToHex(ColorToRGB(Color),6);
Result := Copy(Result,5,2)+Copy(Result,3,2)+Copy(Result,1,2);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
p:TPoint;
h:HWND;
s:array [0..255] of char;
DC: HDC;
clr: COLORREF;
begin
GetCursorPos(p);
//Get Class Name
h:=WindowFromPoint(p);
GetClassName(h,s,255);
labHandle.Caption:=format('0x%x',[h]);
labclassname.Caption:=s;
h:=GetParent(h);
GetClassName(h,s,255);
labParent.Caption:=s;
可以不用Timer,而用全局鼠标钩子。
通过 if (wParam = WM_MOUSEMOVE) then 判断鼠标位置
通过 if (wParam = WM_LBUTTONDOWN) then 判断左键单击
参考:http://service.lonetear.com/delphi/dispdoc.asp?id=1300