在很多软件中(比如:WPS OFFICE),当鼠标移动时,会有一个以鼠标所在点为原点的虚线的坐标系来表明当前鼠标的位置,这两条线是怎样画的?希望高手们帮帮我啊!

解决方案 »

  1.   

    { 以下是主程序 unit1.pas }
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls;type
      TForm1 = class(TForm)
        PaintBox1: TPaintBox;
        procedure FormCreate(Sender: TObject);
        procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
        procedure FormResize(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses Unit2;var myline:TMouseLine;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
    myline:=TMouseLine.Create (self,self.PaintBox1.Canvas);
        myline.ClientWidth := self.Width;
        myline.ClientHeight := self.Height;
    end;procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
    myline.ReLine (x,y);
    end;procedure TForm1.FormResize(Sender: TObject);
    begin
    myline.ClientWidth := self.Width;
        myline.ClientHeight := self.Height;
    end;end.{ 以下是自定义的类 TMouseLine 的单元 unit2.pas }
    unit Unit2;interfaceuses Graphics, Controls, Classes;type TMouseLine = Class (TGraphicControl)
    private
         ox,oy:integer;
            FCanvas:TCanvas;
            FWidth, FHeight:integer;
        public
         procedure ReLine (x,y:Integer);
            constructor Create (AOwner:TComponent;Canvas:TCanvas);
            destructor Destroy; override;
        published
        property ClientWidth: integer read FWidth write FWidth;
         property ClientHeight: Integer read FHeight write FHeight;
    end;implementationconstructor TMouseLine.Create(AOwner:TComponent;Canvas:TCanvas);
    begin
    inherited Create(AOwner);
        FCanvas:=Canvas;
        FWidth:=0;
        FHeight:=0;
        ox:=0;oy:=0;
    end;destructor TMouseLine.Destroy;
    begin
    inherited Destroy;
    end;procedure TMouseLine.ReLine(x,y:Integer);
    begin
    with FCanvas do begin
         pen.Color := clBtnFace;
            pen.Width := 1;
            MoveTo (ox,0);
            LineTo (ox,FHeight);
            MoveTo (0,oy);
            LineTo (FWidth,oy);     pen.Color := clBlack;
            pen.Width := 1;
            MoveTo (x,0);
            LineTo (x,FHeight);
            MoveTo (0,y);
            LineTo (FWidth,y);
        end;
        ox:=x;oy:=y;
    end;end.
    刚才我把问题想复杂了,说要用线程,其实没有必要,不好意思.
      

  2.   

    我试了试,觉得要是在Reline的开头加上pen.Mode:=pmNot,就可以在任何背景下画线了。但是,不知道为什么,运行时老是有最初的一个坐标系不能被还原,还望多多指教!