我要实现鼠标在整个windows屏幕中(而不是应用程序窗口中)移动时所在的位置,
应该写什么事件才能实现呢?

解决方案 »

  1.   

    先取得鼠标在程序中的位置,然后再中这个函数:he ClientToScreen function converts the client coordinates of a specified point to screen coordinates. BOOL ClientToScreen(    HWND hWnd, // window handle for source coordinates 
        LPPOINT lpPoint  // pointer to structure containing screen coordinates  
       );
     
      

  2.   

    var 
      MPosTp : Tpoint; 
    begin 
      GetCursorPos(MPosTp); 
      MPosTp.x ... 
      MPosTp.y ... 
    end;
    或用hook
      

  3.   

    procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
      Caption := format('%d, %d', [mouse.CursorPos.x,mouse.CursorPos.y]);
      if mouse.IsDragging then
        caption := caption + 'Dragging';
    end; 
      

  4.   

    这里有源码http://202.207.125.98/home/pMain.dll/dlpage?softid=30
      

  5.   

    unit HookMain;interfaceuses
      Windows, Messages, Dialogs, SysUtils;type mydata=record
      data1:array [1..2] of DWORD;
      data2:TMOUSEHOOKSTRUCT;
    end;var
      hObject : THandle;
      pMem : Pointer;
      NextHook: HHook;
      procSaveExit: Pointer;function HookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; export;
    function OpenGetKeyHook(sender : HWND;MessageID : WORD) : BOOL; export;
    function CloseGetKeyHook: BOOL; export;
    function GetPublicP : Pointer;stdcall; export;
    procedure DLLMain(dwReason:DWord); far;
    procedure HookExit; far;
    implementation
    Procedure UnMapMem;  
    begin  
    if Assigned(pMem) then  
    begin  
    UnMapViewOfFile(pMem);  
    pMem := Nil  
    end;  
    end;Procedure MapMem;  
    begin  
    hObject := CreateFileMapping($FFFFFFFF,Nil,Page_ReadWrite,0,$FFFF,pChar('_IOBuffer'));
    if hObject = 0 then Raise Exception.Create('创建公用数据的Buffer不成功!');
    pMem := MapViewOfFile(hObject,FILE_MAP_WRITE,0,0,SizeOf(mydata));
    // 1 or SizeOf(DataBuf) ????  
    // 创建SizeOf(DataBuf)的数据区 
    if not Assigned(pMem) then  
    begin
    UnMapMem;  
    Raise Exception.Create('创建公用数据的映射关系不成功!');  
    end;  
    end;  
    Procedure DLLMain(dwReason:DWord); far;  
    begin  
    Case dwReason of  
    DLL_PROCESS_ATTACH :  
    begin  
    pMem := nil;  
    hObject := 0;
    MapMem; //以下的公有数据,如tHWND,tMessageID将直接使用本Buf.  
    end;  
    DLL_PROCESS_DETACH : UnMapMem;  
    DLL_THREAD_ATTACH,  
    DLL_THREAD_DETACH :; //缺省  
    end;
    end;  procedure HookExit; far;  
    begin
    CloseGetKeyHook;  
    ExitProc := procSaveExit; 
    end; function GetPublicP : Pointer;export; 
    begin //这里引出了公用数据区的指针,你可以在你的应用程序中自由操作它。但建议去掉此接口。
    Result := pMem; 
    end; function HookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM):  
    LRESULT; stdcall; export;
    begin 
    Result := 0; 
    If iCode < 0 
    Then Result := CallNextHookEx(NextHook, iCode, wParam, lParam); 
    // This is probably closer to what you would want to do... 
    case wparam of 
    WM_LBUTTONDOWN: 
    begin
    end; 
    WM_LBUTTONUP: 
    begin 
    end; 
    WM_LBUTTONDBLCLK: 
    begin 
    end; 
    WM_RBUTTONDOWN: 
    begin 
    messagebeep(1); 
    end;
    WM_RBUTTONUP: 
    begin 
    end; 
    WM_RBUTTONDBLCLK: 
    begin 
    end;
    WM_MBUTTONDOWN: 
    begin 
    end;
    WM_MBUTTONUP:
    begin 
    end; 
    WM_MBUTTONDBLCLK: 
    begin 
    end; 
    WM_NCMouseMove, WM_MOUSEMOVE: 
    begin 
    mydata(pmem^).data2:=pMOUSEHOOKSTRUCT(lparam)^; 
    // messagebeep(1); 
    //SendMessage(DataBuf(pMem^)[1],DataBuf(pMem^)[2],wParam,lParam ); 
    SendMessage(mydata(pMem^).data1[1],mydata(pMem^).data1[2],wParam,integer(@(mydata(pmem^).data2)) );
    end; 
    end; //发送消息 
    end; function OpenGetKeyHook(sender : HWND;MessageID : WORD) : BOOL; export 
    ;
    begin 
    Result := False;
    if NextHook <> 0 then Exit; //已经安装了本钩子 
    // DataBuf(pMem^)[1] := Sender; //填数据区
    // DataBuf(pMem^)[2] := MessageID; //填数据区 
    mydata(pmem^).data1[1]:=sender; 
    mydata(pmem^).data1[2]:=messageid; NextHook := SetWindowsHookEx(WH_mouse, HookHandler, HInstance, 0); 
    Result := NextHook <> 0;  
    end;  function CloseGetKeyHook: BOOL; export;  
    begin  
    if NextHook <> 0 then  
    begin  
    UnhookWindowshookEx(NextHook); //把钩子链链接到下一个钩子处理上.  
    NextHook := 0;  
    end;  
    Result := NextHook = 0;  
    end;
    end.生成GetKey.dll主程序调用
    unit demounit;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        uncapture: TButton;
        capture: TButton;
        exit: TButton;
        Panel1: TPanel;
        show: TLabel;
        Label1: TLabel;
        counter: TLabel;
        procedure captureClick(Sender: TObject);
        procedure uncaptureClick(Sender: TObject);
        procedure exitClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        procedure WndProc(var Message: TMessage); override;
      end;const
      MessageID = WM_User + 100;var
      Form1: TForm1;
      num : integer;implementation
    function OpenGetKeyHook(sender : HWND;MessageID : WORD) : BOOL; external 'GetKey.DLL';
    function CloseGetKeyHook: BOOL; external 'GetKey.DLL';
    {$R *.DFM}procedure TForm1.captureClick(Sender: TObject);
    begin
    //
    if OpenGetKeyHook(handle,MessageID) then
    //ShowMessage('开始记录...');
    show.caption:='开始记录...';
    num := 0;
    end;procedure TForm1.uncaptureClick(Sender: TObject);
    begin
    //
    if CloseGetKeyHook then //ShowMessage('结束记录...');
    show.caption:='结束记录...';
    end;procedure TForm1.exitClick(Sender: TObject);
    begin
      close;
    end;
    procedure TForm1.WndProc(var Message: TMessage);
    var x,y:integer;
    begin
    if Message.Msg = MessageID then
    begin
    // Panel1.Caption := IntToStr(Num);
    x:=PMouseHookStruct( message.lparam)^.pt.x ;
    y:=PMouseHookStruct( message.lparam)^.pt.y ;panel1.caption:='x='+inttostr(x)+' y='+inttostr(y);
    inc(Num);
    counter.Caption := IntToStr(Num);
    end
    else Inherited;
    end;
    end. 
      

  6.   

    如何确定系统桌面屏幕窗口的 HWND  hWnd, //  window  handle  for  source  coordinates
      

  7.   

    lwk_hlj: 不用按钮,只要鼠标移动即显示其位置
      

  8.   

    有必要这么麻烦吗?看看这里:mouse.CursorPos.X;  //屏幕中的横坐标
    mouse.CursorPos.Y;  //屏幕中的纵坐标
      

  9.   

    mouse.cursorpos.x还有。Y是没有用的
      

  10.   

    如果在编辑框出现的话要用edit1.text:=inttostr(mouse.cursorpos.x);
      

  11.   

    lwk_hlj: 你的太麻烦了,小弟看不懂,还是非常感谢你
      

  12.   

    goomoo:mouse.CursorPos.X;  //屏幕中的横坐标
    mouse.CursorPos.Y;  //屏幕中的纵坐标你的只能在你的应用程序窗口起作用,但到了windows状态栏中不起作用 。
      

  13.   

    goomoo:
        写在哪个事件呢?
      

  14.   

    anan8210:
       在屏幕上的位置也可以说是桌面上的位置