Solved

Help me explain this procedure

Posted on 2006-11-27
16
294 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
[X]
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
  • 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
Independent Software Vendors: 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 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Spellcheck in Webbrowser 1 73
Help on project with Soap 10 67
Delphi: ForceDirectory plain function 7 78
How to Get Images From Server using App Tethering 11 49
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

738 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