We help IT Professionals succeed at work.

Help with alphablend

hidrau
hidrau asked
on
921 Views
Last Modified: 2013-11-12
Hello Guys,

is it possible to use alphablend only in a form without affects all the components on the form?

With alphablend property, when I use it, all the component are affected.

I use Delphi x4

Thanks
Alex
Comment
Watch Question

Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I'll google a few, but this is the code I used to make a form transparent without affecting other components:

//declare within your private variables
var
  FullRgn, ClientRgn, CtlRgn, TBordRgn, BBordRgn, LBordRgn, RBordRgn,
  EllRgn, EllRgn2, EllRgn3 : THandle;

//procedure which does the magic
procedure TBarFrm.DoInvisible;
var
  AControl : TControl;
  A, Margin, X, Y, CtlX, CtlY : Integer;
begin
  Margin := ( Width - ClientWidth ) div 2;
  //First, get form region
  FullRgn := CreateRectRgn(0, 0, Width, Height);
  //Find client area region
  X := Margin;
  Y := Height - ClientHeight - Margin;
  ClientRgn := CreateRectRgn( X, Y, X + ClientWidth, Y + ClientHeight );
  //'Mask' out all but non-client areas
  CombineRgn( FullRgn, FullRgn, ClientRgn, RGN_DIFF );

  //Now, walk through all the controls on the form and 'OR' them
  // into the existing Full region.
  for A := 0 to ControlCount - 1 do begin
    AControl := Controls[A];
    if ( AControl is TComboBox ) or (AControl is TRxSpeedButton)
        then with AControl do begin
      if Visible then begin
        CtlX := X + Left;
        CtlY := Y + Top;
        CtlRgn := CreateRectRgn( CtlX, CtlY, CtlX + Width, CtlY + Height );
        CombineRgn( FullRgn, FullRgn, CtlRgn, RGN_OR );
      end;
    end;
  end;
//here you can set different border accordingly to the user choice stored in regEntries.noBorder, regEntries.smallBord and so on. You can use only a little part of this code to set borders of your form

  if not regEntries.noBorder then
  begin
    if regEntries.smallBord then
    begin
      TBordRgn := CreateRectRgn(0, 0, 150, 2);     //left, top, right, bottom
      CombineRgn(FullRgn, FullRgn, TBordRgn, RGN_OR);
      BBordRgn := CreateRectRgn(0, 80, 150, 82);
      CombineRgn(FullRgn, FullRgn, BBordRgn, RGN_OR);
      LBordRgn := CreateRectRgn(0, 2, 2, 80);
      CombineRgn(FullRgn, FullRgn, LBordRgn, RGN_OR);
      RBordRgn := CreateRectRgn(148, 2, 150, 80);
      CombineRgn(FullRgn, FullRgn, RBordRgn, RGN_OR);
    end else if regEntries.medBord then
    begin
      TBordRgn := CreateRectRgn(0, 0, 150, 3);     //left, top, right, bottom
      CombineRgn(FullRgn, FullRgn, TBordRgn, RGN_OR);
      BBordRgn := CreateRectRgn(0, 79, 150, 82);
      CombineRgn(FullRgn, FullRgn, BBordRgn, RGN_OR);
      LBordRgn := CreateRectRgn(0, 3, 3, 79);
      CombineRgn(FullRgn, FullRgn, LBordRgn, RGN_OR);
      RBordRgn := CreateRectRgn(147, 3, 150, 80);
      CombineRgn(FullRgn, FullRgn, RBordRgn, RGN_OR);
    end else if regEntries.largeBord then
    begin
      TBordRgn := CreateRectRgn(0, 0, 150, 4);
      CombineRgn(FullRgn, FullRgn, TBordRgn, RGN_OR);
      BBordRgn := CreateRectRgn(0, 78, 150, 82);
      CombineRgn(FullRgn, FullRgn, BBordRgn, RGN_OR);
      LBordRgn := CreateRectRgn(0, 4, 4, 78);
      CombineRgn(FullRgn, FullRgn, LBordRgn, RGN_OR);
      RBordRgn := CreateRectRgn(146, 4, 150, 79);
      CombineRgn(FullRgn, FullRgn, RBordRgn, RGN_OR);
    end;
  end;

  // Create transparent form
  EllRgn := CreateRectRgn(11, 59, 28, 74);        //left, top, right, bottom
  CombineRgn(FullRgn, FullRgn, EllRgn, RGN_OR);
  EllRgn2 := CreateRectRgn(35, 59, 52, 74);
  CombineRgn(FullRgn, FullRgn, EllRgn2, RGN_OR);
  EllRgn3 := CreateRectRgn(59, 59, 76, 74);
  CombineRgn(FullRgn, FullRgn, EllRgn3, RGN_OR);
  //When the region is all ready, put it into effect:
  SetWindowRgn(Handle, FullRgn, TRUE);
end;

//Free memory
procedure TForm1FormDestroy(Sender: TObject);
begin
  //Clean up the regions we created
  DeleteObject(ClientRgn);
  DeleteObject(FullRgn);
  DeleteObject(CtlRgn);
end;

Open in new window


Hope this helps
Cheers
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I found this tutorial which seems to be interesting: you can see if it makes sense for you: http://melander.dk/articles/alphasplash/

Author

Commented:
marqusG,

I am having problem in this part:

Checking project dependencies...
Compiling Project2.dproj (Debug configuration)
[DCC Error] Unit1.pas(64): E2003 Undeclared identifier: 'regEntries'
[DCC Error] Unit1.pas(64): E2029 'THEN' expected but identifier 'noBorder' found
[DCC Error] Unit1.pas(66): E2029 'THEN' expected but identifier 'smallBord' found
[DCC Error] Unit1.pas(76): E2029 'THEN' expected but identifier 'medBord' found
[DCC Error] Unit1.pas(86): E2029 'THEN' expected but identifier 'largeBord' found
[DCC Hint] Unit1.pas(16): H2219 Private symbol 'DoInvisible' declared but never used
[DCC Fatal Error] Project2.dpr(5): F2063 Could not compile used unit 'Unit1.pas'
Failed
Elapsed time: 00:00:00.1
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
All those variables have to be replaced by actual values: I posted the code as I used but regEntries was a unit when I initialized values loading from registry or storing them in registry. NoNorder, smallBord, medBord, largeBord are variables you must declare. My code allows you to set borders but you have to set if you want a smallBorder or a largeBorder.

DoInvisible is a procedure: declare it in your private clause and be sure your form is called Form1, otherwise you have to change

procedure TForm1.DoInvisible

with TYourFormName.DoInvisible
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Use this code as it is: it will draw a small border around. If you don't want the border comment out lines 32-39.
Remember to change TForm1 with the actual form you're using and to declare the procedure DoInvisible in your private clause.

procedure TForm1.DoInvisible;
var
  AControl : TControl;
  A, Margin, X, Y, CtlX, CtlY : Integer;
begin
  Margin := ( Width - ClientWidth ) div 2;
  //First, get form region
  FullRgn := CreateRectRgn(0, 0, Width, Height);
  //Find client area region
  X := Margin;
  Y := Height - ClientHeight - Margin;
  ClientRgn := CreateRectRgn( X, Y, X + ClientWidth, Y + ClientHeight );
  //'Mask' out all but non-client areas
  CombineRgn( FullRgn, FullRgn, ClientRgn, RGN_DIFF );

  //Now, walk through all the controls on the form and 'OR' them
  // into the existing Full region.
  for A := 0 to ControlCount - 1 do begin
    AControl := Controls[A];
    if ( AControl is TComboBox ) or (AControl is TRxSpeedButton)
        then with AControl do begin
      if Visible then begin
        CtlX := X + Left;
        CtlY := Y + Top;
        CtlRgn := CreateRectRgn( CtlX, CtlY, CtlX + Width, CtlY + Height );
        CombineRgn( FullRgn, FullRgn, CtlRgn, RGN_OR );
      end;
    end;
  end;
  
  //this will you give a small border
  TBordRgn := CreateRectRgn(0, 0, 150, 2);     //left, top, right, bottom
  CombineRgn(FullRgn, FullRgn, TBordRgn, RGN_OR);
  BBordRgn := CreateRectRgn(0, 80, 150, 82);
  CombineRgn(FullRgn, FullRgn, BBordRgn, RGN_OR);
  LBordRgn := CreateRectRgn(0, 2, 2, 80);
  CombineRgn(FullRgn, FullRgn, LBordRgn, RGN_OR);
  RBordRgn := CreateRectRgn(148, 2, 150, 80);
  CombineRgn(FullRgn, FullRgn, RBordRgn, RGN_OR);
  
  EllRgn := CreateRectRgn(11, 59, 28, 74);        //left, top, right, bottom
  CombineRgn(FullRgn, FullRgn, EllRgn, RGN_OR);
  EllRgn2 := CreateRectRgn(35, 59, 52, 74);
  CombineRgn(FullRgn, FullRgn, EllRgn2, RGN_OR);
  EllRgn3 := CreateRectRgn(59, 59, 76, 74);
  CombineRgn(FullRgn, FullRgn, EllRgn3, RGN_OR);
  //When the region is all ready, put it into effect:
  SetWindowRgn(Handle, FullRgn, TRUE);
end;

Open in new window

Author

Commented:
marqusG,

I don't want the invisible form, that I can do with transparentcolor.

I'd like a alphablend form opacity but that all my buttons, edits could not be affected.

Even though, I thank you  very much to trying to help me.

But Did you get it?

Author

Commented:
Well, I think I found the way, as you can see the answer on that link I gave to you, but it only works on windows vista upper, it doesn't work on windows XP.

And as we can see, it is different from alpha blend. the form gets a like a blue color transparent. It wasn't what I'd like. I'd like an effect like a glass.
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Have tak a look at the link I posted above? I didn't read it but it seems interesting... I talk about translucent form.
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I found teh component TAeroGlass that you can find on a dedicated Torry's page.

The component is declared for D2 up to Delphi 2006 but I've installed in Delphi XE3 and it works without problem. It gives you full transparency, glass effect in a region you can define settings margin values and a full translucent effect as you can see in the image below. For this effect you have to set component's properties as follows:

DragGlass: dgNone
MarginBottom, MarginLeft, MarginRight, MarginTop as they are
SheetOfGlass: True

Glass form with normal components
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Ooops, jpg is not a ggod format to post that image:

png more near to the real effect
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
A better sample:)

This is good enough, I think...
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I just seen that not always teh form is translucent: launching the exe file sometimes is translucent, somtimes is transparent, so this component is not affordable, but its source code can teach you something interesting about the effect you want...
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Lol, I dodn't follow the link you psted above so I didn't know you had solved the problem yet :)
Cheers

Author

Commented:
Hello marqusG,

Thanks very much, I will download it and try it.
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
WAIT, WAIT! Don't install that component: study the source, but don't install it. I've just uninstalled because it is malformed: when you try to remove it from the form it raises an error saying it isn't possible cancel an external created thread plus other errors of the IDE. So I repeat: don't install it.

Let's me ask a thing: in the link you posted above there is a solution simple and clear: the FlassFrame property which is native in Delphi XE3 so I suppose you have it also in your Delphi XE4... or Delphi x4 is another thing?

Author

Commented:
Ok, I didn't install the component. Thanks for the concerning.

I use Delphi X4.
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I know Delphi 4 and Delphi XE4, but not Delphi X4 :) Am i missing something?

Author

Commented:
Sorry, I made a mistake with my x4 LOL, you are right, it is XE4 :)
Freelancer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Yeah, it is correct.
Thanks

Author

Commented:
thanks
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Thanks for undeserved points and good luck with your project.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.