unit kbKernel;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  pRGBTripleArray = ^TRGBTripleArray;
  TRGBTripleArray = ARRAY[0..32767] OF TRGBTriple;
  TRGBTriple =
  PACKED RECORD
    rgbtBlue : BYTE;
    rgbtGreen: BYTE;
    rgbtRed : BYTE;
  END;

procedure CloseWindows;
  procedure LeftClick(x, y: integer);
  procedure RightClick(x, y: integer);
  procedure DoubleClick(x, y: integer);
  procedure MoveTo(x, y: integer);
  procedure Presskey(vk: integer);
  procedure PressTwoKey(key1, key2: integer);
  function GetPixelColor(x, y: integer): integer;
  function Findcolor(iLeft, iTop, iRight, iBottom, Acolor: integer;
    var iX, iY: integer):boolean;
  function Findpicture(iLeft, iTop, iRight, iBottom: integer;
    strPic: string; var iX, iY: integer):boolean;
  procedure inputNum(num:integer);
  function GetXY(var x, y: integer): boolean;

implementation

procedure CloseWindows();
var
 hdlProcessHandle : Cardinal;
 hdlTokenHandle : Cardinal;
 tmpLuid : Int64;
 //tkpPrivilegeCount : Int64;
 tkp : TOKEN_PRIVILEGES;
 tkpNewButIgnored : TOKEN_PRIVILEGES;
 lBufferNeeded : Cardinal;
 Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;
begin
  hdlProcessHandle := GetCurrentProcess;
  OpenProcessToken(hdlProcessHandle,
                  (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),
                   hdlTokenHandle);

// Get the LUID for shutdown privilege.
  LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
  Privilege[0].Luid := tmpLuid;
  Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
  tkp.PrivilegeCount := 1;   // One privilege to set
  tkp.Privileges[0] := Privilege[0];
  // Enable the shutdown privilege in the access token of this
  // process.
  AdjustTokenPrivileges(hdlTokenHandle,
                        False,
                        tkp,
                        Sizeof(tkpNewButIgnored),
                        tkpNewButIgnored,
                        lBufferNeeded);
  ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE), $FFFF);
end;

//点击鼠标左键
procedure LeftClick(x,y:integer);
begin
  mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTDOWN,round(x*65535/1024),round(y*65535/768),0,0);
  sleep(20);
  mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP,round(x*65535/1024),round(y*65535/768),0,0);
end;

//点击鼠标右键
procedure RightClick(x,y:integer);
begin
  mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_RIGHTDOWN,round(x*65535/1024),round(y*65535/768),0,0);
  sleep(20);
  mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_RIGHTUP,round(x*65535/1024),round(y*65535/768),0,0);
end;

//双击鼠标左键
procedure DoubleClick(x,y:integer);
begin
  LeftClick(x,y);
  sleep(50);
  LeftClick(x,y);
end;

//移动鼠标到指定位置
procedure MoveTo(x,y:integer);
begin
  mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_MOVE,round(x*65535/1024),round(y*65535/768),0,0);
end;

//按下一个键
procedure Presskey(vk:integer);
begin
  keybd_event(vk,MapVirtualKey(vk, 0),0,0);
  sleep(20);
  keybd_event(vk,MapVirtualKey(vk, 0),KEYEVENTF_KEYUP,0);
end;

Procedure PressTwoKey(key1,key2:integer);
begin
  keybd_event(key1,MapVirtualKey(key1, 0),0,0);
  sleep(50);
  PressKey(key2);
  sleep(50);
  keybd_event(key1,MapVirtualKey(key1, 0),KEYEVENTF_KEYUP,0);
end;

//得到屏幕上的某点的颜色
function GetPixelColor(x,y:integer):integer;
var
  aDc:HDC;
begin
  aDc:=getdc(0);
  result:=getpixel(aDc,x,y);
  releasedc(0,aDc);
end;

//在指定范围内查找一个点,找到返回TRUE,失败为FALSE
function findcolor(iLeft,iTop,iRight,iBottom,Acolor:integer;var iX,iY:integer):boolean;
var
  aDc:HDC;
  i,j:integer;
  bitmap:Tbitmap;
  row,row1 : pRGBTripleArray;
  ScanlineBytes: INTEGER;
begin
  iX := -1;
  iY := -1;

bitmap:= Tbitmap.Create;
  bitmap.PixelFormat := pf24bit;
  bitmap.Width := iRight-iLeft+1;
  bitmap.Height := iBottom-iTop+1;
  aDc:= getdc(0);
  bitblt(bitmap.Canvas.Handle,0,0,bitmap.Width-1,bitmap.Height-1,aDc,iLeft,iTop,srccopy);
  releasedc(0,aDc);
  row := Bitmap.Scanline[0];
  row1 := Bitmap.Scanline[1];
  ScanlineBytes := Integer(row1) - Integer(row);
  for j := 0 to Bitmap.Height-1 do
  begin
    for i := 0 to Bitmap.Width-1 do
    begin
      if (row[i].rgbtRed=getRvalue(Acolor))and(row[i].rgbtGreen=getGvalue(Acolor))and(row[i].rgbtBlue=getBvalue(Acolor))then
      begin
        iX:= i;
        iY:= j;
        break;
      end;
    end;
    if iX<>-1 then break;
    inc(Integer(Row), ScanlineBytes);
  end;
  result := iX<>-1;
  bitmap.Free;
end;

function findpicture(iLeft,iTop,iRight,iBottom:integer;strPic:string;var iX,iY:integer):boolean;
var
  aDc:HDc;
  bitmap,bitmap1:Tbitmap;
  arrPoint:array[1..20] of Tpoint;
  iColors:array[1..20] of integer;
  i,j,k,x,y:integer;
  finded:boolean;

row,row1 : pRGBTripleArray;
  ScanlineBytes: INTEGER;
begin
  iX := -1;
  iY := -1;
  result:=false;
  if not fileexists(strPic) then exit;//文件不存在,则退出;
  bitmap1 := Tbitmap.Create;
  bitmap1.LoadFromFile(strPic);
  for i:= 1 to 20 do
  begin
    repeat
      x := random(bitmap1.Width);
      y := random(bitmap1.Height);
      while bitmap1.Canvas.Pixels[x,y]= 16777215 do
      begin
        x := random(bitmap1.Width);
        y := random(bitmap1.Height);
      end;

finded:= false;
      for j:= i-1 downto 1 do
      begin
        if (arrPoint[j].X = x) and (arrPoint[j].Y = y) then
        begin
          finded := true;
          break;
        end;
      end;
    until not finded;
    arrPoint[i].X := x;
    arrPoint[i].Y := y;
    iColors[i] := bitmap1.Canvas.Pixels[arrPoint[i].X,arrPoint[i].Y];
  end;

bitmap:= Tbitmap.Create;
  bitmap.PixelFormat := pf24bit;
  bitmap.Width := iRight-iLeft+1;
  bitmap.Height := iBottom-iTop+1;
  aDc:= getdc(0);
  bitblt(bitmap.Canvas.Handle,0,0,bitmap.Width-1,bitmap.Height-1,aDc,iLeft,iTop,srccopy);
  releasedc(0,aDc);
  row := Bitmap.Scanline[0];
  row1 := Bitmap.Scanline[1];
  ScanlineBytes := Integer(row1) - Integer(row);

for j := 0 to Bitmap.Height-bitmap1.Height do
  begin
    for i := 0 to Bitmap.Width-bitmap1.Width do
    begin
      finded := true;
      for k:= 1 to 20 do
      begin
        integer(row1):= integer(row)+ arrPoint[k].Y*ScanlineBytes;
        if (abs(row1[i+arrPoint[k].X].rgbtRed-getRvalue(iColors[k]))>255*(1-0.9))or(abs(row1[i+arrPoint[k].X].rgbtGreen-getGvalue(iColors[k]))>255*(1-0.9))or(abs(row1[i+arrPoint[k].X].rgbtBlue-getBvalue(iColors[k]))>255*(1-0.9))then
        begin
          finded := false;
          break;
        end;
      end;
      if finded then
      begin
        iX := iLeft+i+round(bitmap1.Width/2);
        iY := iTop+j+round(bitmap1.Height/2);
        break;
      end;
    end;
    if iX<>-1 then break;
    inc(Integer(Row), ScanlineBytes);
  end;
  result:=iX<>-1;
  bitmap.Free;
  bitmap1.Free;
end;

procedure inputNum(num:integer);
var
  i:integer;
  aStr:string;
begin
  aStr:= inttostr(num);
  for i:=1 to length(aStr) do
  begin
    PressKey(ord(aStr[i]));
    sleep(50);
  end;
  PressKey(VK_RETURN);
  sleep(50);
end;

//读取当前游戏坐标
function GetXY(var x, y: integer): boolean;
var
 aDc:HDC;
 i,j,newX,TZCount:integer;
 PXCount:integer;//象素数量
 EmptyLine,EmptyContent,negtiveX,negtiveY,XorY:boolean;

bitmap:Tbitmap;
 row,row1 : pRGBTripleArray;
 ScanlineBytes: INTEGER;
begin
  bitmap:= Tbitmap.Create;
  bitmap.PixelFormat := pf24bit;
  bitmap.Width := 950-870+1;
  bitmap.Height := 34-22+1;
  aDc:= getdc(0);
  bitblt(bitmap.Canvas.Handle,0,0,bitmap.Width-1,bitmap.Height-1,aDc,870,22,srccopy);
  releasedc(0,aDc);
  row := Bitmap.Scanline[0];
  row1 := Bitmap.Scanline[1];
  ScanlineBytes := Integer(row1) - Integer(row);

EmptyContent:=true;
  EmptyLine:=true;
  TZCount:=0;//累加一个数字的所有想素的坐标总和
  PXCount:= 0;
  negtiveX:=false;//x坐标负数标记
  negtiveY:=false;//y坐标负数标记
  XorY:=false;//表示扫描X还是Y,先扫描X
  newX:=0;//该语句多余,仅仅为了不产生警告错误而已
  result:=false;
  x:=0;
  y:=0;

for i:=0 to bitmap.Width-1 do
  begin
    if (not EmptyContent)and(EmptyLine) then
    begin
      case TZCount of
        111:;//      [ 125
        127: result:=true;//      ] 141
        11: XorY:=true;//      . 12  开始扫描  Y
        45: if XorY then negtiveY:=true else negtiveX:=true;//      - 50
        171:if XorY then y:=y*10 else x:=x*10;//      0 189
        97:if XorY then y:=y*10+1 else x:=x*10+1;//      1 108
        153:if XorY then y:=y*10+2 else x:=x*10+2;//      2 169
        149:if XorY then y:=y*10+3 else x:=x*10+3;//      3 164
        163:if PXCount = 16 then
              if XorY then y:=y*10+4 else x:=x*10+4//      4 179
            else
              if XorY then y:=y*10+5 else x:=x*10+5;//      5 181
        176:if XorY then y:=y*10+6 else x:=x*10+6;//      6 195
        107:if XorY then y:=y*10+7 else x:=x*10+7;//      7 120
        180:if XorY then y:=y*10+8 else x:=x*10+8;//      8 199
        185:if XorY then y:=y*10+9 else x:=x*10+9;//      9 204
      end;

EmptyContent:=true;
      TZCount:=0;
      PXCount:= 0;
    end;
    EmptyLine:=true;
    for j:=0 to bitmap.Height-1 do
    begin
      integer(row1) :=integer(row)+j*ScanlineBytes;
      if (abs(row1[i].rgbtRed-getRvalue(198))<20)and(abs(row1[i].rgbtGreen-getGvalue(198))<20)and(abs(row1[i].rgbtBlue-getBvalue(198))<20)then
      begin
        if EmptyContent then newX := i; //保存新数字的开始横坐标
        TZCount:=TZCount+i-newX+1+j+1; //把像素点的横纵坐标累加
        inc(PXCount);
        EmptyContent:=false;
        EmptyLine:=false;
      end;
    end;
  end;
  if negtiveX then x:=0-x;
  if negtiveY then y:=0-y;
  bitmap.Free;
end;

end.

delphi實現按键精靈的功能的更多相关文章

  1. Jquery scrollTop animate 實現動態滾動到頁面頂部

    這個方法之前都是用的錨點實現的,但是效果僵硬,動感不足! 之後參考了一些網站,發現都是用的js,於是自己想到用jquery 來做一個插件也來實現以下這個小功能. $.fn.backTop = func ...

  2. ASP.NET MVC 4.0 学习2-留言板實現

    新增專案實現留言板功能,瞭解MVC的運行機制 1,新增專案   2,添加數據庫文件message.mdf   Ctrl+W,L 打開資料庫連接,添加存放留言的Atricle表 添加字段,後點擊&quo ...

  3. Neo4j中實現自定義中文全文索引

    資料庫檢索效率時,一般首要優化途徑是從索引入手,然後根據需求再考慮更復雜的負載均衡.讀寫分離和分散式水平/垂直分庫/表等手段:索引通過資訊冗餘來提高檢索效率,其以空間換時間並會降低資料寫入的效率,因此 ...

  4. linux下c++實現簡單的生產者消費者隊列模式

    引言 生產者消費者是一個經典的模式 利用生產者,消費者和緩衝區降低了生產者和消費者之間的的耦合度 便於對生產者和消費者的修改 下面記錄的是一個經典的單一生產者多消費者的模式 設計思路 以隊列做為緩衝區 ...

  5. 在Android中afinal框架下實現sqlite數據庫版本升級的辦法

    public abstract void onUpgrade(SQLiteDatabase db,int oldVersion,int new Version) 這個方法在實現時需要重寫.   pub ...

  6. [實現DDD] 第10章 聚合(1)設計原則

    聚合只是將一些實體(Entity)與值對象(Value Object)聚集起來的對象樹嗎?? 有些途徑可能使我們設計出不正確的聚合模型, 如:可能為了對象組合上的方便而將聚合設計的很大;也可能設計的聚 ...

  7. 開玩樹莓派(二):配置IP,實現無顯示器局域網內Putty連接和RDP遠程

    目錄: 開玩樹莓派(一):安裝Raspbian系統 開玩樹莓派(二):配置IP,實現無顯示器局域網內Putty連接和RDP遠程 開玩樹莓派(三):Python編程 開玩樹莓派(四):GPIO控制和遠程 ...

  8. C# 實現文件壓縮-- 背景:服務器Log.txt 過多,佔用過多硬盤空間,壓縮備份后節省空間資源

    1.壓縮實現代碼如下: 調用ICSharpCode.SharpZipLib.dll(free software,可以搜到源碼). 轉移指定目錄文件夾轉移到目標文件夾 壓縮目標文件夾 刪除目標文件夾 u ...

  9. css 實現微信聊天類似的氣泡

    要實現這樣的效果 代碼如下: --------------------------------------- <style> .test{width:300px; padding:30px ...

随机推荐

  1. PhpSpreadsheet生成Excel时实现单元格自动换行

    PhpSpreadsheet是PHPExcel的替代版本,PHPExcel的作者已经停止了更新,今天尝试了使用PhpSpreadsheet生成Excel的时候支持单元格内的自动换行,发现用法其实差不多 ...

  2. With Visual Studio, Open Same File In Two Windows, Updates Reflected in Both

    I’ve always been frustrated in Visual Studio (all versions I can remember including latest vs2012) w ...

  3. 机器学习简史brief history of machine learning

    BRIEF HISTORY OF MACHINE LEARNING My subjective ML timeline (click for larger) Since the initial sta ...

  4. 操作系统的启动与引导问题 BIOS、UEFI、MBR、GPT

    关于ISO.WIM.GHO三者的正确理解. ISO(Isolation)文件一般以ISO为扩展名,是复制光盘上全部信息而形成的镜像文件. WIM是英文Microsoft Windows Imaging ...

  5. uva 10618 Tango Tango Insurrection 解题报告

    Tango Tango Insurrection Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebu ...

  6. Ubuntu 常用命令大全

    Ubuntu 常用命令大全查看软件 xxx 安装内容#dpkg -L xxx查找软件#apt-cache search 正则表达式查找文件属于哪个包#dpkg -S filename apt-file ...

  7. 【面试必读】一不注意就做错的五道JavaScript题目

    如果不会,可以存在DW中运行试一下哦~ 1.这段代码会输出什么? function Container( properties ) { var objthis = this; for ( var i  ...

  8. .Net垃圾收集机制—了解算法与代龄

    垃圾收集器在本质上就是负责跟踪所有对象被引用到的地方,关注对象不再被引用的情况,回收相应的内存.在.NET平台中同样如此,有效的提高.NET垃圾回收性能,能够提高程序执行效率. 其实垃圾收集并不是伴随 ...

  9. c# 使用 HttpWebRequest模拟登陆(附带验证码)

    在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等. 先说下流程 1.使用httpwebrequest先进入你要登录的 ...

  10. BNU Concentric Rings

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=16030 Concentric Rings   There are several different ...