zoukankan      html  css  js  c++  java
  • 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.
    
  • 相关阅读:
    java类,接口浅谈
    人月神话阅读笔记01
    学习进度条14
    学习进度条13
    每日站立会议10(完结)
    每日站立会议09
    每日站立会议08
    构建之法阅读笔记06(完)
    每日站立会议07
    每日站立会议06
  • 原文地址:https://www.cnblogs.com/del/p/1208315.html
Copyright © 2011-2022 走看看