Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Help me explain this procedure

Posted on 2006-11-27
16
291 Views
Last Modified: 2010-04-05
Hi, all, I'm learning to write components and I had the follwoing ShadowButton source to study, I did not quite understnad the procecedure RefRGN, could some of you help explain to me in details? Thank y ou in advance.

//.................................below codes.................//
interface

uses
  Controls, Graphics, Classes, SysUtils, Windows;

type
  TShadowPos=(spLeft,spRight,spTop,spBottom,spTopLeft,spTopRight,spBottomLeft,spBottomRight,spCenter);
  TButtonShape=(bsRectangle,bsImage);

  TMCoPShadowButton = class(TCustomControl)
  private
    { Private declarations }
    fImg,fShd:Graphics.TBitmap;
    eMU,eMD:TMouseEvent;
    fTrans,fBack,fFore,fCol:TColor;
    fAlpha:Byte;
    fShdPos:TShadowPos;
    fBtnStl:TButtonShape;
    fDown,fTraS:Boolean;
    fIP:TPoint;
    fTraMo:TTransparentMode;
    Procedure RefRGN;
  protected
    { Protected declarations }
  public
    { Public declarations }
    Constructor Create(AOwner:TComponent);override;
    Destructor Destroy;override;
    Procedure Paint;override;
  published
    { Published declarations }
//...  
end;

procedure Register;

{$R Ico.dcr}

implementation


Procedure TMCoPShadowButton.RefRGN;
var
 r,m:hrgn;
 x,y:integer;
 b:Graphics.TBitmap;
begin
if fBtnStl=bsRectangle then
 r:=createrectrgn(0,0,Width,Height)
else
 begin
 b:=Graphics.TBitmap.Create;
 b.Assign(fShd);
 b.Canvas.Draw(fIP.X,fIP.Y,fImg);
 r:=0;
 for x:=0 to b.Width-1 do
  for y:=0 to b.Height-1 do
   if b.Canvas.Pixels[x,y]<>fBack then
    begin
    if r=0 then
     r:=createrectrgn(x,y,x+1,y+1)
    else
     begin
     m:=createrectrgn(x,y,x+1,y+1);
     combinergn(r,r,m,RGN_OR);
     deleteobject(m);
     end;
    end;
 b.Free;
 end;
setwindowrgn(Handle,r,True);
deleteobject(r);
end;
0
Comment
Question by:adm_adamlee
  • 6
  • 6
  • 4
16 Comments
 
LVL 1

Author Comment

by:adm_adamlee
ID: 18017820
Especially these APIs:
createrectrgn
combinergn
setwindowrgn

Thank you
0
 
LVL 28

Expert Comment

by:2266180
ID: 18017842
well ... did you read through the MSDN?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/regions_2h0u.asp
http://msdn2.microsoft.com/en-us/library/ms536688.aspx
http://msdn2.microsoft.com/en-US/library/ms534845.aspx

they are functions to create regions. not much to understand there :)
what exactly don't you understand?
0
 
LVL 1

Author Comment

by:adm_adamlee
ID: 18024407
What are the running results of each API below?:

createrectrgn
combinergn
setwindowrgn
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:adm_adamlee
ID: 18024767
Your three links are quite helpful. Thanks.
0
 
LVL 28

Expert Comment

by:2266180
ID: 18025737
np. did you cleared the question: "What are the running results of each API below?" ? if not, can you be more specific in what you want me to explain? (I don't exactly understand the question :) )
0
 
LVL 1

Author Comment

by:adm_adamlee
ID: 18033310
I read the MSDN and have some understanding to these api:
createrectrgn
combinergn
setwindowrgn

In the procedure Procedure TMCoPShadowButton.RefRGN; what I mean, if you could explain each line, how it works, that would be better.

Thank you in advance.

0
 
LVL 28

Accepted Solution

by:
2266180 earned 250 total points
ID: 18035554
hm.... ok

begin
if fBtnStl=bsRectangle then// if you button is a rectangle,.
 r:=createrectrgn(0,0,Width,Height)// then just create a rectangluar region and the reagion is done
else // else, our button has a shape other than a rectangle, in which case we must create an appropriate region
 begin
 b:=Graphics.TBitmap.Create;// create a dummy bitmap
 b.Assign(fShd);// copy the shadow in the bitmap
 b.Canvas.Draw(fIP.X,fIP.Y,fImg);// copy (draw) the image in the bitmap
 r:=0;// initialization (now, we should have in the bitmap the button as it looks on the screen)
 for x:=0 to b.Width-1 do
  for y:=0 to b.Height-1 do// iterate trhough all pixels of the bitmap
   if b.Canvas.Pixels[x,y]<>fBack then
    begin
    if r=0 then// do we have the initial region?
       r:=createrectrgn(x,y,x+1,y+1)//. no. create it (this will be 1 (one) pixel region.
            else
       begin
         m:=createrectrgn(x,y,x+1,y+1);// now create another temporary pixel region
         combinergn(r,r,m,RGN_OR);// combine the 2, and now we have a region formed out of some pixels, not necessarely one near another
         deleteobject(m);// delete the temporary pixel region
       end;
    end;
 b.Free;// free up the bitmap as we finished with it.
 end;
setwindowrgn(Handle,r,True);// now set the region to the region we just created
deleteobject(r);// delete the region
end;

conclusion: the button will have a region everywhere there it has no fback pixel. so you look at the button, and where it has a fback pixel, you make a whole in it. something like that.
I'm not exactly a good explainer so if you still need explanations, just tell me exactly which part don't you understand. you might also want to take a look at these articles: http://delphi.about.com/od/windowsshellapi/l/aa120898.htm (they use the same api calls and maybe you can find better explanations there :)
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 18039147
Essentially, the routine is creating the region that is used in the SetWindowRgn call to create a Button that
is shaped like the region like the bitmap. As Russell said, it uses CombineRegion to combine all the regions that
consist of 1 pixel to create a larger region that does not have the fBack color. It is much easier to do this
using another routine, however, since this is a small rectangle, it would not help performance.
0
 
LVL 28

Expert Comment

by:2266180
ID: 18039302
> As Russell said
yeah, he tends to do that now and then :P
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 18040784
Well, you certainly have begun to do that, too, this year.
0
 
LVL 28

Expert Comment

by:2266180
ID: 18041111
I was trying to say that Russel didn't post in this question ;-)
0
 
LVL 1

Author Comment

by:adm_adamlee
ID: 18042665
Actually,l ciuly, you are a good explainer. thanks a lot.
0
 
LVL 28

Expert Comment

by:2266180
ID: 18043497
no problem. let us know if there is anything else you need help on for these functions ;)
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 18045465
oh, I don't know why I thought it was Russell's post, my bad...
0
 
LVL 1

Author Comment

by:adm_adamlee
ID: 18050292
I forgot to split the scores, Eddieshipman should have some.

In the future I have a lot of questions to ask here. I trust this site can give me help.

Thank you all.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 18053793
FYI, you can reopen to split by going to Community Service and requesting it.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question