Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Mouseposition innerhalb des Forms bestimmen (https://www.delphipraxis.net/86061-mouseposition-innerhalb-des-forms-bestimmen.html)

skyquaker 8. Feb 2007 14:32


Mouseposition innerhalb des Forms bestimmen
 
Ich würde gerne die Mausposition innerhalb meines Programms bestimmen und setzen.(d.h., dass der erste Pixel meins Forms auch gleichzeitig Mouse.Position X/Y = 1/1 ist)

Außerdem möchte ich dasselbe mit einem gerade laufenden Programm machen.

Möglich?

:dp:

bitsetter 8. Feb 2007 18:00

Re: Mouseposition innerhalb des Forms bestimmen
 
Wenn du das Handle des fremden Fensters kennst, müsste folgendes funktionieren.
Delphi-Quellcode:
function GetPos(Handle: HWND): TPoint;
var
   WinInfo: TWindowInfo;
   CurPos: TPoint;
begin
   Result.X:= -1;
   Result.Y:= -1;
   FillChar(WinInfo, Sizeof(WinInfo), 0);
   WinInfo.cbSize := Sizeof(WinInfo);
   GetWindowInfo(Handle, WinInfo);
   GetCursorPos(CurPos);
   //if windows.PtInRect(WinInfo.rcWindow, CurPos) then
   if windows.PtInRect(WinInfo.rcClient, CurPos) then
     begin
       Result.X:= CurPos.X- WinInfo.rcClient.Left;
       Result.Y:= CurPos.Y- WinInfo.rcClient.Top;
     end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  CurPos: TPoint;
begin
  CurPos:= GetPos(Form1.Handle);
  Caption:= 'X: '+inttostr(CurPos.X)+ ' Y: '+inttostr(CurPos.Y);
end;

procedure SetPos(Handle: HWND; CurPos: TPoint);
var
   WinInfo: TWindowInfo;
begin
   FillChar(WinInfo, Sizeof(WinInfo), 0);
   WinInfo.cbSize := Sizeof(WinInfo);
   GetWindowInfo(Handle, WinInfo);
   inc(CurPos.X, WinInfo.rcClient.Left);//WinInfo.rcWindow.Left
   inc(CurPos.Y, WinInfo.rcClient.Top);//WinInfo.rcWindow.Top
   if windows.PtInRect(WinInfo.rcClient, CurPos) then
   setCursorPos(CurPos.X, CurPos.Y);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  CurPos: TPoint;
begin
  CurPos.X:= 5;
  CurPos.Y:= 15;
  SetPos(Form1.Handle, CurPos);
end;
Ich hoffe mal, du meintest es auch so.

skyquaker 8. Feb 2007 18:19

Re: Mouseposition innerhalb des Forms bestimmen
 
Zitat:

Zitat von bitsetter
Wenn du das Handle des fremden Fensters kennst, müsste folgendes funktionieren.
Delphi-Quellcode:
function GetPos(Handle: HWND): TPoint;
var
   WinInfo: TWindowInfo;
   CurPos: TPoint;
begin
   Result.X:= -1;
   Result.Y:= -1;
   FillChar(WinInfo, Sizeof(WinInfo), 0);
   WinInfo.cbSize := Sizeof(WinInfo);
   GetWindowInfo(Handle, WinInfo);
   GetCursorPos(CurPos);
   //if windows.PtInRect(WinInfo.rcWindow, CurPos) then
   if windows.PtInRect(WinInfo.rcClient, CurPos) then
     begin
       Result.X:= CurPos.X- WinInfo.rcClient.Left;
       Result.Y:= CurPos.Y- WinInfo.rcClient.Top;
     end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  CurPos: TPoint;
begin
  CurPos:= GetPos(Form1.Handle);
  Caption:= 'X: '+inttostr(CurPos.X)+ ' Y: '+inttostr(CurPos.Y);
end;

procedure SetPos(Handle: HWND; CurPos: TPoint);
var
   WinInfo: TWindowInfo;
begin
   FillChar(WinInfo, Sizeof(WinInfo), 0);
   WinInfo.cbSize := Sizeof(WinInfo);
   GetWindowInfo(Handle, WinInfo);
   inc(CurPos.X, WinInfo.rcClient.Left);//WinInfo.rcWindow.Left
   inc(CurPos.Y, WinInfo.rcClient.Top);//WinInfo.rcWindow.Top
   if windows.PtInRect(WinInfo.rcClient, CurPos) then
   setCursorPos(CurPos.X, CurPos.Y);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  CurPos: TPoint;
begin
  CurPos.X:= 5;
  CurPos.Y:= 15;
  SetPos(Form1.Handle, CurPos);
end;
Ich hoffe mal, du meintest es auch so.

Ja, das sieht stark nach dem aus, was ich suche. Kann mir jemand sagen, wie ich das Hangle des Fensters auslese? :thumb:

bitsetter 8. Feb 2007 18:58

Re: Mouseposition innerhalb des Forms bestimmen
 
Suche mal nach folgenden Begriffen:
FindWindow
EnumWindows
WindowFromPoint


Oder du kannst das Handle auch mit einem Programm herausfinden z.B. xSpy:
http://www.x-spy.net

skyquaker 9. Feb 2007 17:15

Re: Mouseposition innerhalb des Forms bestimmen
 
Ist es möglich dasselbe wie im Code dort oben nur statt mit dem Hanle mit dem WindowsCaption zu machen?

DGL-luke 9. Feb 2007 17:19

Re: Mouseposition innerhalb des Forms bestimmen
 
Jop, indem du das Handle mithilfe von Findwindow rausfindest.

skyquaker 9. Feb 2007 17:47

Re: Mouseposition innerhalb des Forms bestimmen
 
Zitat:

Zitat von DGL-luke
Jop, indem du das Handle mithilfe von Findwindow rausfindest.

Thx, das habe ich auchgerade rausgefunden.

skyquaker 10. Feb 2007 13:23

Re: Mouseposition innerhalb des Forms bestimmen
 
Das mit der Mausposition geht sehr gut, nur leider krieg ich es nicht hin das übers Netzwerk zu übertragen.

So hab ichs gemacht:

Server:
Delphi-Quellcode:
function GetPos(Handle: HWND): TPoint;
var
   WinInfo: TWindowInfo;
   CurPos: TPoint;
begin
   Result.X:= -1;
   Result.Y:= -1;
   FillChar(WinInfo, Sizeof(WinInfo), 0);
   WinInfo.cbSize := Sizeof(WinInfo);
   GetWindowInfo(Handle, WinInfo);
   GetCursorPos(CurPos);
   //if windows.PtInRect(WinInfo.rcWindow, CurPos) then
   if windows.PtInRect(WinInfo.rcClient, CurPos) then
     begin
       Result.X:= CurPos.X- WinInfo.rcClient.Left;
       Result.Y:= CurPos.Y- WinInfo.rcClient.Top;
     end;
end;
.
.
.
.

procedure TForm1.Server2Execute(AThread: TIdPeerThread);
var mp: TPoint;
begin
mp:= getpos(Form1.Handle);
   with Athread.Connection do
     begin
    WriteInteger(mp.x);
    WriteInteger(mp.y);
   Disconnect;
     end;
end;
Client:
Delphi-Quellcode:
with Client2 do
       begin
          Connect;
            try
               xy.X := ReadInteger(true);
               xy.Y := ReadInteger(true);
            finally
          Disconnect;
         end;
    SetPos(Han,xy);
ShowMEssage(strtoint(xy.X)+'/'+strtoint(xy.y));


procedure SetPos(Handle: HWND; CurPos: TPoint);
var
   WinInfo: TWindowInfo;
begin
   FillChar(WinInfo, Sizeof(WinInfo), 0);
   WinInfo.cbSize := Sizeof(WinInfo);
   GetWindowInfo(Handle, WinInfo);
   inc(CurPos.X, WinInfo.rcClient.Left);//WinInfo.rcWindow.Left
   inc(CurPos.Y, WinInfo.rcClient.Top);//WinInfo.rcWindow.Top
   if windows.PtInRect(WinInfo.rcClient, CurPos) then
   setCursorPos(CurPos.X, CurPos.Y);
end;
Das Problem ist, das es keine Message anzeigt und auch nicht die Mausposition verändert. (Ich kriege aber auch keine Fehler)

skyquaker 10. Feb 2007 16:40

Re: Mouseposition innerhalb des Forms bestimmen
 
Inzwischen gibt er mir eine Message aus, aber leider nur eine bei der X= 1 ist und Y irgendeine sechsstellige Zahl.


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:17 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz