Achieve a form with shadow effect

procedure ShadeIt(f: TForm; c: TControl; Width: Integer; Color: TColor);
var
  rect: TRect;
  old: TColor;
begin
  if (c.Visible) then
  begin
    rect := c.BoundsRect;
    rect.Left := rect.Left + Width;
    rect.Top := rect.Top + Width; 
    rect.Right := rect.Right + Width;
    rect.Bottom := rect.Bottom + Width;
    old := f.Canvas.Brush.Color;
    f.Canvas.Brush.Color := Color;
    f.Canvas.fillrect(rect);
    f.Canvas.Brush.Color := old;
  end;
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to Self.ControlCount - 1 do
    ShadeIt(Self, Self.Controls[i], 3, clBtnShadow);
end;
View Code

Mobile no window frame

    procedure WMMove(var Message: TWMMove); message WM_MOVE;
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.WMMove(var Message: TWMMove);
begin
  if TForm(Self.Owner) = nil then
    Exit;
  TForm(Self.Owner).left := Self.left - 7;
  TForm(Self.Owner).Top := Self.Top - 7; // + FDistT;
end;
View Code

Perfect PNG translucent forms solution

The system then Vista first came out, the most attractive translucent frosted window interface, and captured the hearts of many people. This immediately triggered a programming interface technology sector commotion, a lot of people are asking: how to achieve this interface effects? Of course, actually very simple in Vista, support for the system itself, there is almost no need to write a code, but was still dominated by XP, so we can study how to achieve this effect in XP. 

        The first to achieve should be a desktop weather show, as well as simple-minded bell, then fish the fish show the desktop software also successfully mimics the Vista sidebar in XP, indeed, people are excited, but they are confidential, ask also asked not exactly what technology is used, remember that the wealthiest Forum: (HTTP // on www.delphibbs.com more famous Delphi Forum) has been discussed for this purpose, and finally there is a light rain brother who called ID provided a method (of course he is not original I do not know, Shanda currently in the rain brother, met her in an infinite worship), the following is a Delphi implementation code:         

var 
PT1, PT2: TPoint; 
sz: TSize; 
bf: TBlendFunction; 
the begin 
Bitmap: = tgpbitmap. the Create (PNGFile); // this is a specific PNGFile PNG image path 
PT1: = Point (left, Top); // window to do the top corner coordinates 
PT2: = Point ( 0 , 0 ) ;// this would not have said, the sight of (0,0) should understand 
sz.cx: = bitmap.GetWidth;   // size of not more than image size, otherwise the window will have nothing, not even a shadow 
sz .cy: = bitmap.GetHeight;   // ibid 
bf.BlendOp: = AC_SRC_OVER; // these memorize the line 
bf.BlendFlags: = 0 ;                   // supra 
IF (nTran < 0 ) or (nTran> 255 ) the then  
nTran: = 255 ; 
bf.SourceConstantAlpha: = nTran;   // ibid 
bf.AlphaFormat: = AC_SRC_ALPHA; // above 
the DeleteObject (BMP); //It was here in front of mistake, or an infinite amount of memory will increase 
bitmap.GetHBITMAP ( 0 , BMP); // HBITMAP is the standard windows bitmap format that supports transparency, here is the transition from tgpbitmap to HBITMAP 
DeleteDC (DC); 
DC: = CreateCompatibleDC (Canvas.Handle); 
old_bmp: = SelectObject (DC, BMP); 
UpdateLayeredWindow (the Handle, Canvas.Handle, @ PT1, @sz, DC, @ PT2, 0 , @ bf, ULW_ALPHA); // call UpdateLayeredWindow achieve 
End ; 
         this method actually generate a PNG form, we know, is a PNG picture Alpha property, so if PNG is translucent frosted installed, the generated form that is filled with translucent frosted, Note that the above code needs to use GDIPlus class uses gdipapi, gdipobj; 

        we are not here to discuss this code, the code is written by someone else, seemingly perfect, such as system initialization interface date interrogator can be generated by the above code :



        However, this code has a fatal problem, you can try to put some controls in the Form above, such as button, edit, etc., the compiler again you will find the magic, all the controls are not displayed, this is how it happened? After a review of MSDN, we found that the problem lies in UpdateLayeredWindow function. 

        MSDN, regarding Remarks This function has such a description: 

of The UpdateLayeredWindow is function Maintains The window ' . S Appearance ON The Screen of The Windows Underneath A your layered window do Not need to BE REPAINTED When They are Uncovered Due to A Call to UpdateLayeredWindow is, because the system will automatically repaint them. this permits seamless animation of the layered window. 

        to the effect that, after using this function, the next layer of the form will not be redrawn, that is to say, the form will not respond to heavy Onpaint event painted all the controls, resulting in control can not be seen, but in fact controls exist, you can click on the position in response to what button, you will find a button click event will still respond, but they can not see. 

        This is a headache, if you can not use the controls, or controls invisible form of light and then a nice what use is it? In fact, Microsoft seems to be doing the animation with seamless connection with this function, MSDN thing to say very clearly: This Permits Seamless Animation ofthe layered window. 

        ah, well, since this method does not work, then for a bar, so it was thought to use two forms to solve. 

        2 Ge form how to solve it? In fact, very simple, as a form of semi-transparent PNG on the back, a form as the form to place controls on the front, then the form as long as two synchronous movement on it, for it took Date Finder , landing form: 



this form has a translucent top and bottom borders, but also controls the display above, you may difficult to understand, if I break it down: 



      

        how? So you find, in fact, two forms, a background form behind, in front of a Border: = none of the controls on the form, and then move to synchronizing two forms, so we forged a translucent window body. In fact, a lot of software is to do so, including some forms shaded is the same principle. 

        As for synchronous movement, is also very simple, the process OnMove message on it: 

function WndNewProc (Wnd: the HWND; the uMsg: UINT; WPAR: WPARAM; LPAR: LPARAM): LRESULT; _stdcall ;
 var  
Rect: The TRect; 
the begin 
the Result: = 0 ;
 Case the uMsg of 
the WM_LBUTTONDOWN: the SendMessage (Wnd, the WM_SYSCOMMAND, SC_MOVE+ 2 , 0 );
 the else 
the begin 
IF ((WM_MOVING the uMsg =) or (= the uMsg WM_MOVE)) and the GetWindowRect (Wnd, Rect) the then 
the SetWindowPos (ComponentForm.Handle, 0 , rect.left, rect.top, 0 , 0 , SWP_NOSIZE); 
the Result: = the DefWindowProc (Wnd, uMsg, WPAR, LPAR);
 End ;
 End ;
 End ; 
        we assume that the controls will be laid form name ComponentForm, when we press the left mouse button and move the background form when controls Forms can be followed by synchronized movement.

        Of course, sometimes we can generate directly code background form, so some programs may reduce the volume, background generated form we can call CreateWindowEx with a function, use with Ex attention here, showing there will be additional parameters, as long as we the first parameter to this function is set to WS_EX_LAYERED on it, he said an extra layer properties. 

        Both methods can, we can either directly use two forms can also be used CreateWindowEx function to generate a form background, the effect is the same, depends on personal preference.
View Code

 

Guess you like

Origin www.cnblogs.com/blogpro/p/11453698.html