形式 – 圆形与系统阴影

前端之家收集整理的这篇文章主要介绍了形式 – 圆形与系统阴影前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图用SetWindowRgn,我不能.

可以这样做(顶部的两个角落是圆的,窗口有一个阴影)像这张照片?

解决方法

以下是如何使用阴影设置窗口区域的代码示例:
(注意:表单BorderStyle假定为bsnone,不可重新定义)
  1. type
  2. TForm1 = class(TForm)
  3. procedure FormCreate(Sender: TObject);
  4. private
  5. procedure CreateFlatRoundRgn;
  6. protected
  7. procedure CreateParams(var Params: TCreateParams); override;
  8. public
  9. end;
  10.  
  11. var
  12. Form1: TForm1;
  13.  
  14. implementation
  15.  
  16. {$R *.DFM}
  17.  
  18. procedure ExcludeRectRgn(var Rgn: HRGN; LeftRect,TopRect,RightRect,BottomRect: Integer);
  19. var
  20. RgnEx: HRGN;
  21. begin
  22. RgnEx := CreateRectRgn(LeftRect,BottomRect);
  23. CombineRgn(Rgn,Rgn,RgnEx,RGN_OR);
  24. DeleteObject(RgnEx);
  25. end;
  26.  
  27. procedure TForm1.CreateFlatRoundRgn;
  28. const
  29. CORNER_SIZE = 6;
  30. var
  31. Rgn: HRGN;
  32. begin
  33. with BoundsRect do
  34. begin
  35. Rgn := CreateRoundRectRgn(0,Right - Left + 1,Bottom - Top + 1,CORNER_SIZE,CORNER_SIZE);
  36. // exclude left-bottom corner
  37. ExcludeRectRgn(Rgn,Bottom - Top - CORNER_SIZE div 2,CORNER_SIZE div 2,Bottom - Top + 1);
  38. // exclude right-bottom corner
  39. ExcludeRectRgn(Rgn,Right - Left - CORNER_SIZE div 2,Right - Left,Bottom - Top);
  40. end;
  41. // the operating system owns the region,delete the Rgn only SetWindowRgn fails
  42. if SetWindowRgn(Handle,True) = 0 then
  43. DeleteObject(Rgn);
  44. end;
  45.  
  46. procedure TForm1.FormCreate(Sender: TObject);
  47. begin
  48. BorderStyle := bsNone;
  49. CreateFlatRoundRgn;
  50. end;
  51.  
  52. procedure TForm1.CreateParams(var Params: TCreateParams);
  53. const
  54. CS_DROPSHADOW = $00020000;
  55. begin
  56. inherited CreateParams(Params);
  57. with Params do
  58. begin
  59. Style := WS_POPUP;
  60. WindowClass.Style := WindowClass.Style or CS_DROPSHADOW;
  61. end;
  62. end;

绘制自定义阴影的另一种方法是设置Window WS_EX_LAYERED并使用UpdateLayeredWindow

这是一个very good example的完成(来源是C,但很容易理解)

对于更复杂的形状,您可以在表单和Alpha Blend上使用PNG图像.

编辑:

调整WS_POPUP窗口大小是一个痛苦的世界…
你有几个选择:

>写一个WM_NCHITEST handler
>使用WM_Syscommand $F008调整大小(上面的链接)或$F012到move the Window.
>使用WS_EX_STATICEDGE and WS_SIZEBOX样式.

请注意,当您重新调整窗口区域(例如OnResize事件)时,需要重新创建窗口区域.

猜你在找的Delphi相关文章