• WinAPI: RectInRegion 判断矩形是否位于区域中


    本例效果图:


    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure FormPaint(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    const
      BoolStr: array[Boolean] of string = ('矩形不在区域中', '矩形在区域中');
    
    var
      Rgn: HRGN;
      Rect: TRect;
    
    procedure TForm1.FormCreate(Sender: TObject);
    const
      n = 50;
    var
      x1,y1,x2,y2: Integer;
    begin
      x1 := n;
      y1 := n div 2;
      x2 := ClientWidth - n;
      y2 := ClientHeight - n;
      Rgn := CreateEllipticRgn(x1, y1, x2, y2);
    
      Rect := Bounds(10,10,10,10);
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      OffsetRect(Rect, 10, 6);
      Repaint;
      Text := BoolStr[RectInRegion(Rgn, Rect)];
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      OffsetRect(Rect, -10, -6);
      Repaint;
      Text := BoolStr[RectInRegion(Rgn, Rect)];
    end;
    
    procedure TForm1.FormPaint(Sender: TObject);
    begin
      Canvas.Brush.Color := clSilver;
      Canvas.Brush.Style := bsCross;
      FillRgn(Canvas.Handle, Rgn, Canvas.Brush.Handle);
    
      Canvas.Brush.Color := clRed;
      Canvas.Brush.Style := bsSolid;
      FrameRgn(Canvas.Handle, Rgn, Canvas.Brush.Handle, 2, 2);
    
      FillRect(Canvas.Handle, Rect, Canvas.Brush.Handle);
    end;
    
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      DeleteObject(Rgn);
    end;
    
    end.
    
  • 相关阅读:
    Sql的同一机器不同数据库联合查询示例
    poi 公式自動重新計算
    js 取得文件大小
    java 读写文件
    Postman安装及入门实践(以百度搜索为例)
    PostmanTests模块测试方法记录
    HTML颜色代码
    网盘搜索器 v1.0
    线程知识点一:如何传入多个参数
    asp.net 实现省市级联<简单方法2>前台实现
  • 原文地址:https://www.cnblogs.com/del/p/1208315.html
Copyright © 2020-2023  润新知