⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mapwindowpointsu.pas

📁 Delphi Win32核心API参考光盘源码 本书包含了常用的Windows API函数
💻 PAS
字号:
unit MapWindowPointsU;

interface

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

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    PaintBox1: TPaintBox;
    Label2: TLabel;
    Label3: TLabel;
    procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  DrawnRect: TRect;       // holds the rectangular coordinates
  Drawing: Boolean;       // indicates if a rectangle is being drawn

implementation

{$R *.DFM}

procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  {indicate that a drawing operation has commenced, and initialize the
   rectangular coordinates}
  Drawing := TRUE;
  DrawnRect := Rect(X, Y, X, Y);
end;

procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  {if we a redrawing...}
  if Drawing then
  with PaintBox1.Canvas do
  begin
    {initialize the canvas's pen and brush}
    Pen.Mode := pmNot;
    Pen.Width := 2;
    Brush.Style := bsClear;

    {draw a rectangle over the previous one to erase it}
    Rectangle(DrawnRect.Left, DrawnRect.Top, DrawnRect.Right, DrawnRect.Bottom);

    {set the rectangle to the current coordinates}
    DrawnRect := Rect(DrawnRect.Left, DrawnRect.Top, X, Y);

    {draw the new rectangle}
    Rectangle(DrawnRect.Left, DrawnRect.Top, DrawnRect.Right, DrawnRect.Bottom);
  end;
end;

procedure TForm1.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  {we are no longer drawing}
  Drawing := FALSE;

  {display the coordinates relative to the panel}
  Label2.Caption := 'Panel coordinates - L:'+IntToStr(DrawnRect.Left)+', T: '+
                    IntToStr(DrawnRect.Top)+', R: '+IntToStr(DrawnRect.Right)+
                    ', B: '+IntToStr(DrawnRect.Bottom);

  {translate the rectangular coordinates relative to the form}
  MapWindowPoints(Panel1.Handle, Form1.Handle, DrawnRect, 2);

  {display the coordinates relative to the form}
  Label3.Caption := 'Form coordinates - L:'+IntToStr(DrawnRect.Left)+', T: '+
                    IntToStr(DrawnRect.Top)+', R: '+IntToStr(DrawnRect.Right)+
                    ', B: '+IntToStr(DrawnRect.Bottom);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -