哪位高手指点一下!

解决方案 »

  1.   

    //
    //Mybmp: 用于保存图像
    //DrawCur: 是否抓鼠标
    //
    procedure GetCurrentScreen(var Mybmp: TBitmap; DrawCur: Boolean);
    var
      Cursorx, Cursory: integer;
      dc: hdc;
      Mycan: Tcanvas;
      R: TRect;
      DrawPos: TPoint;
      MyCursor: TIcon;
      hld: hwnd;
      Threadld: dword;
      mp: tpoint;
      pIconInfo: TIconInfo;
    begin
      Mybmp := Tbitmap.Create; //建立BMPMAP 
      Mycan := TCanvas.Create; //屏幕截取
      dc := GetWindowDC(0);//取屏幕DC
      Try
        Mycan.Handle := dc;
        R := Rect(0, 0, screen.Width, screen.Height);
        Mybmp.Width := R.Right;
        Mybmp.Height := R.Bottom;
        Mybmp.Canvas.CopyRect(R, Mycan, R);
      Finally
        ReleaseDC(0, DC);
      end;
      Mycan.Handle := 0;
      Mycan.Free;
      if DrawCur then {画上鼠标图象}
      begin
         GetCursorPos(DrawPos);//取鼠标位置
         MyCursor := TIcon.Create;
         GetCursorPos(mp);
         hld := WindowFromPoint(mp);
         Threadld := GetWindowThreadProcessId(hld, nil);
         AttachThreadInput(GetCurrentThreadId, Threadld, True);
         MyCursor.Handle := Getcursor();
         AttachThreadInput(GetCurrentThreadId, threadld, False);
         GetIconInfo(Mycursor.Handle, pIconInfo);
         cursorx := DrawPos.x - round(pIconInfo.xHotspot);
         cursory := DrawPos.y - round(pIconInfo.yHotspot);
         Mybmp.Canvas.Draw(cursorx, cursory, MyCursor); {画上鼠标}
         DeleteObject(pIconInfo.hbmColor);
         //GetIconInfo 使用时创建了两个bitmap对象. 需要手工释放这两个对象
         DeleteObject(pIconInfo.hbmMask);
         //否则,调用他后,他会创建一个bitmap,多次调用会产生多个,直至资源耗尽
         Mycursor.ReleaseHandle; //释放数组内存
         MyCursor.Free; //释放鼠标指针
      end;
    end;