Solved

Help me explain this procedure

Posted on 2006-11-27
16
280 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:ciuly
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
 
LVL 1

Author Comment

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

Expert Comment

by:ciuly
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:
ciuly 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 28

Expert Comment

by:ciuly
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:ciuly
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:ciuly
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now