Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Simple way to rotate regionFormat this article printer-friendly!Bookmark function is only available for registered users!
Rotating regions
Product:
Delphi 7.x (or higher)
Category:
Win API
Skill Level:
Scoring:
Last Update:
04/09/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet region, OffsetRgn, GetRegionData, ExtCreateRegion, GetRgnBox, rotate
Times Scored:
2
Visits:
2851
Uploader: Nik Ozniev
Company:
Reference: N/A
 
Question/Problem/Abstract:
Simple function returning rotated region. Pprocedure doing the same with source region.
Answer:



It's the simple function returning new region rotated to the angle that
you want around the source region. Source region doesn't change.

The second procedure does the same with source region without creating new region.

I hope this will be useful.
Comments are provided along the code

Function _RotateRgn(ARgn : HRGN; ADegree : Real) : HRGN;
Var
  wXFORM  : XFORM;    // transformation structure, see Windows API
  kRgnD   : DWord;    // count of RGNDATA structures in region
  RgnData : PRgnData; // pointer to region data
  Rt      : TRect;
  kX, kY  : Integer;
begin
  if (ARgn = 0) or (ADegree = 0) then
    Exit;
    
  // Get region's surrounding rectangular
  GetRgnBox(ARgn, Rt);

  // Move source region so that the centre of its surrounding rectangular
  // goes to the left top corner of a window
  kX := Rt.Left+(Rt.Right -Rt.Left) div 2;
  kY := Rt.Top +(Rt.Bottom-Rt.Top) div 2;
  OffsetRgn(ARgn, -kX, -kY);

  // Fill XFORM according to task (rotate region)
  FillChar(wXFORM, SizeOf(wXFORM), #0);
  wXFORM.eM11 := Cos(ADegree/180*pi);
  wXFORM.eM12 := -Sin(ADegree/180*pi);
  wXFORM.eM21 := -wXFORM.eM12;
  wXFORM.eM22 := wXFORM.eM11;

  // Prepare buffer to store region data
  kRgnD := GetRegionData(ARgn, 0, nil);
  GetMem(RgnData, SizeOf(RGNDATA) * kRgnD);
  // ..and fill the buffer with region's data
  GetRegionData(ARgn, kRgnD, RgnData);
  // ..move source region to its initial position
  OffsetRgn(ARgn, kX, kY);

  // Create output region using data in the buffer and transformation wXFORM
  Result := ExtCreateRegion(@wXFORM, kRgnD, RgnData^);
  // Move output region on a place of source region
  OffsetRgn(Result, kX, kY);
  FreeMem(RgnData);
end;

Procedure _RotateRgnEx(Var ARgn : HRGN; ADegree : Real);
Var
  wXFORM  : XFORM;    // transformation structure, see Windows API
  kRgnD   : DWord;    // count of RGNDATA structures in region
  RgnData : PRgnData; // pointer to region data
  Rt      : TRect;
  kX, kY  : Integer;
begin
  if (ARgn = 0) or (ADegree = 0) then
    Exit;
    
  // Get region's surrounding rectangular
  GetRgnBox(ARgn, Rt);

  // Move source region so that the centre of its surrounding rectangular
  // goes to the left top corner of a window
  kX := Rt.Left+(Rt.Right -Rt.Left) div 2;
  kY := Rt.Top +(Rt.Bottom-Rt.Top) div 2;
  OffsetRgn(ARgn, -kX, -kY);

  // Fill XFORM according to task (rotate region)
  FillChar(wXFORM, SizeOf(wXFORM), #0);
  wXFORM.eM11 := Cos(ADegree/180*pi);
  wXFORM.eM12 := -Sin(ADegree/180*pi);
  wXFORM.eM21 := -wXFORM.eM12;
  wXFORM.eM22 := wXFORM.eM11;

  // Prepare buffer to store region data
  kRgnD := GetRegionData(ARgn, 0, nil);
  GetMem(RgnData, SizeOf(RGNDATA) * kRgnD);
  // ..and fill the buffer with region's data
  GetRegionData(ARgn, kRgnD, RgnData);
  // ..delete source region
  DeleteObject(ARgn);

  // Create new region using data in the buffer and transformation wXFORM
  ARgn := ExtCreateRegion(@wXFORM, kRgnD, RgnData^);
  // Move output region to the origin place
  OffsetRgn(ARgn, kX, kY);
  FreeMem(RgnData);
end;





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
S. Carter
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)