[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help me explain this procedure

Posted on 2006-11-27
16
Medium Priority
?
301 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

834 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