我的程序CPU占用率达30%!!!谁能帮忙改改?100分!(100分)

  • 主题发起人 主题发起人 iCANK
  • 开始时间 开始时间
I

iCANK

Unregistered / Unconfirmed
GUEST, unregistred user!
是个屏幕取色的程序。另外,我想加入捕捉鼠标点击和鼠标移动,鼠标点击然后禁止<br>Timer1,把当前颜色copy到剪贴板,这段代码这么写?查过一些资料,说必须要用<br>mouse hook?但我不懂hook啊,而且,好像hook要在dll里调用,我不想再带一个dll,<br>就一个exe多好!:)有没有不用hook的方法就可以捕捉鼠标点击?捕捉鼠标移动是如果<br>鼠标没有在移动,就禁止Timer1,这样可能CPU占用率要小一些!<br><br>源程序:<br>unit ColorSpy_betaP;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; ExtCtrls, StdCtrls, Buttons, regions, VCLBase, EPLabels, epMorph;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Timer1: TTimer;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; Label3: TLabel;<br>&nbsp; &nbsp; DisColor: TShape;<br>&nbsp; &nbsp; epRegionForm1: TepRegionForm;<br>&nbsp; &nbsp; WndCaption: TepLabel;<br>&nbsp; &nbsp; CloseBtn: TepMorphButton;<br>&nbsp; &nbsp; Timer2: TTimer;<br>&nbsp; &nbsp; procedure Timer1Timer(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure CloseBtnClick(Sender: TObject);<br>&nbsp; &nbsp; procedure Timer2Timer(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var<br>&nbsp; MousePos: TPoint; &nbsp; &nbsp;//鼠标位置指针<br>&nbsp; MousePosX,MousePosY: string; &nbsp; //鼠标位置X轴,鼠标位置Y轴<br>&nbsp; MousePosColor,MousePosColorRed,MousePosColorGreen,MousePosColorBlue: longint; &nbsp; //鼠标位置颜色<br>&nbsp; MousePosColorRedH,MousePosColorGreenH,MousePosColorBlueH: string; &nbsp; &nbsp; &nbsp;//颜色16进制<br>begin<br>&nbsp; GetCursorPos(MousePos); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //得到鼠标坐标<br>&nbsp; MousePosX := IntToStr(MousePos.x);<br>&nbsp; MousePosY := IntToStr(MousePos.y);<br>&nbsp; Label1.Caption := Format('鼠标位置:[ %s,%s ]',[MousePosX,MousePosY]);<br>&nbsp; MousePosColor := GetPixel(GetDC(0),MousePos.x,MousePos.y); &nbsp; &nbsp;//得到颜色。<br>&nbsp; MousePosColorRed := MousePosColor and $FF; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//分离出红色<br>&nbsp; MousePosColorGreen := (MousePosColor and 65280) div 256; &nbsp; &nbsp; &nbsp;//分离出绿色<br>&nbsp; MousePosColorBlue := (MousePosColor and $FF0000) div 65536; &nbsp; //分离出蓝色<br>&nbsp; Label2.Caption := Format('RGB 颜色:[ %d,%d,%d ]',[MousePosColorRed,MousePosColorGreen,MousePosColorBlue]);<br>&nbsp; //方块显示颜色<br>&nbsp; DisColor.Brush.Color := RGB(MousePosColorRed,MousePosColorGreen,MousePosColorBlue);<br>&nbsp; //网页颜色16进制,不足8位的补齐8位。<br>&nbsp; MousePosColorRedH := Format('%x',[MousePosColorRed]);<br>&nbsp; if Length(MousePosColorRedH) = 1 then MousePosColorRedH := '0' + MousePosColorRedH;<br>&nbsp; MousePosColorGreenH := Format('%x',[MousePosColorGreen]);<br>&nbsp; if Length(MousePosColorGreenH) = 1 then MousePosColorGreenH := '0' + MousePosColorGreenH;<br>&nbsp; MousePosColorBlueH := Format('%x',[MousePosColorBlue]);<br>&nbsp; if Length(MousePosColorBlueH) = 1 then MousePosColorBlueH := '0' + MousePosColorBlueH;<br>&nbsp; Label3.Caption := Format('网页颜色:[ #%s%s%s ]',[MousePosColorRedH,MousePosColorGreenH,MousePosColorBlueH]);<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);<br>&nbsp; Form1.Width := 178;<br>&nbsp; Form1.Height := 16;<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp; Application.MessageBox('这是icank的软件!!!:P','提醒!:-)',MB_ICONINFORMATION+MB_OK);<br>end;<br><br>procedure TForm1.CloseBtnClick(Sender: TObject);<br>begin<br>&nbsp; Close();<br>end;<br><br>procedure TForm1.Timer2Timer(Sender: TObject);<br>const<br>&nbsp; WndCreateSpeed = 10; &nbsp; &nbsp; &nbsp; //定义窗口打开和关闭时的步长<br>begin<br>&nbsp; Timer2.Enabled := True;<br>&nbsp; with Form1 do<br>&nbsp; &nbsp; Width := Width + WndCreateSpeed;<br>&nbsp; &nbsp; Height := Height + WndCreateSpeed;<br>&nbsp; &nbsp; Top := Screen.Height div 2 - Height div 2;<br>&nbsp; &nbsp; Left := Screen.Width div 2 - Width div 2;<br>&nbsp; &nbsp; if Height &gt; 76 then Timer2.Enabled := False;<br>end;<br><br>end.<br>
 
我试了,不到3%,有时候只有1%。<br>我的环境:D5+W2K<br>下面的过程我没让它执行<br>FormCreate<br>Timer2Timer
 
后退
顶部