• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphi的坐标变换(ClientToScreen等函数的应用)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

 

 注意一点的是,由于函数名 ClientToScreen,被控件的方法与API函数同名使用,所以造成在调用时delphi优先调用控件的ClientToScreen方法。如果只想调用API函数,那么可以用 windows.ClientToScreen。  源代码如下:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormClick(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  p rivate
    { P rivate declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
{$APPTYPE CONSOLE}

procedure TForm1.Button1Click(Sender: TObject);
var
  point1:TPoint;

begin
  point1:=Point(Button1.Top,Button1.Left);
  point1:=ClientToScreen(point1);
  Edit1.Text:='X:'+IntToStr(point1.x)+' '+
    'Y:'+IntToStr(point1.y);
  Edit2.Text:='X:'+IntToStr(Button1.Top)+' '+
    'Y:'+IntToStr(Button1.Left);


end;

procedure TForm1.Button2Click(Sender: TObject);
var
  point1:TPoint;
  point2:TPoint;
begin
  point1.x:= Button2.Top;
  point1.y:= Button2.Left;
  point2:=Button2.ClientToScreen(point1);
  Edit1.Text:='X:'+IntToStr(point2.x)+' '+
    'Y:'+IntToStr(point2.y);
  Edit2.Text:='X:'+IntToStr(Button2.Top)+' '+
    'Y:'+IntToStr(Button2.Left);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  hwnd1,hwnd2:THandle;
  p1:TPoint;
begin
  hwnd1:=FindWindow(nil,PChar('计算器'));
  if hwnd1=0 then WinExec('calc',SW_SHOW);
  while True do
  begin
     hwnd1:=FindWindow(nil,PChar('计算器'));
     if hwnd1<>0 then Break;
     Application.ProcessMessages;
  end;
  hwnd2:=FindWindowEx(hwnd1,0,nil,PChar('十六进制'));
  if hwnd2=0 then
  begin
    ShowMessage('先切换到科学计算器!');
    Exit;
  end;

  p1:=Point(0,0); { TODO -o猪悟能 : 注意point必须先初始化再使用 }
  Windows.ClientToScreen(hwnd2,p1);
  { TODO -o猪悟能 : 计算器窗口置前,点击十六进制单选钮 }
  SetForegroundWindow(hwnd2);
  mouse_event(MOUSEEVENTF_LEFTDOWN,
    p1.X,p1.Y,0,0);

  //Writeln(p1.x);
  //Writeln(p1.y);

end;

procedure TForm1.FormClick(Sender: TObject);
var
  p1:TPoint;
begin
  GetCursorPos(p1);
  Label5.Caption:='鼠标相对于屏幕位置 X:'+IntToStr(p1.x)+
    ' y:'+IntToStr(p1.y);
  
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y:
    Integer);
var
  p1:TPoint;
begin
  p1:=Point(x,y);
  Label3.Caption:= 'X:'+IntToStr(x)+' '+
    'Y:'+IntToStr(y);
  p1:=ClientToScreen(p1);
  Label4.Caption:= 'X:'+IntToStr(p1.x)+' '+
    'Y:'+IntToStr(p1.y);
end;

end.

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
DelphiDataTypes发布时间:2022-07-18
下一篇:
delphi高手突破之异常及错误处理发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap